找到你要的答案

Q:javax.imageio.IIOException: Invalid icc profile: duplicate sequence numbers

Q:javax.imageio.iioexception:无效的ICC Profile:重复序列号

I'm trying to read a JPEG file, but when I try this:

    InputStream input = new URL("http://s4.postimg.org/h1c72546l/invalid_icc_profile.jpg").openStream();
    try {
        ImageIO.read(input);
        System.out.println("Image loaded.");
    } catch (Throwable e) {
        e.printStackTrace();            
    } finally {
        input.close();
    }   

I receive the following exception:

javax.imageio.IIOException: Invalid icc profile: duplicate sequence numbers
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:604)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:342)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:476)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:597)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1054)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1034)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)

This exception occurs only with this image: http://s4.postimg.org/h1c72546l/invalid_icc_profile.jpg

I tested both the JDK 7 and 8, both have the error. Do you have any suggestions on how I can read the image? It's possible to adjust the icc profile?

我想读一个JPEG文件,但是当我尝试这个:

    InputStream input = new URL("http://s4.postimg.org/h1c72546l/invalid_icc_profile.jpg").openStream();
    try {
        ImageIO.read(input);
        System.out.println("Image loaded.");
    } catch (Throwable e) {
        e.printStackTrace();            
    } finally {
        input.close();
    }   

我收到以下异常:

javax.imageio.iioexception:无效的ICC Profile:重复序列号
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:604)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:342)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:476)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:597)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1054)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1034)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)

这个异常只发生与这一形象:http://s4.postimg.org/h1c72546l/invalid_icc_profile.jpg

I tested both the JDK 7 and 8, both have the error. Do you have any suggestions on how I can read the image? It's possible to adjust the icc profile?

answer1: 回答1:

As the error message suggests, the ICC_PROFILE segments in your JPEG file contains duplicate sequence numbers. This means that the software that wrote it, is broken in some way. However, the image data should still be readable, even though the colors might not look exactly as they should.

Because JFIF (the JPEG File Interchange Format) has limited segment size, large ICC profiles must be split over multiple segments. The specification says that the ICC profile should be stored in multiple APP2 segments, and that each segment must start with the null-terminated string ICC_PROFILE, followed by a single unsigned byte sequence number and a total count.

My TwelveMonkeys ImageIO JPEG plugin is able to read your image, even if the ICC profile segments are broken. It will issue a warning (if you have registered an IIOReadWarningListener) telling you that the ICC profile will be ignored. I have tested, and it works fine with your JPEG file. You shouldn't need to change any code, just add the required dependencies (from the link above) and it should work.

You can download the TwelveMonkeys ImageIO 3.2.1 plugins from the Maven repository.


You could probably also fix the indexes/counts of the profile with a hex editor, if you know what you are doing. :-) But from experience, the profiles of such images may not match the color data of the image anyway, so you might be out of luck.

作为错误信息提示,你的JPEG文件的icc_profile段包含重复的序列号。这意味着,写它的软件,在某种程度上被打破。然而,图像数据仍然是可读的,即使颜色可能看起来不完全一样,他们应该。

因为JFIF(JPEG文件交换格式)具有有限的市场规模大的ICC配置文件必须被拆分为多个部分。规范指出,ICC配置文件应该存放在多个段,每个段2,必须以零结尾的字符串icc_profile,紧随其后的是一个无符号的字节序列数和总计数。

我twelvemonkeys ImageIO JPEG插件能够阅读你的图像,即使ICC Profile段破碎。它会发出警告(如果你已经注册了一个iioreadwarninglistener)告诉你,ICC配置文件将被忽略。我已经测试过了,和它的作品符合您的JPEG文件。您不需要更改任何代码,只需添加所需的依赖关系(从上面的链接),它应该工作。

你可以从Maven库下载twelvemonkeys ImageIO 3.2.1插件。


如果你知道你在做什么,你也可以用十六进制编辑器修复配置文件的索引/计数。-但是)从经验来看,这些图像的配置文件可能不匹配的图像的颜色数据无论如何,所以你可能是运气不好。

java  jpeg  javax.imageio  image-loading