只有ImageIO.read()和ImageIO.write()后,jpeg图像颜色才会大幅改变 [英] jpeg image color gets drastically changed after just ImageIO.read() and ImageIO.write()

查看:2314
本文介绍了只有ImageIO.read()和ImageIO.write()后,jpeg图像颜色才会大幅改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在javax.imageio.ImageIO中使用ImageIO.read()和ImageIO.write()方法来读取和写入图像,我发现一些图像的颜色变得奇怪。

I have been using ImageIO.read() and ImageIO.write() methods in javax.imageio.ImageIO for reading and writing images, and I found that some images' color gets changed weirdly.

即使我改变我的代码除了只读取图像并编写它们(使用jpeg,png,gif),所有这些新图像都有相同的问题。

Even if I change my code to do nothing but just read images and write them (with jpeg, png, gif), all those new images have the same issue.

我是否需要在ImageIO.read/write方法之前/之后添加任何其他代码?

Do I need to add any other code before/after ImageIO.read/write methods?

这是我使用的代码:

    File f = new File("obw.jpg");
    BufferedImage bi = ImageIO.read(f);
    FileOutputStream fos2 = new FileOutputStream("obw2.jpg");
    ImageIO.write(bi, "JPG", fos2);
    FileOutputStream fos3 = new FileOutputStream("obw3.gif");
    ImageIO.write(bi, "GIF", fos3);
    FileOutputStream fos4 = new FileOutputStream("obw4.png");
    ImageIO.write(bi, "PNG", fos4);

我的环境:

    java version "1.6.0_35"
    MacOSX 10.8.2

原始图片:

Original Image:

读写后的图像之一:

推荐答案

您的问题是ImageIO误将JPEG中的YCbCr数据误解为RBG数据。相关的Java错误是 4712797 4776576 ,Oracle错误地声称已在Java 1.4中修复,但实际上仍然困扰着一些Java 5,6和7 JVM。

Your problem is that ImageIO is misinterpreting the YCbCr data in your JPEG as RBG data. The relevant Java bugs are 4712797 and 4776576, which Oracle wrongly claims were fixed in Java 1.4, but in reality still afflict some Java 5, 6, and 7 JVMs.

在我工作的项目中,我们通过加载一个特殊构造的使用单个黑色像素测试JPEG,以查看ImageIO是否正确加载它。如果像素显示为绿色,则ImageIO会错误解释图像数据,因此当我们稍后加载JPEG并检测导致问题的JPEG类型时,我们也会应用颜色校正。 (在JVM中触发问题的JPEG类型具有特定类型的子采样而没有JFIF标记。)

In a project I work on, we've dealt with this problem by loading a specially-constructed test JPEG with a single black pixel to see if ImageIO loads it properly. If the pixel comes up green, then ImageIO is misinterpreting the image data, so when we load JPEGs later and we detect the sort of JPEG which causes the problem, we also apply a color correction. (The kind of JPEG which triggers the problem in JVMs which exhibit it has a particular kind of subsampling and no JFIF marker.)

这里有一些 LGPLv2许可代码有问题。当世界其他地方设法正确加载JPEG时,需要像这样的代码解决十几年前的错误是我希望Java在火灾中死亡的原因之一。

Here's some LGPLv2-licensed code which deals with the problem. The need for code like this to work around dozen-year-old bugs when the whole rest of the world manages to load JPEGs properly is one of the reasons I want Java to die in a fire.

这篇关于只有ImageIO.read()和ImageIO.write()后,jpeg图像颜色才会大幅改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆