zoukankan      html  css  js  c++  java
  • 把PDF InputStream转JPG图片 并输出byte[] 保存至blob pdf多页时纵向拼接

    
    
    <dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>fontbox</artifactId>
    <version>2.0.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
    <dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
    </dependency>



    public
    static byte[] useStream(InputStream is) throws Exception { try { PDDocument pdf = PDDocument.load(is); int actSize = pdf.getNumberOfPages(); List<BufferedImage> piclist = new ArrayList<BufferedImage>(); for (int i = 0; i < actSize; i++) { BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, 100, ImageType.RGB); piclist.add(image); } byte[] bytes = yPic(piclist); is.close(); return bytes; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同 多页时纵向拼接 * * @param piclist 文件流数组 */ public static byte[] yPic(List<BufferedImage> piclist) {// 纵向处理图片 if (piclist == null || piclist.size() <= 0) { System.out.println("图片数组为空!"); return null; } 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); // 写入流中 } ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean flag = ImageIO.write(imageResult, "jpg", out); byte[] b = out.toByteArray(); File outFile = new File("H:\temp.jpg"); ImageIO.write(imageResult, "jpg", outFile);// 写图片到本地 return b; } catch (Exception e) { e.printStackTrace(); } return null; }
  • 相关阅读:
    创龙TMS320C6748开发板串口和中断学习笔记
    RTL8195AM开发板使用
    CC3100BoosterPack和CC31XXEMUBOOST板子的测试
    利尔达NB-IOT的PSM和eDRX低功耗模式笔记
    【原创】大数据基础之Zookeeper(3)选举算法
    【原创】大数据基础之Zookeeper(2)源代码解析
    【原创】大数据基础之Zookeeper(1)介绍、安装及使用
    【原创】论码农的财富修养
    【原创】大叔案例分享(2)处理大批量数据时如何实现“高效”同时实现“断点续传”功能
    【原创】大数据基础之Spark(1)Spark Submit即Spark任务提交过程
  • 原文地址:https://www.cnblogs.com/pan-my/p/13999531.html
Copyright © 2011-2022 走看看