2017-10-11 175 views
0

每次我更新相同的圖片時,圖像壓縮方法都會降低圖像質量。如何將圖像轉換爲Base64字符串而不壓縮圖像?

public String BitMapToString(Bitmap bitmap) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
    byte[] b = baos.toByteArray(); 
    base64Image = Base64.encodeToString(b, Base64.DEFAULT); 
    return base64Image; 
} 

我不想一次又一次壓縮圖像。請提出建議。

+0

是什麼'「更新同一畫面「」是什麼意思? – pskink

+0

刪除此行bitmap.compress(Bitmap.CompressFormat.PNG,100,baos); –

+0

不,它沒有。 PNG壓縮是無損的。 – EJP

回答

1

首先使用PNG壓縮不會失去圖像質量。

如果你想獲得無壓縮字節無論如何,你可以嘗試以下方法:

ByteBuffer buffer = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight()); 
bitmap.copyPixelsToBuffer(buffer); 
byte[] data = buffer.array(); 

要獲得Bitmap回從字節數組:

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(data)); 
return bitmap; 
相關問題