使用缓冲区更改复制的新位图

Hex*_*xor 6 java android bitmap bitmapfactory

当我使用copyPixelsFromBuffer和copyPixelsToBuffer时,位图没有显示为相同的位图,我试过下面的代码:

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);
Run Code Online (Sandbox Code Playgroud)

为什么Bitmap bm和newbm没有显示相同的内容?

Dhe*_*.S. 1

在代码中,您将像素复制到具有RGB_565格式的位图中,而从中获取像素的原始位图必须采用不同的格式。

从以下文档中可以清楚地看出问题copyPixelsFromBuffer()

缓冲区中的数据不会以任何方式更改(与 不同setPixels(),它从未预乘的 32 位转换为位图的本机格式。

因此,要么使用相同的位图格式,要么使用原始位setPixels()图或通过调用将原始位图绘制到新位图上Canvas.drawBitmap()

还可以使用bm.getWidth()&bm.getHeight()指定新位图的大小,而不是硬编码为160