Cannot read JBIG2 image

2025-10-26 02:38:50

1、添加levigo-jbig2-imageio插件:

针对jdk1.7或以上的:

建议组合:PDFbox的2.07版本+levigo-jbig2-imageio的2.0版本。

如:

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox</artifactId>            <version>2.0.7</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox-tools</artifactId>            <version>2.0.7</version>        </dependency>                <!-- https://mvnrepository.com/artifact/com.levigo.jbig2/levigo-jbig2-imageio 1.6.5-->        <dependency>            <groupId>com.levigo.jbig2</groupId>            <artifactId>levigo-jbig2-imageio</artifactId>            <version>2.0</version>        </dependency>

针对jdk1.6的:

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox</artifactId>            <version>2.0.7</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox-tools</artifactId>            <version>2.0.7</version>        </dependency>                <!-- https://mvnrepository.com/artifact/com.levigo.jbig2/levigo-jbig2-imageio 1.6.5-->        <dependency>            <groupId>com.levigo.jbig2</groupId>            <artifactId>levigo-jbig2-imageio</artifactId>            <version>2.0</version>        </dependency>

报错:java.lang.UnsupportedClassVersionError: com/levigo/jbig2/JBIG2ImageReaderSpi : Unsupported major.minor version 51.0 (unable to load class com.levigo.jbig2.JBIG2ImageReaderSpi)

于是将levigo-jbig2-imageio降为1.6.5版本,又报错:

org.apache.pdfbox.contentstream.PDFStreamEngine.operatorException(875) | Cannot read JBIG2 image: jbig2-imageio is not installed

于是怒了,查看PDFbox源码分析,发现jbig2-imageio在jdk1.6时,兼容性问题?????

百度百度.........发现有个JavaX的东东???

然后,发现了rt.jar中有一个javax.imageio.spi包下,有一些类提供的图所示。

再分析发现,ImageIO类中扫描插件注册函数,

  public static void scanForPlugins() {        theRegistry.registerApplicationClasspathSpis();    }

2、经分析测试,在jdk1.6版本下,应将levigo-jbig2-imageio降为1.6.5版本,且在调用PDFRenderer renderer = new PDFRenderer(document);

添加代码:

            ImageIO.scanForPlugins();

3、<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox</artifactId>            <version>2.0.7</version>        </dependency>        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->        <dependency>            <groupId>org.apache.pdfbox</groupId>            <artifactId>pdfbox-tools</artifactId>            <version>2.0.7</version>        </dependency>                <!-- https://mvnrepository.com/artifact/com.levigo.jbig2/levigo-jbig2-imageio 1.6.5-->        <dependency>            <groupId>com.levigo.jbig2</groupId>            <artifactId>levigo-jbig2-imageio</artifactId>            <version>1.6.5</version>        </dependency>        <dependency>            <groupId>org.bouncycastle</groupId>            <artifactId>bcprov-jdk15on</artifactId>            <version>1.54</version>        </dependency>        <dependency>            <groupId>org.bouncycastle</groupId>            <artifactId>bcmail-jdk15on</artifactId>            <version>1.54</version>        </dependency>        <dependency>            <groupId>org.bouncycastle</groupId>            <artifactId>bcpkix-jdk15on</artifactId>            <version>1.54</version>        </dependency>

4、 PDDocument document = null;                boolean failed = false;                File file = new File(pdfPath);                System.out.println("Opening: " + file.getName());                try                {                    new FileOutputStream(new File(path, file.getName() + ".parseerror")).close();                                        document = PDDocument.load(file, (String)null);                    String outputPrefix = path + '/' + file.getName() + "-";                    int numPages = document.getNumberOfPages();                    if (numPages < 1)                    {                        failed = true;                        System.out.println("file " + file.getName() + " has < 1 page");                    }                    else                    {                        new File(path, file.getName() + ".parseerror").delete();                    }                    ImageIO.scanForPlugins();                     PDFRenderer renderer = new PDFRenderer(document);                    for (int i = 0; i < numPages; i++)                    {                        Map map = new HashMap<String, String>();                        String fileName = outputPrefix + (i + 1) + ".png";                        BufferedImage image = renderer.renderImageWithDPI(i, 96); // Windows native DPI                        ImageIO.write(image, "PNG", new File(fileName));            }

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢