gpt4 book ai didi

java - 使用缓冲区复制时更改了新位图

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:22 24 4
gpt4 key购买 nike

当我使用 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显示的内容不一样?

最佳答案

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

问题从documentation就很清楚了的 copyPixelsFromBuffer() :

The data in the buffer is not changed in any way (unlike setPixels(), which converts from unpremultipled 32bit to whatever the bitmap's native format is.

所以要么使用相同的位图格式,要么使用setPixels()或使用 Canvas.drawBitmap() 将原始位图绘制到新位图上打电话。

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

关于java - 使用缓冲区复制时更改了新位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9964138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com