Amokrane Chentir
Amokrane Chentir

Reputation: 30385

How to convert a Bitmap to ByteArray without the use of compression?

I would like to convert an instance of Bitmapto an instance of ByteArrayin order to be able to pass it to a Webservice.

Right now, what I'm doing is:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);  
byte[] imageData = bos.toByteArray();
ByteArrayBody byteArrayBody = new ByteArrayBody(imageData, imagePath);

The problem here, is that I am using compression to make it happen, and this results in lowering the quality of the bitmap.

Is there any other way to do this?

Thanks!

EDIT: I can't compress to PNG, since the server uses JPEG.

Upvotes: 0

Views: 3073

Answers (2)

Jave
Jave

Reputation: 31846

  • As Sean Owen said, use png rather than jpeg.
  • You can get an int array with getPixels().
  • You can get a buffer object with copyPixelsToBuffer(), which you can later convert to a byte[].

In any case png compression is the best solution as it does not need as many intermediary operations and gives a compressed result.

Upvotes: 3

Sean Owen
Sean Owen

Reputation: 66886

Use a lossless format like PNG then. JPEG is lossy.

Upvotes: 3

Related Questions