2013-11-28 51 views
6

我試圖通過使用壓縮方法來減少位圖大小。位圖壓縮不會更改位圖字節大小

這是我的代碼:

public Bitmap compressImage(Bitmap image) { 
     Bitmap immagex = image; 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     Log.i("before compress", immagex.getByteCount()+""); 

     boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); 

     if(compress) 
      Log.i("after compress", immagex.getByteCount()+""); 
     else 
      Log.i("bad compress", "bad compress"); 

     return immagex; 
    } 

當我檢查我的日誌我得到:

11-28 11:10:38.252: I/before compress(2429): 374544 
11-28 11:10:38.262: I/after compress(2429): 374544 

爲什麼壓縮不起作用?

UPDATE:

我試過這段代碼:

public Bitmap compressImage(Bitmap image) { 
     Bitmap immagex = image; 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     Log.i("before compress", immagex.getByteCount()+""); 

     boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); 

     Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+""); 

     return immagex; 
    } 

還是一樣的字節數

11-28 11:33:04.335: I/before compress(3472): 374544 
11-28 11:33:04.395: I/after compress 2(3472): 374544 
+0

請參考以下鏈接http://stackoverflow.com/questions/8417034/how-to-make-bitmap -compress-without-change-the-bitmap-size – prakash

+1

它不是我的問題。 我不想縮放我的位圖只是爲了減少內存中的大小通過改變是很容易。 – dasdasd

+2

找到了解決此問題的解決方案?我在這裏面對同樣的問題。 – Prateek

回答

1

使用Bitmap.compress()你只需指定壓縮算法,並由雙向壓縮操作需要相當長的時間。如果您需要使用縮小尺寸來減少圖像的內存分配,則您需要使用Bitmap.Options對圖像進行縮放,首先計算位圖邊界,然後將其解碼爲指定的大小。

我在StackOverflow上找到的最佳示例就是這一個。 Strange out of memory issue while loading an image to a Bitmap object

+2

我不想縮放位圖只是爲了減少質量。 – dasdasd

4

以下是減少位圖大小,並將其轉換爲Base64編碼,

Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] byteArray = baos.toByteArray(); 
    String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT); 

    Log.e("Base 64 String", imageEncoded); 
+0

感謝您的評論,但我已經嘗試過不使用更改大小的其他解決方案... – dasdasd