我有一个 RGB 值列表。 (r,g,b)->(49,34,3),(1,9,26),(34,4,4)。现在我想用这些创建图像。可以吗?
我希望创建 png 或 jpg 格式的图像。我要创建的图像将在此位置-> E:/RSA 中创建。我了解了一些关于 img.setRGB(x,y,rgb) 的知识。我不知道如何应用它。
int r[ ]={49,1,34};
int g[ ]={34,9,4};
int b[ ]={3,26,4};
BufferedImage img = null;
for(int i=0;i<r.length;i++){
img.setRGB(r,g,b);}}
File imageFile= new File("R:/RSA/newImage.png");
try {
ImageIO.write(img,"png",imageFile);
} catch (IOException e) {
System.out.println("Error");
}
为了更好地理解这个问题,我们来看看 int RGB 是什么。您可以在这里找到说明:在此处输入链接描述
另一个有用的做法是写出颜色然后加载它,如下所示:
Color white = new Color(255, 255, 255); // Color white
int whiteRGBValue = white.getRGB();
System.out.println(whiteRGBValue);
这样你就可以将rgb值放入构建图像的循环中:
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// convert to image
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
img.setRGB(x, y, whiteRGBValue);
}
}