2015-08-09 200 views
0

我有這臺機器,我必須捕獲產品的圖像並填充其他數據以將它們上載到服務器。我一直在使用圖像捕捉意圖,但系統將圖像縮小到150 * 200,但感謝您現在我擁有全尺寸圖像。 問題是圖像的大小是一個大的(2-5mbs)我試圖縮小位圖方法和畢加索圖書館,但圖像文件仍然具有相同的大小。你能給我指導如何減少它的大小(如果需要的話甚至是分辨率)。 我的嘗試:減少位圖大小

public void grabImage(ImageView imageView) { 
    this.getContentResolver().notifyChange(mImageUri, null); 
    ContentResolver cr = this.getContentResolver(); 
    Bitmap bitmap; 
    try 
    { 
     bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri); 

     //ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     Bitmap bm = ShrinkBitmap(fullpath, bitmap.getWidth(), bitmap.getHeight()); 
     imageView.setImageBitmap(bm); 
    } 
    catch (Exception e) 
    { 
     Log.d("This", "Failed to load", e); 
    } 
} 

Bitmap ShrinkBitmap(String file, int width, int height){ 

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
    bmpFactoryOptions.inJustDecodeBounds = true; 
    Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 

    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height); 
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width); 

    if (heightRatio > 1 || widthRatio > 1) 
    { 
     if (heightRatio > widthRatio) 
     { 
      bmpFactoryOptions.inSampleSize = 4; 
     } else { 
      bmpFactoryOptions.inSampleSize = 4; 
     } 
    } 

    bmpFactoryOptions.inJustDecodeBounds = false; 
    bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions); 
    return bitmap; 
} 

問候,

+0

顯示你的嘗試。 – iTurki

+0

[如何在上載到服務器之前減少圖像文件大小]的可能重複(http://stackoverflow.com/questions/18573774/how-to-reduce-an-image-file-size-before-uploading-to -a服務器) –

回答

0

以下網站非常清楚地解釋瞭如何以高效的方式加載圖像。這意味着,在加載圖片時,您已經以2(2,4,8,...)的最高次冪進行縮放,以使加載的圖片仍然大於所需的大小。這樣你只需加載你實際需要的東西,並使用更少的內存。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

(我看你使用此過程的一些元素在你的代碼,但它沒有意義對我來說)

在第二個步驟,那麼你可以進一步縮小圖片的實際寬度+ heigth你需要,這隻需要完成: Bitmap.createScaledBitmap(bitmap_large,(int)width,(int)height,false);

0

試試這個;

if (heightRatio > 1 || widthRatio > 1) 
{ 
if (heightRatio > widthRatio) 
{ 
bmpFactoryOptions.inSampleSize *=2; 
} 
else { 
bmpFactoryOptions.inSampleSize = 4; 
} 
}