繁体   English   中英

使用PDFBox将图像转换为byte []

[英]Converting an image to byte[] by using PDFBox

我正在使用PDFBox 2.0。 在解析PDF文档时,我还希望将第一页作为图像保存到hbase以便在搜索结果中使用(我将创建一个搜索列表页面,如amazon.com的搜索页面)。

HBase接受byte []变量来存储(索引)一个值。 我需要将图像转换为byte [],然后将其存储到HBase。 我已经实现了图像渲染,但是如何将其转换为byte []?

        PDDocument document = PDDocument.load(file, "");
        BufferedImage image = null;
        try {
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            if (document.isEncrypted()) {
                try {
                    System.out.println("Trying to decrypt...);
                    document.setAllSecurityToBeRemoved(true);
                    System.out.println("The file has been decrypted in .");
                }
                catch (Exception e) {
                    throw new Exception("cannot be decrypted. ", e);
                }
            }
            PDPage firstPage = (PDPage) document.getDocumentCatalog().getPages().get(0);
            pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
               // 0 means first page.

            image = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);                  
            document.close();

    } catch (Exception e) {
            e.printStackTrace();
    } 

如果我写ImageIOUtil.writeImage(image , fileName+".jpg" ,300); document.close();上方的右上方document.close(); ,程序会在项目路径中创建一个jpg文件。 我需要将其放入byte []数组中,而不是创建文件。 可能吗?

这可以通过ImageIO.write(Image,String,OutputStream)完成 ,它可以写入任意OutputStream而不是磁盘。 ByteArrayOutputStream可以将输出字节存储到内存中的数组中。

import java.io.ByteArrayOutputStream;
...
// example image
BufferedImage image = new BufferedImage(4, 3, BufferedImage.TYPE_INT_ARGB);

// to array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", bos);
byte [] output = bos.toByteArray();
System.out.println(Arrays.toString(output));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM