如何使用像素RGB值0到1在Java中编写PNG文件? [英] How to write PNG files in java using pixel RGB values 0 to 1?
问题描述
我正在用Java编写光线跟踪器,并且试图弄清楚如何将生成的图像写入PNG文件.到目前为止,我发现的所有示例都演示了如何使用BufferedImage创建PNG,但是它们都使用RGB值0至255.在我的代码中,我表示0至1之间的每个像素颜色值,例如洋红色为(1 ,0,1).我该如何编写具有此类值的PNG?
I am writing a ray tracer in java and I am trying to figure out how to write my generated image to a PNG file. So far, all the examples I have found demonstrate the use of BufferedImage to create a PNG, but they all use RGB values 0 to 255. In my code I represent each pixel colour value between 0 and 1, so for example magenta is (1, 0, 1). How can I go about writing a PNG with such values?
谢谢
推荐答案
如果将0到1之间的值乘以255,则会得到0到255之间的数字.
If you multiply your value between 0 and 1 with 255, you'll get a number between 0 and 255.
注意:使用ImageIO API将BufferedImage
写入PNG文件非常容易,它只是一行代码:
Note: Writing a BufferedImage
to a PNG file is very easy with the ImageIO API, it's just one line of code:
import javax.imageio.ImageIO;
// ...
BufferedImage image = ...;
ImageIO.write(image, "png", new File("output.png"));
这篇关于如何使用像素RGB值0到1在Java中编写PNG文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!