我尝试从 rgb 值创建图像,这是我的代码:
try {
BufferedImage img = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
Color clc = new Color(40, 21, 60);
int rgb = clc.getRGB();
img.setRGB(i, j, rgb);
}
}
// retrieve image
File outputfile = new File("D:\\saved.jpg");
ImageIO.write(img, "jpg", outputfile);
} catch (IOException e) {
}
我创建彩色图像红色 = 40,绿色 = 21,蓝色 = 60 创建图像后,我尝试再次获取像素。但是当我创建时输出像素与 rgb 值不同。这是我获取 rgb 值的代码:
public static void main(String[] args){
try {
BufferedImage bf = ImageIO.read(new File("D:\\saved.jpg"));
RGB(bf);
} catch (IOException ex) {
Logger.getLogger(ektract.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void RGB(BufferedImage end)
{
System.out.println("GREEN");
System.out.println("====");
for (int y = 0; y < end.getHeight(); y++)
{
for (int x = 0; x < end.getWidth(); x++)
{
int rgb = end.getRGB(x, y);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >>8 ) & 0x000000FF;
int blue = (rgb) & 0x000000FF;
System.out.print(red+" "+green+" "+blue+" ");
}
System.out.println();
}
}
这个输出:
绿色 ==== 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59 39 21 59
请你帮帮我。谢谢