- 浏览: 135015 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
zheng_zhimeng:
这个版本在linux的版本下有问题,亲们用的没有问题么
文档展示:IcePDF 将PDF转换为图片 -
yuming.xiao:
转换的某些图片,有些模糊。不知道楼主遇到这个问题没有
文档展示:IcePDF 将PDF转换为图片 -
zenghongqing:
您好,请教您一个问题://cell内容字符串总宽度 doub ...
Java POI Excel 行高自适应 -
xiang37:
http://xiva.iteye.com/blog/2066 ...
视频分割项目预研 -
I白I:
怎么还配置数据库了?
视频分割项目预研
上接 文档展示:PDFRender 将PDF转换为图片
http://zhuyufufu.iteye.com/admin/blogs/2012236
本篇文章研究如何利用多线程技术提高PDF转图片的效率(减少用时)
对上一篇的例子加上用时统计:
重写代码为多线程,暂时一页PDF起一个线程
线程代码
主代码:
代码问题有两个:
1. 1页起一个线程肯定浪费了
2. 计时程序出了问题,计不了时了
先解决计时问题
原始的想法有两个:
1. 使用一个计数器,其大小等于线程数,每一线程执行完后就减一,当其为0时执行最后的计时
2. 使用一个布尔数组,其大小等于线程数,每一线程执行完后就置对应的布尔值为已完成,主程序轮询数组,当其全部为已完成时执行最后计时
但是这两种想法怎么看怎么不高端大气上档次,继续查资料,找到两个JDK自带的类 CyclicBarrier与CountDownLatch。这两个类都能实现多线程计时,而CountDownLatch好像更符合我的要求,就采用它了。
改代码
PDF线程修改
主程序修改
测试结果:
对于一个79页的PDF,不开线程用时8秒,开79个线程用时5秒
对于一个634页的PDF,不开线程用时459秒,开不了634个线程,改进线程程序
PDFThred:
主程序:
测试结果:
对于544页的PDF,开不开线程对于时间影响不大,应该在图像处理部分阻塞掉了
在继续定位处理
http://zhuyufufu.iteye.com/admin/blogs/2012236
本篇文章研究如何利用多线程技术提高PDF转图片的效率(减少用时)
对上一篇的例子加上用时统计:
long beginTime = System.nanoTime(); PDFRenderTest.convert(inputPDFPath, outputFDir); long endTime = System.nanoTime(); System.out.println("耗时: " + (endTime - beginTime) / 1000000000 + " 秒" );
重写代码为多线程,暂时一页PDF起一个线程
线程代码
package com.zas.pdfrender.test; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFPage; public class PDFThread implements Runnable{ PDFPage page; int i; String outputFDir; public PDFThread(PDFPage page, int i, String outputFDir) { this.page = page; this.i = i; this.outputFDir = outputFDir; } @Override public void run() { Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); Image img = page.getImage(rect.width, rect.height, // width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics2D g=tag.createGraphics(); //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(img, 0, 0, rect.width, rect.height, null); FileOutputStream out; try { out = new FileOutputStream(outputFDir + i + ".png"); } catch (FileNotFoundException e1) { e1.printStackTrace(); return; } // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag); param2.setQuality(1f, false);// 1f是提高生成的图片质量 encoder.setJPEGEncodeParam(param2); try { encoder.encode(tag); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // JPEG编码 try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
主代码:
package com.zas.pdfrender.test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; public class PDFRenderTest { public static void convert(String inputPDFPath, String outputFDir) throws IOException, FileNotFoundException { //pdf文件存在校验,输出文件夹创建 File file = new File(inputPDFPath); if(!file.exists()){ throw new FileNotFoundException("文件不存在: " + inputPDFPath); } File outputFolder = new File(outputFDir); if(!outputFolder.exists()){ outputFolder.mkdirs(); } //获取PDFFile RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); channel.close(); raf.close(); PDFFile pdffile = new PDFFile(buf); System.out.println("PDF页数: " + pdffile.getNumPages() + " , " + inputPDFPath); //转化处理 for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); PDFThread thread = new PDFThread(page, i, outputFDir); new Thread(thread).start(); } } public static void main(final String[] args) throws FileNotFoundException, IOException { String inputPDFPath = "D:\\pdf\\ppt\\2010110东南大学档案管理系统需求分析说明书正式.pdf"; String outputFDir = "D:\\pdf\\222222222222010110系统需求分析说明书正式\\"; long beginTime = System.nanoTime(); PDFRenderTest.convert(inputPDFPath, outputFDir); long endTime = System.nanoTime(); System.out.println("耗时: " + (endTime - beginTime) / 1000000000 + " 秒" ); } }
代码问题有两个:
1. 1页起一个线程肯定浪费了
2. 计时程序出了问题,计不了时了
先解决计时问题
原始的想法有两个:
1. 使用一个计数器,其大小等于线程数,每一线程执行完后就减一,当其为0时执行最后的计时
2. 使用一个布尔数组,其大小等于线程数,每一线程执行完后就置对应的布尔值为已完成,主程序轮询数组,当其全部为已完成时执行最后计时
但是这两种想法怎么看怎么不高端大气上档次,继续查资料,找到两个JDK自带的类 CyclicBarrier与CountDownLatch。这两个类都能实现多线程计时,而CountDownLatch好像更符合我的要求,就采用它了。
改代码
PDF线程修改
package com.zas.pdfrender.test; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.concurrent.CountDownLatch; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFPage; public class PDFThread implements Runnable{ PDFPage page; int i; String outputFDir; CountDownLatch latch; public PDFThread(PDFPage page, int i, String outputFDir, CountDownLatch latch) { this.page = page; this.i = i; this.outputFDir = outputFDir; this.latch = latch; } @Override public void run() { Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); Image img = page.getImage(rect.width, rect.height, // width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics2D g=tag.createGraphics(); //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(img, 0, 0, rect.width, rect.height, null); FileOutputStream out; try { out = new FileOutputStream(outputFDir + i + ".png"); } catch (FileNotFoundException e1) { e1.printStackTrace(); return; } // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag); param2.setQuality(1f, false);// 1f是提高生成的图片质量 encoder.setJPEGEncodeParam(param2); try { encoder.encode(tag); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // JPEG编码 try { out.close(); } catch (IOException e) { e.printStackTrace(); } latch.countDown(); } }
主程序修改
package com.zas.pdfrender.test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.concurrent.CountDownLatch; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; public class PDFRenderTest { public static void convert(String inputPDFPath, String outputFDir) throws IOException, FileNotFoundException { long beginTime = System.nanoTime(); //pdf文件存在校验,输出文件夹创建 File file = new File(inputPDFPath); if(!file.exists()){ throw new FileNotFoundException("文件不存在: " + inputPDFPath); } File outputFolder = new File(outputFDir); if(!outputFolder.exists()){ outputFolder.mkdirs(); } //获取PDFFile RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); channel.close(); raf.close(); PDFFile pdffile = new PDFFile(buf); System.out.println("PDF页数: " + pdffile.getNumPages() + " , " + inputPDFPath); CountDownLatch latch=new CountDownLatch(pdffile.getNumPages()); //转化处理 for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); PDFThread thread = new PDFThread(page, i, outputFDir, latch); new Thread(thread).start(); } try { latch.await(); long endTime = System.nanoTime(); System.out.println("耗时: " + (endTime - beginTime) / 1000000000 + " 秒" ); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(final String[] args) throws FileNotFoundException, IOException { String inputPDFPath = "D:\\pdf\\ppt\\2010110东南大学档案管理系统需求分析说明书正式.pdf"; String outputFDir = "D:\\pdf\\222222222222010110系统需求分析说明书正式\\"; PDFRenderTest.convert(inputPDFPath, outputFDir); } }
测试结果:
对于一个79页的PDF,不开线程用时8秒,开79个线程用时5秒
对于一个634页的PDF,不开线程用时459秒,开不了634个线程,改进线程程序
PDFThred:
package com.zas.pdfrender.test; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; import java.util.concurrent.CountDownLatch; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFPage; public class PDFThread implements Runnable{ Map<Integer, PDFPage> map; String outputFDir; CountDownLatch latch; public PDFThread(Map<Integer, PDFPage> map, String outputFDir, CountDownLatch latch) { this.map = map; this.outputFDir = outputFDir; this.latch = latch; } @Override public void run() { for (Integer key : map.keySet()) { this.convert(map.get(key), key); } latch.countDown(); } private void convert(PDFPage page, Integer i) { Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); Image img = page.getImage(rect.width, rect.height, // width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Graphics2D g=tag.createGraphics(); //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(img, 0, 0, rect.width, rect.height, null); FileOutputStream out; try { out = new FileOutputStream(outputFDir + i + ".png"); } catch (FileNotFoundException e1) { e1.printStackTrace(); return; } // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param2 = encoder.getDefaultJPEGEncodeParam(tag); param2.setQuality(1f, false);// 1f是提高生成的图片质量 encoder.setJPEGEncodeParam(param2); try { encoder.encode(tag); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // JPEG编码 try { out.close(); } catch (IOException e) { e.printStackTrace(); } } }
主程序:
package com.zas.pdfrender.test; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.CountDownLatch; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; public class PDFRenderTest { public static void convert(String inputPDFPath, String outputFDir) throws IOException, FileNotFoundException { long beginTime = System.nanoTime(); //pdf文件存在校验,输出文件夹创建 File file = new File(inputPDFPath); if(!file.exists()){ throw new FileNotFoundException("文件不存在: " + inputPDFPath); } File outputFolder = new File(outputFDir); if(!outputFolder.exists()){ outputFolder.mkdirs(); } //获取PDFFile RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); channel.close(); raf.close(); PDFFile pdffile = new PDFFile(buf); System.out.println("PDF页数: " + pdffile.getNumPages() + " , " + inputPDFPath); int threadNumber = 0; if(pdffile.getNumPages() % 8 != 0){ threadNumber = pdffile.getNumPages() / 8 + 1; }else{ threadNumber = pdffile.getNumPages() / 8 ; } CountDownLatch latch=new CountDownLatch(threadNumber); //转化处理 int threadCount = 0; Map<Integer, PDFPage> map = new LinkedHashMap<Integer, PDFPage>(); for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); map.put(i, page); if(i % 8 == 0){ PDFThread thread = new PDFThread(map, outputFDir, latch); new Thread(thread).start(); threadCount++; map = new LinkedHashMap<Integer, PDFPage>(); } } System.out.println("threadCount = " + threadCount); if(map.size() > 0){ PDFThread thread = new PDFThread(map, outputFDir, latch); new Thread(thread).start(); threadCount++; } System.out.println("threadCount = " + threadCount + " : map size = " + map.size()); try { latch.await(); long endTime = System.nanoTime(); System.out.println("耗时: " + (endTime - beginTime) / 1000000000 + " 秒" ); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(final String[] args) throws FileNotFoundException, IOException { // String inputPDFPath = "D:\\pdf\\2010110东南大学档案管理系统需求分析说明书正式.pdf"; String inputPDFPath = "D:\\pdf\\面向对象软件构造(第二版)中英对照版.pdf"; // String inputPDFPath = "D:\\pdf\\Linux命令行技术大全.pdf"; String outputFDir = "D:\\pdf\\222222222222010110系统需求分析说明书正式\\"; PDFRenderTest.convert(inputPDFPath, outputFDir); } }
测试结果:
对于544页的PDF,开不开线程对于时间影响不大,应该在图像处理部分阻塞掉了
在继续定位处理
发表评论
-
oracle按照某一字段里的数字排序
2014-10-21 19:59 1028select * from LSK_SBCAJ t ord ... -
JS onkeydown onenter
2014-10-20 16:53 957html中 onenter不是一个标准的事件。 js 中仿o ... -
Java数组删除指定元素
2014-09-18 11:30 2206package com.zas.util; impo ... -
sql 去重
2014-09-18 10:43 576delete from table t1 where t1.i ... -
linux 干掉所有java进程
2014-08-07 12:31 992ps -ef|grep java|grep -v grep|c ... -
Oracle自带连接池使用(转载收录)
2014-07-31 10:01 1363最近在搞数据迁移:从sql server 迁数据到oracle ... -
html dom jsoup httpclient
2014-07-10 21:45 1051xml dom 对大多数java程序员来说并不陌生,但是htm ... -
Oracle 清库脚本
2014-07-08 22:40 1256清库脚本一份 表dossier_group 的字段Dossi ... -
Java 对象存储到oracle Blob字段
2014-07-08 14:52 1058Java 数据对象在没有持久存储到业务表时,可能需要临时存 ... -
Java 科学计数法数字转字符串
2014-07-08 14:30 1428科学计数法数字转字符串,记录代码,留后使用 double ... -
突破tomcat jsp编译65535行的限制
2014-07-04 17:16 4675使用tomcat时有可能会遇到其对jsp编译行数的限制, ... -
oracle 函数中游标及递归的应用
2014-06-19 17:13 1373在代码中使用递归可能大部分程序员都不陌生,但是在存储过程或 ... -
视频操作类
2014-06-19 17:04 1083接 视频分割项目预研 http://zhuyufufu.i ... -
视频分割项目预研
2014-06-11 16:12 2161由于工作需要,研究下视频切割。 现在的情况:视频切割是重中之 ... -
Java POI Excel 行高自适应
2014-03-28 14:08 15598在Excel处理的过程中,可能有需要用到行高自适应的时候。 ... -
Java POI Excel sheet 合并遇到的问题解决2
2014-03-25 18:03 3171上接 Java POI Excel sheet 合并 http ... -
文档展示:使用iText转换各种图片为PDF
2014-03-23 12:38 2847如题: 下面这段代码可以处理各种格式的图片,代码的出处忘记了 ... -
Java 进程执行外部程序,造成外部程序阻塞的一种原因
2014-03-23 12:06 1424前一阵子在研究文档展示时使用了java进程直接调用外部程序 ... -
Java POI Excel sheet 合并遇到的问题解决
2014-03-23 11:30 5017上接 Java POI Excel sheet http:// ... -
Java POI Excel sheet合并
2014-03-19 10:59 6562由于工作上的需要,特地研究了下Excel合并的问题,现贴出来, ...
相关推荐
office文档通过openoffice或者microsoft多线程转换成pdf文档,并通过pdf.js显示
根据网上的资料总结的一个springboot 转换pdf Word文档大小最好2m以下 需要安装OpenOffice.org 3.3 链接:https://pan.baidu.com/s/1onrkhBCNlGLEmf3hPwzXWw 密码:8h5a
Win32多线程程序设计.pdf(带目录)Win32多线程程序设计.pdf(带目录)Win32多线程程序设计.pdf(带目录)Win32多线程程序设计.pdf(带目录)Win32多线程程序设计.pdf(带目录)Win32多线程程序设计.pdf(带目录)...
迅捷PDF转换成Word转换器具备三大特色功能: 完美的PDF文件识别技术:迅捷...超线程技术使得软件本身可以在高效率转换程序的基础之上,进一步接触多核心CPU的强大执行效率,辅助提升转换的效率,减少转换过程的耗时。
4.转换效率高:晨光pdf转换器采用了最新的超远程转换技术,超线程技术的出现大大提高了软件的转换效率,深度的解析和识别更好地保证了转换质量。简单清爽的操作界面,只需轻轻点击“开始转换”按钮,即刻一键转换。...
批量pdf转高清png图片,支持多文件多线程同时转换,转出支持彩色、黑白与灰度png格式图片 使用方法:双击运行软件,选择要执行程序点击确定,跳出文件夹拾取框选择文件所在文件夹,程序自动转换文件夹下所有pdf文件...
易捷PDF转换成Word工具是一款专业的PDF转Word工具软件,易捷pdf转word转换器能快速准确的将您PDF文档中的文字及图片提取出来,转换保存成Word格式,同时很好的让原有PDF中的文字/字体/大小/行间距等保持不变,图片也...
Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux...
linux多线程编程.pdf
4.转换效率高:pdf转换成word转换器采用了最新的超远程转换技术,超线程技术的出现大大提高了软件的转换效率,深度的解析和识别更好地保证了转换质量。简单清爽的操作界面,只需轻轻点击“开始转换”按钮,即刻一键...
Java线程:线程状态的转换.pdf
Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调度-休眠 Java线程:线程的调度-优先级 Java线程:线程的调度-让步 Java线程:线程的调度-合并 Java线程:线程的...
Linux下的多线程编程.pdf
这种多线程处理方式可以大大提高相机图像处理的效率和速度,使得相机可以更快地处理和分析图像数据,提高图像处理的精度和准确性。同时,多线程处理还可以避免互相干扰,提高系统的稳定性和实时性。本资源提供一个...
java使用了itext7将PDF文件转成png图片格式,里面有demo可以直接运行,希望给需要的朋友带来帮助,谢谢支持!
Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调度-休眠 Java线程:线程的调度-优先级 Java线程:线程的调度-让步 Java线程:线程的调度-合并 Java线程:线程的调度-...
pdf转换成为图片需要的两个jar,包含fontbox和pdfbox.jar
C#多线程编程实例实战.pdf
初学者学习java多线程的必备良师啊!! 该文档内容简单易懂 条理清晰 !! 内容包含:线程的状态及状态之间的转换 线程的优先级 线程的几大常用方法! 申明: 本文出自 “熔 岩” 博客,本人从百度文库转载而来! 大家都...