开发者

BufferedImage: Red is being displayed as Grey

开发者 https://www.devze.com 2023-04-11 11:57 出处:网络
I am writing a small program in java which will draw a path on an image. To do so, I have the following code

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.

BufferedImage: Red is being displayed as Grey

The return value of Color.red.getRGB is 0xFFFF0000. When I do a getRGB on the pixel after I set it 开发者_如何学Pythonto 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.


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:

  1. If you're initializing BufferedImage explicitly, check out either the imageType or the ColorModel arguments, depending on the constructor you're using.
  2. If you're initializing the image object through some factory method or otherwise indirectly, check out the value of the getType() method. The values are listed in the JavaDoc . If that's a "correct" value (one of the RGB types), check out the return of getColorModel().

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号