PDF转图片,拼接图片
作者:互联网
一、导入PDF处理的包
阿里云仓库搜索icepdf-core依赖,找到合适的版本,导入pom.xml文件。
<dependency> <groupId>org.icepdf.os</groupId> <artifactId>icepdf-core</artifactId> <version>6.1.2</version> </dependency>
二、PDF转图片存储
1、读取目标PDF文件;
2、创建Document对象,通过getNumberOfPages方法获取PDF页数;
3、循环创建BufferedImage对象,读取每页PDF,并以图片流的方式输出,将图片写入指定路径。
import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.util.GraphicsRenderingHints; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; public class PDFToImageUtil { /*获取pdf文件名*/ public String getPDFName(String filePath){ File fileOrigin = new File(filePath); String fileName = fileOrigin.getName(); fileName = fileName.substring(0,fileName.lastIndexOf(".")); return fileName; } /*pdf转图片*/ public List<String> Pdf2Img(String filePath,String imagePath) throws Exception { String fileName = getPDFName(filePath); File dir = new File(imagePath); if(!dir.exists()){ boolean b = dir.mkdir(); if(!b){ return null; } } Document document = new Document(); document.setFile(filePath); float scale = 2.5f;//缩放比例 float rotation = 0f;//旋转角度 List<String> filePaths = new ArrayList<String>(); for(int i = 0; i<document.getNumberOfPages();i++){ BufferedImage bufferedImage = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,rotation,scale); try { File file = new File(imagePath+"/"+fileName+new Date().getTime() +".png"); filePaths.add(file.getPath()); ImageIO.write(bufferedImage,"png",file); }catch (IOException e){ e.printStackTrace(); } bufferedImage.flush(); } document.dispose(); return filePaths; } }
4、生成图片后,方法会将图片的全路径存入List并返回。
三、拼接图片
1、获取所有图片路径,循环遍历图片,用BufferedImage对象存储;
2、获取每张图片的RGB,用数组存储;
3、以每张图片的左上角为起始坐标,根据纵向坐标不断累加的原理,将RGB拼接;
4、循环完成后,对最终拼接完成的BufferedImage对象写入指定位置生成拼接后的图片。
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.List; import javax.imageio.ImageIO; public class PDFToMergeImageUtil { public String Pdf2MergeImg(String filePath,String imagePath) throws Exception { PDFToImageUtil pdfToImageUtil = new PDFToImageUtil(); List<String> filePaths = pdfToImageUtil.Pdf2Img(filePath, imagePath); String fileName = pdfToImageUtil.getPDFName(filePath); BufferedImage destImage = null; for(int i =0 ; i < filePaths.size() - 1;i++){ BufferedImage img1 = null; BufferedImage img2 = null; if(i == 0){ img1 = getBufferedImage(filePaths.get(i)); img2 = getBufferedImage(filePaths.get(i+1)); }else{ img1 = destImage; img2 = getBufferedImage(filePaths.get(i+1)); } // 从图片中读取RGB int[] imageArrayOne = new int[img1.getWidth() * img1.getHeight()]; imageArrayOne = img1.getRGB(0, 0, img1.getWidth(), img1.getHeight(), imageArrayOne, 0, img1.getWidth()); // 逐行扫描图像中各个像素的RGB到数组中 int[] imageArrayTwo = new int[img2.getWidth() * img2.getHeight()]; imageArrayTwo = img2.getRGB(0, 0, img2.getWidth(), img2.getHeight(), imageArrayTwo, 0, img2.getWidth()); destImage = new BufferedImage(img1.getWidth(), img1.getHeight()+img2.getHeight(), BufferedImage.TYPE_INT_RGB); destImage.setRGB(0, 0, img1.getWidth(), img1.getHeight(), imageArrayOne, 0, img1.getWidth()); // 设置上半部分或左半部分的RGB destImage.setRGB(0, img1.getHeight(), img2.getWidth(), img2.getHeight(), imageArrayTwo, 0, img2.getWidth()); // 设置下半部分的RGB } File fileAll = new File(imagePath+"/"+fileName+".jpg"); ImageIO.write(destImage,"png",fileAll); destImage.flush(); return fileAll.getPath(); } /** * @param fileUrl * 文件绝对路径或相对路径 * @return 读取到的缓存图像 * @throws IOException * 路径错误或者不存在该文件时抛出IO异常 */ private BufferedImage getBufferedImage(String fileUrl) throws IOException { File f = new File(fileUrl); return ImageIO.read(f); } }
四、测试
import org.jeecg.business.utils.PDFToMergeImageUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class Pdf2Image { @Value(value = "${path.imagePath}") private String imagePath; @Test public void test1() throws Exception { PDFToMergeImageUtil pdf = new PDFToMergeImageUtil(); String listImg = pdf.Pdf2MergeImg("D://java//maven.pdf",imagePath); System.out.println(listImg); } }
五、总结
总的来说就是通过BufferedImage的读写及RGB渲染实现,如果PDF文件的页数较多时,可能会出现堆内存不够的情况。
标签:img2,java,String,BufferedImage,拼接,import,PDF,img1,图片 来源: https://www.cnblogs.com/stupidxixi/p/15717799.html