为什么使用 JavaIO 加载此 jpg 会给出 CMMException?

发布于 2024-10-08 06:38:09 字数 864 浏览 8 评论 0原文

ImageIO.read(imagePath) 与此文件给出了 CMMException,为什么 Java 不能处理这个看似有效的文件 http://www.jthink.net/jaikoz/scratch/front.jpg

java.awt.color.CMMException: Invalid image format
    at sun.awt.color.CMM.checkStatus(Unknown Source)
    at sun.awt.color.ICC_Transform.<init>(Unknown Source)
    at java.awt.image.ColorConvertOp.filter(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)

ImageIO.read(imagePath) with this file gives a CMMException, why cant Java cope with this seemingly valid file http://www.jthink.net/jaikoz/scratch/front.jpg

java.awt.color.CMMException: Invalid image format
    at sun.awt.color.CMM.checkStatus(Unknown Source)
    at sun.awt.color.ICC_Transform.<init>(Unknown Source)
    at java.awt.image.ColorConvertOp.filter(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

农村范ル 2024-10-15 06:38:10

我想我已经解决了你的问题。我检查了您链接的图像( http://www.jthink.net/jaikoz/scratch /front.jpg )。这是由于 Exif 和 JFIF 标准。

当您执行诸如 ImageIO.read('some file') 之类的操作时,它会调用默认的 sun jpeg 实现 com.sun.imageio.plugins.jpeg.JPEGImageReader。曾经在加载 JFIF 文件时遇到问题BUG 6488904(检查对结尾)。

根据规范,Exif 和
JFIF 要求其各自
应用程序标记段必须是
SOI 之后的第一个(APP1 和 APP0)
,所以实际上不可能
JPEG 文件兼容的规范
符合这两个标准。

虽然很久以前就有报道说

解决方法是使用 JAI 库 (https:/ /jai.dev.java.net/binary-builds.html#Release_builds)。我正在使用Java(无本机加速)版本。

SeekableStream seekableStream =  new FileSeekableStream(new File("front.jpg"));
ParameterBlock pb = new ParameterBlock();
pb.add(seekableStream);

BufferedImage image = JAI.create("jpeg", pb).getAsBufferedImage();

希望这会有所帮助。

I think I got the hang of your problem. I checked the image you linked ( http://www.jthink.net/jaikoz/scratch/front.jpg ). Its due to Exif and JFIF standard.

when you are doing something like ImageIO.read('some file') it invokes the default sun jpeg implementation com.sun.imageio.plugins.jpeg.JPEGImageReader. Which used to have issues loading JFIF files BUG 6488904 (check the comment towards the end).

According to spec, both Exif and
JFIF demands that their respective
application marker segment must be the
first right after SOI (APP1 and APP0)
, so it is actually not possible per
spec for an JPEG file to be compliant
with both standards.

Though it was reported long time back

The workaround is to use JAI library (https://jai.dev.java.net/binary-builds.html#Release_builds). I am using Java (no native acceleration) version.

SeekableStream seekableStream =  new FileSeekableStream(new File("front.jpg"));
ParameterBlock pb = new ParameterBlock();
pb.add(seekableStream);

BufferedImage image = JAI.create("jpeg", pb).getAsBufferedImage();

hope this will help.

×眷恋的温暖 2024-10-15 06:38:10

顺便说一句,这个问题在 JDK8 中已得到修复(请注意 https:// 底部的提交bugs.java.com/bugdatabase/view_bug?bug_id=7064516)。我已经确认 JDK8 的预发布版本 可以正确加载 JDK7 失败的图像如上所述。

BTW, this problem is fixed in JDK8 (notice the commit at the bottom of https://bugs.java.com/bugdatabase/view_bug?bug_id=7064516). I've confirmed that a pre-release build of JDK8 properly loads images that JDK7 fails on as described above.

流云如水 2024-10-15 06:38:10

旧帖子,但供将来参考:

受此问题和此处找到的链接的启发,我为 ImageIO 编写了一个 JPEGImageReader 插件,它支持具有此类“不良”ICC 颜色配置文件的 JPEG 图像(“问题”是中的渲染意图) ICC 配置文件与 Java 的 ColorConvertOp 不兼容)。它是纯 Java,不需要 JAI。源代码可免费获取:

https://github.com/haraldk /TwelveMonkeys/tree/master/imageio/imageio-jpeg

Old post, but for future reference:

Inspired by this question and links found here, I've written a JPEGImageReader plugin for ImageIO that supports JPEG images with these kind of "bad" ICC color profiles (the "issue" is the rendering intent in the ICC profile is incompatible with Java's ColorConvertOp). It's plain Java and does not require JAI. The source code is freely available at:

https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-jpeg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文