简体   繁体   English

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

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

I am using PDFBox 2.0. 我正在使用PDFBox 2.0。 While parsing a PDF document, I also want to get first page as image and store it to hbase for using it in search results(I am going to create a search list page like search page of amazon.com). 在解析PDF文档时,我还希望将第一页作为图像保存到hbase以便在搜索结果中使用(我将创建一个搜索列表页面,如amazon.com的搜索页面)。

HBase accepts byte[] variable to store(index) a value. HBase接受byte []变量来存储(索引)一个值。 I need to convert the image as byte[], then store it to HBase. 我需要将图像转换为byte [],然后将其存储到HBase。 I have implemented image render, but how can I convert it to byte[]? 我已经实现了图像渲染,但是如何将其转换为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();
    } 

If I write ImageIOUtil.writeImage(image , fileName+".jpg" ,300); 如果我写ImageIOUtil.writeImage(image , fileName+".jpg" ,300); above right above document.close(); document.close();上方的右上方document.close(); , program creates a jpg file in project path. ,程序会在项目路径中创建一个jpg文件。 I need to put it in a byte[] array instead of creating a file. 我需要将其放入byte []数组中,而不是创建文件。 Is it possible? 可能吗?

This can be done with ImageIO.write(Image, String, OutputStream) which can write to an arbitrary OutputStream rather than disk. 这可以通过ImageIO.write(Image,String,OutputStream)完成 ,它可以写入任意OutputStream而不是磁盘。 ByteArrayOutputStream can store the output bytes into an array in memory. 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