我有以下问题: 我在java中使用图像。我设置了颜色像素,然后我保存图像。但是,如果我将此图像加载到程序。像素有不同的颜色! 代码:
BufferedImage img = loadImage();
.
.
//new image for drawing
BufferedImage newImg = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) newImg.getGraphics();
//get pixel color
int pixel = img.getRGB(0, 0);
//parsing color
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
//real drawing
g2.setColor(new Color(red, green, blue));
g2.drawLine(0, 0, 0, 0); // COLOR IS R: 55, G: 54 B: 53
//saving
ImageIO.write(newImg, "jpg", outputfile);
在此之后,我运行另一个可以读取像素颜色的程序。
命令相同..
如果我检查新图像的颜色,则rgb为:R 52, G: 48 B: 81
。
我设置R: 55, G: 53, B:53
及其R 52, G: 48 B: 81
哪里可能有问题?
感谢您的建议。
答案 0 :(得分:1)
这是因为Jpeg 类型
Jpeg 值不会离散保存,但Jpeg会将图片的RGB值保存为函数。
而不是使用jpeg使用png格式,并看到setRGB()完全正常工作。
所以请将您的代码替换为
ImageIO.write(image, "png", outputfile);