6

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 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nah ... you just needed to use a darker shade of black.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.