2012-04-01 63 views
5

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

为什么位图BM和newbm不显示相同的内容?

回答

0

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

的问题是从copyPixelsFromBuffer()documentation明确:

在缓冲区不以任何方式改变(不像setPixels()数据, 从unpremultipled 32位转换到任何位图的 原生格式是

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

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

+0

BitmapFactory.Options opts = new Options(); opts.inPreferredConfig = Config.RGB_565; \t \t \t \t \t bm = BitmapFactory.decodeByteArray(a,0,a.length,opts); 原始位图的配置是RGB565,即使我没有设置配置,配置默认为RGB565 ... – Hexor 2012-04-01 16:08:47

+0

@Hexor在我的回答工作中描述的其他方法吗? 'newbm'看起来和'bm'有什么不同? – 2012-04-01 18:27:15

+0

尺寸不对,质量很低。我自己找到了一个解决方案,创建一个名为originBm的Bitmap,并调用BitmapFactory的decode方法初始化originBm(只要确保参数可以初始化originBm),然后使用newbm = originBm; newbm.copyPixelsFromBuffer(缓冲器1)。在此之后,新手可以展示正确的东西。我不知道是哪个原因造成这种情况,但我想也许在使用copyPixelsFromBuffer()时,我们会错过createBitmap()方法无法给我们的东西。 – Hexor 2012-04-02 09:06:10