【发布时间】:2015-04-25 17:35:52
【问题描述】:
我有以下问题: 我在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
哪里有问题?
感谢您的建议。
【问题讨论】:
-
JPG 是一种有损编码。您正在设置图像的单个像素,JPEG 压缩可能会更改其值。
-
@JBNizet:它能以某种方式解决吗?禁用压缩?
-
首先通过设置图像的所有像素或使用无损编码(如 PNG)来确认这是问题所在。
标签: java rgb bufferedimage