getpixels java,Android Java Bitmap.getPixels返回仅用-1作为值填充的数组

博主在尝试分析Bitmap图像时遇到问题,发现获取的像素值全为-1。问题可能在于4字节颜色码中包含透明度信息,而Bitmap本身不包含透明度。解决方案是忽略透明度字节,通过取像素值与0x00FFFFFF进行位运算来获取实际颜色。
摘要由CSDN通过智能技术生成

I'm having a problem, where I basically want to analyse a Bitmap to find the lowest pixel that is not white on the bitmap.

That for I wanted to to loop through every row of pixels of the bitmap. See the code:

Bitmap bmp = BitmapFactory.decodeFile(fileName);

for(int i = 0; i < bmp.getHeight(); i++){

int[] pixels = new int[bmp.getWidth()];

bmp.getPixels(pixels, 0, bmp.getWidth(), 0, i, bmp.getWidth(), 1);

//Now when a look at the pixels array at this point all the values are set to -1 everytime

}

The fileName is valid and the bitmap is loaded, I have double-checked that. Also the pixels cannot all have the same color, because when I look at the file in an image-Viewer it looks like a normal image.

I'm really bored of this problem and I was searching for a few hours and did not find anything useful.

I think it may have something to do with the third parameter, the stride, I do still not understand fully what that parameter is for.

Thanks in advance

解决方案

It's also possible you're getting the color value with an 4 byte color code, instead of 3 byte color code, where most significant byte is used for transparency. There is no transparency in a bitmap so that byte can be ignored. In other words, you're getting 0xFF000000 for black, which is -16777216 if you're using int(which is a signed 32 bits, or four bytes, and so the leading bit is 1 making it negative) and 0xFFFFFFFF for white, which is -1 if you're using int.

I have found this happens in android when a bitmap has been used for an android View. It does not happen in my experience when you get a bitmap from a camera capture or an image file.

The solution I used was to mask out the transparency byte so none of my color codes are recognized as negative:

int color = pixels[i] & 0x00FFFFFF;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值