Java 1.5.0_16 保存 jpg 图像时颜色损坏

发布于 2024-08-31 05:04:27 字数 466 浏览 7 评论 0原文

我有一个从磁盘加载的图像(存储为 BufferedImage ),它可以在 JPanel 上正确显示,但是当我尝试使用下面的命令重新保存该图像时,图像以微红色调保存。

ImageIO.write(image, "jpg", fileName);

笔记! image 是一个 BufferedImagefileName 是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沩ん囻菔务 2024-09-07 05:04:27

好的,解决了我的问题,似乎由于某种原因我需要将图像转换为 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.

梦初启 2024-09-07 05:04:27

我首先会调查是 BifferedImage 颜色模型有问题还是 jpeg 编码有问题。您可以尝试更改图像类型(构造函数),看看是否会产生差异,并直接使用 JPEGCodec 来保存 jpeg。

例如

 BufferedImage bufferedImage = ...;  // your image
 out = new FileOutputStream ( filename );
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bufferedImage );
 encoder.setJPEGEncodeParam ( param );
 encoder.encode ( bufferedImage );
 out.close();

编辑:更改文本,这是您要更改的图像类型。请参阅构造函数的链接。

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.

 BufferedImage bufferedImage = ...;  // your image
 out = new FileOutputStream ( filename );
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bufferedImage );
 encoder.setJPEGEncodeParam ( param );
 encoder.encode ( bufferedImage );
 out.close();

EDIT: changed text, it's the image type you want to change. See the link to the constructor.

萧瑟寒风 2024-09-07 05:04:27

另一种方法是在 TYPE_INT_ARGB 缓冲区,具有带 alpha 的 DirectColorModel,如下所述和建议此处

private BufferedImage process(BufferedImage old) {
    int w = old.getWidth();
    int h = old.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(old, 0, 0, null);
    g2d.dispose();
    return img;
}

Another approach is to render the image in a TYPE_INT_ARGB buffer, has a DirectColorModel with alpha, as outlined below and suggested here.

private BufferedImage process(BufferedImage old) {
    int w = old.getWidth();
    int h = old.getHeight();
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage(old, 0, 0, null);
    g2d.dispose();
    return img;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文