Java 1.5.0_16 保存 jpg 图像时颜色损坏
我有一个从磁盘加载的图像(存储为 BufferedImage ),它可以在 JPanel 上正确显示,但是当我尝试使用下面的命令重新保存该图像时,图像以微红色调保存。
ImageIO.write(image, "jpg", fileName);
笔记! image 是一个 BufferedImage
,fileName
是一个 File
对象,指向将要保存的文件名,以“.jpg”。
我读过早期 JDK 中的 ImageIO
方法存在问题,但据我所知,我没有使用其中一个版本。我正在寻找一种在不更新 JDK 的情况下解决这个问题的方法,但是话虽如此,我仍然想知道这个问题是在哪个 JDK 中解决的(如果它确实仍然是我正在使用的 JDK 的一个错误) )。
谢谢。
i have a loaded image from disk (stored as a BufferedImage
), which i display correctly on a JPanel
but when i try to re-save this image using the command below, the image is saved in a reddish hue.
ImageIO.write(image, "jpg", fileName);
Note! image is a BufferedImage
and fileName
is a File
object pointing to the filename that will be saved which end in ".jpg
".
I have read that there were problems with ImageIO
methods in earlier JDKs but i'm not on one of those versions as far as i could find. What i am looking for is a way to fix this issue without updating the JDK, however having said that i would still like to know in what JDK this issue was fixed in (if it indeed is still a bug with the JDK i'm using).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,解决了我的问题,似乎由于某种原因我需要将图像转换为 BufferedImage.TYPE_INT_RGB 。我认为 Alpha 通道在某些层可能无法正确处理。
Ok, solved my problem, it seems that i need to convert the image to BufferedImage.TYPE_INT_RGB for some reason. I think the alpha channels might not be handled correctly at some layer.
我首先会调查是 BifferedImage 颜色模型有问题还是 jpeg 编码有问题。您可以尝试更改图像类型(构造函数),看看是否会产生差异,并直接使用 JPEGCodec 来保存 jpeg。
例如
编辑:更改文本,这是您要更改的图像类型。请参阅构造函数的链接。
I would first start by investigating if it is the BifferedImage color model that is the problem or the jpeg encoding. You could try changing the image type (3rd argument in the constructor), to see if that produces a difference, and also use the JPEGCodec directly to save the jpeg.
E.g.
EDIT: changed text, it's the image type you want to change. See the link to the constructor.
另一种方法是在
TYPE_INT_ARGB
缓冲区,具有带 alpha 的 DirectColorModel,如下所述和建议此处。Another approach is to render the image in a
TYPE_INT_ARGB
buffer, has a DirectColorModel with alpha, as outlined below and suggested here.