java pdf轉一張圖片

maven依賴

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>fontbox</artifactId>
  <version>2.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <version>2.0.15</version>
</dependency>



/**
     * @Description pdf轉成一張圖片
     * @created 2019年4月19日 下午1:54:13
     * @param 
**/
//bytes==>網絡字節數組
    public static void pdf2multiImage(byte[] bytes, ServletOutputStream stream) {
        try {

            PDDocument pdf = PDDocument.load(bytes);
            int actSize  = pdf.getNumberOfPages();
            List<BufferedImage> piclist = new ArrayList<BufferedImage>();
            for (int i = 0; i < actSize; i++) {
                BufferedImage  image = new PDFRenderer(pdf).renderImageWithDPI(i,130, ImageType.RGB);
                piclist.add(image);
            }
            yPic(piclist, stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

/**
     * 將寬度相同的圖片,豎向追加在一起 ##注意:寬度必須相同
     * @param piclist  文件流數組
     * @param outPath  輸出路徑*/


    public static void yPic(List<BufferedImage> piclist, ServletOutputStream outPath) {// 縱向處理圖片
        if (piclist == null || piclist.size() <= 0) {
            System.out.println("圖片數組爲空!");
            return;
        }
        try {
            int height = 0, // 總高度
                    width = 0, // 總寬度
                    _height = 0, // 臨時的高度 , 或保存偏移高度
                    __height = 0, // 臨時的高度,主要保存每個高度
                    picNum = piclist.size();// 圖片的數量
            int[] heightArray = new int[picNum]; // 保存每個文件的高度
            BufferedImage buffer = null; // 保存圖片流
            List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的圖片的RGB
            int[] _imgRGB; // 保存一張圖片中的RGB數據
            for (int i = 0; i < picNum; i++) {
                buffer = piclist.get(i);
                heightArray[i] = _height = buffer.getHeight();// 圖片高度
                if (i == 0) {
                    width = buffer.getWidth();// 圖片寬度
                }
                height += _height; // 獲取總高度
                _imgRGB = new int[width * _height];// 從圖片中讀取RGB
                _imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
                imgRGB.add(_imgRGB);
            }
            _height = 0; // 設置偏移高度爲0
            // 生成新圖片
            BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < picNum; i++) {
                __height = heightArray[i];
                if (i != 0) _height += __height; // 計算偏移高度
                imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 寫入流中
            }
            ImageIO.write(imageResult, "jpg", outPath);// 寫圖片
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                outPath.flush();
                outPath.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章