Android 位图操作
你能告诉我这段代码有什么问题吗?
//bmp is a bitmap of already present image
int width=bmp.getWidth();
int height=bmp.getHeight();
int rgbval[]=new int[width*height];
bmp.getPixels(rgbval, 0, width, 0, 0, width, height);
rgbval=actual(rgbval);
Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true);
bmp2.setPixels(rgbval, 0, width, 0, 0, width, height);
actual
是我创建的一个函数,用于操作 bmp 的 rgb 值。通过使用 eclipse 的调试功能,我已经检查它是否正常工作,但是当我尝试恢复 bmp2 的 rgb 值时,我没有得到操纵的值。
Can you tell me what's wrong with this piece of code?
//bmp is a bitmap of already present image
int width=bmp.getWidth();
int height=bmp.getHeight();
int rgbval[]=new int[width*height];
bmp.getPixels(rgbval, 0, width, 0, 0, width, height);
rgbval=actual(rgbval);
Bitmap bmp2=bmp.copy(Bitmap.Config.ARGB_8888,true);
bmp2.setPixels(rgbval, 0, width, 0, 0, width, height);
actual
is a function I have created to manipulate the rgb values of a bmp. By using debug functions of eclipse I have checked that it is working correctly , but when I try to recover rgb values of bmp2 , I don't get the manipulated values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请参阅这个答案: Android 位图 setPixel 无法正常工作? (设置值,然后读取不同的值)
所以,似乎如果你的原始图像没有 Alpha 通道(即 hasAlpha() 返回 false),它会自动将 rgb 值转换为预乘格式。
这就是你所经历的吗? bmp.hasAlpha() 是假的吗?
我刚刚使用默认的 android 图标作为位图测试了您的代码,其中 hasAlpha() 返回 true。输入和输出颜色数组中的 Alpha 通道是相同的。然而,2304 个像素中有 15 个的红色通道偏离一两个。我没有检查蓝色或绿色通道。我认为这是内部舍入错误?我发布了我使用的相当冗长且仅用于测试的代码,以防万一有人有其他需要添加的内容。
这是 LogCat 输出
First of all, see this answer: Android Bitmap setPixel doens't work correctly? (set value, then read a different value)
So, it seems that if your original image does not have an alpha channel (i.e., hasAlpha() returns false), it will automatically convert the rgb values to a premultiplied format.
Is that what you're experiencing? Is bmp.hasAlpha() false?
I just tested your code with the default android icon as the bitmap, with which hasAlpha() returns true. The alpha channels in the input and output color arrays are identical. However, the red channels are off by one or two for 15 out of 2304 pixels. I didn't check the blue or green channels. I assume this is internal rounding error? I'm posting the rather verbose and used-only-for-testing code I used just in case anyone has anything else to add.
and here is the LogCat output
我认为这个问题是由于一些 android bug 造成的。我已经操作了位图的像素,并且需要像素值完全相同,但是当我使用 setPixels 设置位图的完整像素时,它不起作用。我通过使用找到了解决方案,
所以如果我一个一个地设置像素而不是一次全部设置像素,它会以某种方式起作用!
I think the problem is due to some android bug. I have manipulated the pixels of the bitmap and i needed the pixel values to exactly the same , but when i used setPixels to set the complete pixels of the bitmap it did not work. I found the solution by using
So if i set the pixel one by one instead of all at a time somehow it works!