如何修复 "Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed"

How to fix "Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed"

我正在建立一个 java 项目,我在其中使用 pdfBox 从 PDF 中获取图像。由于我将 tika-app 用于其他功能,因此我决定使用 tika-app-1 中的 pdfBox。20.jar。

我之前尝试过包含 jai-imageio-core-1.3。1.jar,因为 Tika-app 已经与这个 jar 捆绑在一起。我尝试单独使用 tika-app jar。

抛出错误的行

PDXObject object = resources.getXObject(cosName);

错误的日志跟踪:

org.apache.pdfbox.filter.MissingImageReaderException: Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed
    at org.apache.pdfbox.filter.Filter.findImageReader(Filter.java:163)
    at org.apache.pdfbox.filter.JPXFilter.readJPX(JPXFilter.java:115)
    at org.apache.pdfbox.filter.JPXFilter.decode(JPXFilter.java:64)
    at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:77)
    at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:175)
    at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:163)
    at org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:236)
    at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.<init>(PDImageXObject.java:140)
    at org.apache.pdfbox.pdmodel.graphics.PDXObject.createXObject(PDXObject.java:70)
    at org.apache.pdfbox.pdmodel.PDResources.getXObject(PDResources.java:426)

但我很确定我在 tika 中有 jai-imageio-core,当我 运行 代码时它是不可见的。

碰巧它需要一个名为 jai-imageio-jpeg2000 的额外 jar 支持jp2k图片。

实际上,我也偶然发现了这个错误,但这在 PDFBox 文档 here 中有所提及。您需要将以下依赖项添加到 pom.xml :

<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-core</artifactId>
    <version>1.4.0</version>
</dependency>

<dependency>
    <groupId>com.github.jai-imageio</groupId>
    <artifactId>jai-imageio-jpeg2000</artifactId>
    <version>1.3.0</version>
</dependency>

<!-- Optional for you ; just to avoid the same error with JBIG2 images -->
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>jbig2-imageio</artifactId>
    <version>3.0.3</version>
</dependency>

如果您使用的是Gradle:

dependencies {
    implementation 'com.github.jai-imageio:jai-imageio-core:1.4.0'
    implementation 'com.github.jai-imageio:jai-imageio-jpeg2000:1.3.0'

    // Optional for you ; just to avoid the same error with JBIG2 images
    implementation 'org.apache.pdfbox:jbig2-imageio:3.0.3'
}