BufferedImage:红色显示为灰色
我正在用java编写一个小程序,它将在图像上绘制一条路径。为此,我有以下代码
while(!path.isEmpty())
{
Position p = path.poll();
image.setRGB(p.getX(),p.getY(),Color.red.getRGB());
}
,其中路径是旧 X 和 Y 坐标的对象队列,图像是标准 BufferedImage(来自 ImageIO.read)。此代码的目的只是在队列中图像的每个像素上绘制一个红色像素。不过,当我将此图像写入文件时,我得到的不是红色,而是灰色。
Color.red.getRGB 的返回值为 0xFFFF0000。当我将像素设置为红色后对其执行 getRGB 时,我返回 0xFF7F7F7F。
我对 Java 比较陌生,不知道为什么会发生这种情况。任何帮助将不胜感激。
如果有区别,则该图像来自 .bmp 文件。
I am writing a small program in java which will draw a path on an image. To do so, I have the following code
while(!path.isEmpty())
{
Position p = path.poll();
image.setRGB(p.getX(),p.getY(),Color.red.getRGB());
}
Where path is a Queue of objects which old the X and Y coordinates and image is a standard BufferedImage (from ImageIO.read). This code is just meant to draw a red pixel on every pixel of the image that does is in the queue. Instead of Red though, when I write this image to file I get a gray color.
The return value of Color.red.getRGB is 0xFFFF0000. When I do a getRGB on the pixel after I set it to red, I get back 0xFF7F7F7F.
I am relatively new to Java and have no idea why this is happening. Any help would be greatly appreciated.
If it makes a difference, the image is from a .bmp file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能使用的是灰度类型的 BufferedImage,或者将这些 sRGB 值映射到灰色的类型。
一般来说,您有两种可能性之一:
由于您是从 .bmp 文件加载图像,因此第二个可能是您的情况。
有关颜色值转换问题的信息,请参阅 此处 和 这里。
一般来说,如果您只想了解 Java 中的图像处理,我建议使用第二个 BufferedImage 构造函数,并以 *TYPE_INT_ARGB* 作为初学者的类型,并从中扩展您的代码。根据我对早期 Java 的记忆,学习图像加载可能有点棘手:)。
另外,您可能还想阅读官方 Java2D 教程。这是对该主题的很好的介绍。
It could be that you're using a BufferedImage that's a grayscale type, or a type that maps those sRGB values to a gray color.
Generally, you have one of two possibilities:
Since you're loading the image from a .bmp file, the 2nd one is probably your case.
For info about color value conversion issues, see here and here.
Generally, if you just want to learn about image handling in Java, I would suggest to use the second BufferedImage constructor with *TYPE_INT_ARGB* as the type for starters, and expand your code from that. From what I remember of my early Java days, learning image loading can be a bit tricky :).
Also, you might want to read the official Java2D tutorial. It's a quite good introduction to the topic.