将BufferedImage栅格数据转换为BufferedImage

3

这是我的代码:

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"));

这段代码看起来没问题,应该能实现我的需求。它获取了图片的像素数据,然后使用它创建了一个新的图片,应该与原始图片完全相同。然而,保存的图像颜色与原始图像不同。为什么?

最终,我需要操作像素字节,但目前我不知道为什么它会给我一个不同的图像。


你能否提供一个(最好是小的)原始输入和输出图像的链接? - Harald K
1个回答

2

可能是相关的,也可能完全无关。无论如何,我并不建议使用工具包作为解决方法。那个特定的错误在后来的JRE中已经修复了。 - Harald K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接