6

当我使用 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);

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

4

1 回答 1

1

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

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

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

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

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

于 2012-04-01T14:16:04.987 回答