java Buffered Image : Detecting black pixels

两盒软妹~` 提交于 2019-12-22 04:17:11

问题


I have this simple code to go through a 24bit color windows bmp file

BufferedImage mapa = BMPDecoder.read(new File("maps/map.bmp"));

final int xmin = mapa.getMinX();
final int ymin = mapa.getMinY();

final int ymax = ymin + mapa.getHeight();
final int xmax = xmin + mapa.getWidth();


for (int i = xmin;i<xmax;i++)
{
   for (int j = ymin;j<ymax;j++)
   {

    int pixel = mapa.getRGB(i, j);

    if (pixel == 0)
    {
        System.out.println("black at "+i+","+j);
    }
   }
}

However, when testing on a completely black image, I get this value at pixel : -16777216.

I was hoping to get a 0x0.

How can I test for black pixels (or any other color for that reason) ?

update

Im testing against ((pixel & 0xff) == 0). Is this right? Thanks in advance.


回答1:


-16777216 is 0xFF000000 in hexadecimal, corresponding to opaque black.

Addendum: Looking at your update, I'd think you want ((pixel & 0x00FFFFFF) == 0) as your predicate.



来源:https://stackoverflow.com/questions/3539670/java-buffered-image-detecting-black-pixels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!