简体   繁体   English

Java BufferedImage.setRGB在类型13的图像上给出灰度?

[英]Java BufferedImage.setRGB gives grey scale on image of type 13?

I have some manipulation to do on images and I want to show specific parts of the image in a specific color. 我需要对图像进行一些操作,并且希望以特定的颜色显示图像的特定部分。 However, on some image, the color is display in grey. 但是,在某些图像上,颜色显示为灰色。

For exemple, if I write : 例如,如果我写:

BufferedImage baseImage = javax.imageio.ImageIO.read(new File(fileName));
System.out.println(baseImage.getType());
System.out.println(baseImage.getRGB(0, 0));
baseImage.setRGB(0, 0, Color.BLUE.getRGB());
System.out.println(baseImage.getRGB(0, 0));

And that my file is a single pixel in red. 我的文件是红色的单个像素。 The output is : 输出为:

13
-65536
-16777216

If I save the image, I can see a black pixel. 如果保存图像,则可以看到黑色像素。

I tried on an image of the moon which type is 5 instead of 13 in my last example and it works fine. 我在最后一个示例中尝试使用类型为5而不是13的月亮图像,并且效果很好。

Am I doing something wrong ? 难道我做错了什么 ?

The buffered image can be of different types as you found out yourself: http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html It depends on the file that is read. 您发现自己时,缓冲的图像可以具有不同的类型: http : //docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html取决于读取的文件。

If you require that your buffered image is of a certain type, you need to create the image manually and draw the loaded image into it. 如果您需要缓冲的图像是某种类型的图像,则需要手动创建图像并将绘制的图像绘制到其中。

Eg: 例如:

BufferedImage loadedImage = ImageIO.read(...);

BufferedImage rgbImage = new BufferedImage(loadedImage.getWidth(), loadedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
rgbImage.getGraphics().drawImage(loadedImage, 0, 0, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM