Convert To And From Uncompressed Bitmap To Byte Array
I am trying to implement two methods. One that takes an ImageView as input and outputs uncompressed byte array. The second takes byte array input and converts to a bitmap. These a
Solution 1:
Try this on your imageToBytes method
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
And on your bytesToImage
BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
Solution 2:
So the problem is with the bitmap in bytesToImage being incorrectly configured.
This requires the original bitmap to be passed in directly.
Here is the updated bytesToImage method which gives correct answer.
private static Bitmap bytesToImage(byte data[], Bitmap originalImage) {
Bitmap newBmp;
newBmp = Bitmap.createBitmap(originalImage.getWidth(), originalImage.getHeight(), originalImage.getConfig());
ByteBuffer buffer1 = ByteBuffer.wrap(data);
buffer1.rewind();
newBmp.copyPixelsFromBuffer(buffer1);
byte[] imageInByte = null;
ByteBuffer byte_buffer = ByteBuffer.wrap(data);
byte_buffer.rewind();
newBmp.copyPixelsFromBuffer(byte_buffer);
return newBmp;
}
Post a Comment for "Convert To And From Uncompressed Bitmap To Byte Array"