Decode byte array to bitmap that has been compressed in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 05:47:24

问题


I am compressing a bitmap in the following way

Bitmap bmpSig = getMyBitMap();
int size = bmpSig.getWidth() * bmpSig.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out);   
byte[] bytSig = out.toByteArray();

I am then trying to display the image in an Android ImageView from the byte array. When I do this I get an image that is completely black image.

ImageView myImg = (ImageView) findViewById(R.id.img_view);
myImg.setImageBitmap(BitmapFactory.decodeByteArray(bytSig, 0, bytSig.length));

I'm assuming it's because I am missing a step before BitmapFactory.decodeByteArray() to reverse the jpeg compression. Or have I misunderstood how the compression works?


回答1:


I didn't realise that the background of my bitmap (from a Canvas object) was transparent. Since this bitmap is just black lines on a white background the black image is due to compressing to JPEG giving the image a black background.

I have changed

bmpSig.compress(Bitmap.CompressFormat.JPEG, 100, out); 

to

bmpSig.compress(Bitmap.CompressFormat.PNG, 100, out); 

And it is working as expected.



来源:https://stackoverflow.com/questions/5243547/decode-byte-array-to-bitmap-that-has-been-compressed-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!