基於雙注意模型的影象描述生成方法研究論文筆記
阿新 • • 發佈:2022-03-27
package com.....util; import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList;import java.util.List; @Slf4j public class PdfUtil { //經過測試,dpi為96,100,105,120,150,200中,105顯示效果較為清晰,體積穩定,dpi越高圖片體積越大,一般電腦顯示解析度為96 public static final float DEFAULT_DPI = 105; //預設轉換的圖片格式為jpg public static final String DEFAULT_FORMAT = "jpg"; public static void main(String[] args) throws IOException { PdfUtil.pdfToImage("D:\\test.pdf", "D:\\test"); } public static void pdfToImage(String pdfPath, String imgPath) { try { //影象合併使用引數 // 總寬度 int width = 0; // 儲存一張圖片中的RGB資料 int[] singleImgRGB; int shiftHeight = 0; //儲存每張圖片的畫素值 BufferedImage imageResult = null; //利用PdfBox生成影象 PDDocument pdDocument = PDDocument.load(new File(pdfPath)); PDFRenderer renderer = new PDFRenderer(pdDocument); //迴圈每個頁碼 for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) { BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); //計算高度和偏移量 //使用第一張圖片寬度; width = imageWidth; //儲存每頁圖片的畫素值 imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB); //這裡有高度,可以將imageHeight*len,我這裡值提取一頁所以不需要 singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width); // 寫入流中 imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 寫圖片 File f = new File(imgPath, "test_" + i + ".png"); if (!f.exists()) { f.createNewFile(); } ImageIO.write(imageResult, DEFAULT_FORMAT, f); } pdDocument.close(); } catch (Exception e) { e.printStackTrace(); } //OVER } public static List<byte[]> pdfToImage(String pdfPath) { List<byte[]> result = new ArrayList<>(); try { //影象合併使用引數 // 總寬度 int width = 0; // 儲存一張圖片中的RGB資料 int[] singleImgRGB; int shiftHeight = 0; //儲存每張圖片的畫素值 BufferedImage imageResult = null; //利用PdfBox生成影象 PDDocument pdDocument = PDDocument.load(new File(pdfPath)); PDFRenderer renderer = new PDFRenderer(pdDocument); //迴圈每個頁碼 for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) { BufferedImage image = renderer.renderImageWithDPI(i, DEFAULT_DPI, ImageType.RGB); int imageHeight = image.getHeight(); int imageWidth = image.getWidth(); //計算高度和偏移量 //使用第一張圖片寬度; width = imageWidth; //儲存每頁圖片的畫素值 imageResult = new BufferedImage(width, imageHeight, BufferedImage.TYPE_INT_RGB); //這裡有高度,可以將imageHeight*len,我這裡值提取一頁所以不需要 singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width); // 寫入流中 imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); // 寫圖片 ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(imageResult, DEFAULT_FORMAT, out); result.add(out.toByteArray()); } pdDocument.close(); } catch (Exception e) { e.printStackTrace(); } return result; } }