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;