ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PDF转图片,拼接图片

2021-12-22 01:04:41  阅读:206  来源: 互联网

标签:img2 java String BufferedImage 拼接 import PDF img1 图片


一、导入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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有