0

我想将每个 pdf 页面转换为图像(如屏幕截图),然后将该图像上传到存储服务。

  private void getImageBytes(PDDocument document, int pageIndex, int dpi, ConcurrentHashMap<String, byte[]> imgsToUpload, String imgKey) throws IOException {


    PDFRenderer pdfRenderer = new PDFRenderer(document);
    BufferedImage bim = pdfRenderer.renderImageWithDPI(pageIndex, dpi, ImageType.RGB);
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(bim, "png", os); // import javax.imageio.ImageIO;
    os.flush();
    os.close();
    imgsToUpload.put(imgKey, os.toByteArray());
  }

我确实jbig2-imageio通过将此片段添加到 pom.xml 来导入

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>jbig2-imageio</artifactId>
            <version>3.0.3</version>
        </dependency>

但生成的图像仍然是空白的。并记录此错误:Cannot read JPEG2000 image: Java Advanced Imaging (JAI) Image I/O Tools are not installed

那么,我错过了什么?,我认为添加依赖项可以解决该错误。

我应该使用 pdfbox-tools.imageIo 而不是 java.imageio

PS:我是Java新手,所以我可能是配置问题??

4

1 回答 1

1

您需要在 pom 文件中添加这些依赖项才能解决问题。

<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>

https://pdfbox.apache.org/2.0/dependencies.html#optional-components

于 2021-04-20T12:32:36.753 回答