`
zhuyufufu
  • 浏览: 135015 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

文档展示:PDFRender 将PDF转换为图片 多线程处理 提高效率

阅读更多
上接 文档展示:PDFRender 将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,开不开线程对于时间影响不大,应该在图像处理部分阻塞掉了
在继续定位处理
0
0
分享到:
评论

相关推荐

    office文档通过openoffice或者microsoft多线程转换成pdf文档并通过pdfjs显示

    office文档通过openoffice或者microsoft多线程转换成pdf文档,并通过pdf.js显示

    springboot 文件转换PDF多线程

    根据网上的资料总结的一个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(带目录)Win32多线程程序设计.pdf(带目录)...

    迅捷PDF转换成Word转换器

    迅捷PDF转换成Word转换器具备三大特色功能: 完美的PDF文件识别技术:迅捷...超线程技术使得软件本身可以在高效率转换程序的基础之上,进一步接触多核心CPU的强大执行效率,辅助提升转换的效率,减少转换过程的耗时。

    晨光万能pdf转换器V3.2(内含注册机).zip

    4.转换效率高:晨光pdf转换器采用了最新的超远程转换技术,超线程技术的出现大大提高了软件的转换效率,深度的解析和识别更好地保证了转换质量。简单清爽的操作界面,只需轻轻点击“开始转换”按钮,即刻一键转换。...

    批量pdf转高清png图片,支持多文件多线程同时转换,转出支持彩色、黑白与灰度png格式图片

    批量pdf转高清png图片,支持多文件多线程同时转换,转出支持彩色、黑白与灰度png格式图片 使用方法:双击运行软件,选择要执行程序点击确定,跳出文件夹拾取框选择文件所在文件夹,程序自动转换文件夹下所有pdf文件...

    易捷PDF转Word(PDF转换成Word工具) v3.2 免费版.zip

    易捷PDF转换成Word工具是一款专业的PDF转Word工具软件,易捷pdf转word转换器能快速准确的将您PDF文档中的文字及图片提取出来,转换保存成Word格式,同时很好的让原有PDF中的文字/字体/大小/行间距等保持不变,图片也...

    Linux系统下的多线程编程入门.pdf

    Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux系统下的多线程编程入门.pdf Linux...

    linux多线程编程.pdf

    linux多线程编程.pdf

    晨光PDF转换成WORD转换器 v3.1.zip

    4.转换效率高:pdf转换成word转换器采用了最新的超远程转换技术,超线程技术的出现大大提高了软件的转换效率,深度的解析和识别更好地保证了转换质量。简单清爽的操作界面,只需轻轻点击“开始转换”按钮,即刻一键...

    Java线程:线程状态的转换.pdf

    Java线程:线程状态的转换.pdf

    Java多线程编程总结

    Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调度-休眠 Java线程:线程的调度-优先级 Java线程:线程的调度-让步 Java线程:线程的调度-合并 Java线程:线程的...

    Linux下的多线程编程.pdf

    Linux下的多线程编程.pdf

    相机采集、处理、显示多线程处理框架

    这种多线程处理方式可以大大提高相机图像处理的效率和速度,使得相机可以更快地处理和分析图像数据,提高图像处理的精度和准确性。同时,多线程处理还可以避免互相干扰,提高系统的稳定性和实时性。本资源提供一个...

    java将PDF文件转成png图片格式

    java使用了itext7将PDF文件转成png图片格式,里面有demo可以直接运行,希望给需要的朋友带来帮助,谢谢支持!

    java多线程编程总结

    Java线程:线程状态的转换 Java线程:线程的同步与锁 Java线程:线程的交互 Java线程:线程的调度-休眠 Java线程:线程的调度-优先级 Java线程:线程的调度-让步 Java线程:线程的调度-合并 Java线程:线程的调度-...

    pdf转换成为图片需要的两个jar

    pdf转换成为图片需要的两个jar,包含fontbox和pdfbox.jar

    C#多线程编程实例实战.pdf

    C#多线程编程实例实战.pdf

    Java线程:线程状态的转换

    初学者学习java多线程的必备良师啊!! 该文档内容简单易懂 条理清晰 !! 内容包含:线程的状态及状态之间的转换 线程的优先级 线程的几大常用方法! 申明: 本文出自 “熔 岩” 博客,本人从百度文库转载而来! 大家都...

Global site tag (gtag.js) - Google Analytics