我想将每个 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新手,所以我可能是配置问题??