Why does loading this jpg using JavaIO give CMMException?

回眸只為那壹抹淺笑 提交于 2019-12-29 07:36:14

问题


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)

回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4470958/why-does-loading-this-jpg-using-javaio-give-cmmexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!