3

here's my code:

byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
WritableRaster raster = newImage.getRaster();
raster.setDataElements(0, 0, image.getWidth(), image.getHeight(), pixels);
newImage.setData(raster);
ImageIO.write(newImage, "jpg", new File("newimage.jpg"));

This code looks right to me and should do what I want. It gets the image's pixel data, then uses it to create a new image which should look exactly the same as the original image. However, the image that is saved has different colors than the original. Why?

Eventually, I'll need to manipulate the pixel bytes but for now, I don't know why it's giving me a different image.

4

1 回答 1

2

这篇文章可能对使用红色蒙版创建的 java 缓冲图像有所帮助

这似乎是 ImageIO 的一个常见问题,因此最好使用 Toolkit。

于 2013-10-05T06:29:22.323 回答