解决 OpenJDK 8 中 ImageIO.read 错误的方法?

Workaround for ImageIO.read bug in OpenJDK 8?

我正在为项目使用 Java 8(特别是 OpenJDK 8),我需要从 JPG 文件中读取图像。一开始我四处搜索得到了一些类似的问题(例如Read byte array into buffered image WITHOUT ImageIO and Is there a 100% Java alternative to ImageIO for reading JPEG files?),但我的问题是不同的,那里的解决方案不符合我的要求。

原来JPEGImageReaderclass还是不见了。 (参见 openjdk-8: Missing JPEGImageReader functions in libjavajpeg.so)尽管该错误报告是针对 Debian 的,但我使用的是 Kubuntu 14.10,它也受到了影响。

根据报告的最后一条消息,目前似乎没有人在处理此问题...

重现此错误的代码片段是:

// ...
BufferedImage img = null;
try {
    img = ImageIO.read(new File(filename));
} catch (IOException e) {
    throw new RuntimeException(e);
}
// ...

文件的路径是有效的,如果我使用 Java 1.7,它可以正常工作,但是更改为 1.8 会导致 ImageIO.read 调用出现以下异常:

Caught UnsatisfiedLinkError: com.sun.imageio.plugins.jpeg.JPEGImageReader.initReaderIDs(Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)

我想帮助解决这个问题,同时避免:

  1. 对 other/external 库的额外依赖;
  2. 回到 Java 1.7;
  3. 必须从源代码重建;
  4. Oracle 的专有实现 JDK

感谢工作代码片段。

EDIT-1: 将第 4 点添加到列表中。

EDIT-2: 重写了主要部分的一部分并添加了另一个参考。

您可以 install the java advanced imaging libraries,它还提供了 jpeg(和其他编解码器)实现。

是否可以使用 Oracle JDK?

如果是这样,有一些关于 ppa 的信息 here 可以与 ubuntu 一起使用。

根据 Matthias Klose 的 email:

,我相信自 2015 年 3 月 17 日发布以来,此问题已得到修复

Source: openjdk-8 Source-Version: 8u40-b27-1

We believe that the bug you reported is fixed in the latest version of openjdk-8, which is due to be installed in the Debian FTP archive.

A summary of the changes between this version and the previous one is attached.

Thank you for reporting the bug, which will now be closed. If you have further comments please address them to 760926@bugs.debian.org, and the maintainer will reopen the bug report if appropriate.

Debian distribution maintenance software pp. Matthias Klose (supplier of updated openjdk-8 package)

(This message was generated automatically at their request; if you believe that there is a problem with it please contact the archive administrators by mailing ftpmaster@ftp-master.debian.org)

我认为这里的问题在于生成的图像是 JPEG format.Once 我得到它以 PNG 格式生成,问题已解决。我使用 Grabzit API 截取屏幕截图并将其保存为 PNG 格式