New bitmap changed when copying using buffer

When I use copyPixelsFromBuffer and copyPixelsToBuffer, the bitmap does not display as the same thing, I tried under the code:

Bitmap bm = BitmapFactory.decodeByteArray(a, 0, a.length);
int[] pixels = new int[bm.getWidth() * bm.getHeight()];
bm.getPixels(pixels, 0, bm.getWidth(), 0, 0,bm.getWidth(),bm.getHeight());

ByteBuffer buffer = ByteBuffer.allocate(bm.getRowBytes()*bm.getHeight());
bm.copyPixelsToBuffer(buffer);//I copy the pixels from Bitmap bm to the buffer

ByteBuffer buffer1 = ByteBuffer.wrap(buffer.array());
newbm = Bitmap.createBitmap(160, 160,Config.RGB_565);
newbm.copyPixelsFromBuffer(buffer1);//I read pixels from the Buffer and put the pixels     to the Bitmap newbm.

imageview1.setImageBitmap(newbm);
imageview2.setImageBitmap(bm);

Why did the bm and newbm bitmaps not display the same content?

+5
source share
1 answer

In the code, you copy the pixels to a raster image with a format RGB_565, while the original raster image from which you received the pixels must be in a different format.

The problem is obvious from the documentation copyPixelsFromBuffer() :

The data in the buffer is not changed in any way (unlike setPixels(), which converts from unsremultipled 32bit to any raster native format.

, , setPixels() Canvas.drawBitmap().

bm.getWidth() bm.getHeight(), 160.

0

All Articles