java缓冲图像:检测黑色像素
我有这个简单的代码来检查 24 位彩色 Windows bmp 文件
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);
}
}
}
但是,在全黑图像上测试时,我在像素处获得此值:-16777216
。
我希望得到 0x0。
如何测试黑色像素(或因此原因的任何其他颜色)?
更新
我正在针对 ((pixel & 0xff) == 0)
进行测试。这是对的吗? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-16777216
是十六进制的0xFF000000
,对应不透明黑色。附录:查看您的更新,我认为您希望
((pixel & 0x00FFFFFF) == 0)
作为谓词。-16777216
is0xFF000000
in hexadecimal, corresponding to opaque black.Addendum: Looking at your update, I'd think you want
((pixel & 0x00FFFFFF) == 0)
as your predicate.