Commit 8650aefb by guanguan1009

新增多线程

parent 5815c54d
......@@ -57,6 +57,11 @@
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
</dependencies>
<build>
......
package com.jtyjy.pdf2pptx.converter;
import com.jtyjy.pdf2pptx.support.MyTask;
import com.lowagie.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
......@@ -10,6 +11,11 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Pdf2Png {
......@@ -19,9 +25,19 @@ public class Pdf2Png {
private static int endPage = 0;
// 执行标识
private boolean exeFlag = true;
public static void main(String[] args) {
pdf2Image("F:\\测试文档\\19届高三英语人教版第1期(1).pdf", "F:\\pdf图片", 350);
// pdf2Image("F:\\测试文档\\19届高三英语人教版第1期(1).pdf", "F:\\pdf图片", 350);
int pages = 615;
int totalPage = (pages + 7) / 8;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 1; i <= totalPage; i++) {
map.put(i, i * 8);
}
System.out.println(totalPage);
}
/***
......@@ -32,7 +48,7 @@ public class Pdf2Png {
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return
*/
public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) {
public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi,ExecutorService executorService) {
File file = new File(PdfFilePath);
PDDocument pdDocument = null;
try {
......@@ -43,11 +59,10 @@ public class Pdf2Png {
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
// 获取图片存放的文件夹路径
imgFolderPath = imgPDFPath + File.separator + imagePDFName+"_图片";
imgFolderPath = imgPDFPath + File.separator + imagePDFName + "_图片";
} else {
imgFolderPath = dstImgFolder + File.separator + imagePDFName+"_图片";
imgFolderPath = dstImgFolder + File.separator + imagePDFName + "_图片";
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
......@@ -55,21 +70,20 @@ public class Pdf2Png {
/* dpi越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
if (pages > 30) {
do {
pdDocument = PDDocument.load(file);
renderer = new PDFRenderer(pdDocument);
int totalPage = (pages + 5) / 6;
for (int i = 1; i <= 6; i++) {
startPage = endPage;
endPage = startPage + 30;
endPage = startPage + totalPage;
if (endPage > pages) {
endPage = pages;
}
ToImage(PdfFilePath, dpi, imagePDFName, imgFolderPath, renderer, endPage, startPage);
executorService.execute(new MyTask(startPage, endPage, imgFolderPath, imagePDFName, file));
if (endPage == pages) {
break;
}
}
reader.close();
pdDocument.close();
} while (endPage < pages);
} else {
endPage = pages;
ToImage(PdfFilePath, dpi, imagePDFName, imgFolderPath, renderer, endPage, startPage);
......@@ -87,7 +101,7 @@ public class Pdf2Png {
}
}
private static void ToImage(String PdfFilePath, int dpi, String imagePDFName, String imgFolderPath, PDFRenderer renderer, int endPage,int startPage) throws IOException {
private static void ToImage(String PdfFilePath, int dpi, String imagePDFName, String imgFolderPath, PDFRenderer renderer, int endPage, int startPage) throws IOException {
StringBuffer imgFilePath;
for (int i = startPage; i < endPage; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
......
package com.jtyjy.pdf2pptx.converter;
import com.alibaba.fastjson.JSONObject;
import com.jtyjy.pdf2pptx.support.Constant;
import com.jtyjy.pdf2pptx.support.FileUtil;
import com.jtyjy.pdf2pptx.support.PdfTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
......@@ -9,6 +11,12 @@ import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
/**
* pdf转pptx
* 常量Constant.SOURCE_PATH为待转换文件根目录
......@@ -20,66 +28,116 @@ public class Pdf2Pptx {
private static final Logger logger = LoggerFactory.getLogger(Pdf2Pptx.class);
public static void main(String[] args) {
//读取指定文件夹下所有文件并转换为图片
// conver(Constant.SOURCE_PATH);
}
// 执行标识
private static boolean exeFlag = true;
public void converte(String path){
private static ExecutorService executorService;
public void converte(String path) {
conver(path);
}
private static void conver(String path) {
//读取指定文件夹下所有文件并转换为图片
long startTime = System.currentTimeMillis();
List<String> allFilePathName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allFilePathName);
for (String filePathName : allFilePathName) {
File file = new File(filePathName);
if (file.getName().endsWith(".pdf")) {
//判断图片是否存在
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(path, allDirPathName);
if (!allDirPathName.contains(file.getAbsolutePath().replace(".pdf", "_图片"))) {
List<String> allFilePathName2 = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allFilePathName2);
if (!allFilePathName2.contains(file.getAbsolutePath().replace(".pdf", ".pptx"))) {
logger.info("转换pdf:{} 为图片开始", file.getAbsolutePath());
long begin = System.currentTimeMillis();
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 350);
executorService = Executors.newFixedThreadPool(6);
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 300,executorService);
long end = System.currentTimeMillis();
logger.info("转换pdf:{} 为图片完成,耗时{}ms", file.getAbsolutePath(), end - begin);
} else {
logger.info("文件:{} 已经转换", file.getName());
}
while (exeFlag) {
if (((ThreadPoolExecutor) executorService).getActiveCount() == 0) {
executorService.shutdown();
exeFlag=false;
System.out.println("转换图片已经完成");
}
}
logger.info("======== pdf转图片已完成,准备生成pptx ========");
exeFlag = true;
try {
Thread.sleep(5000);
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
logger.error("错误:{}", e);
}
} else {
logger.info("文件:{} 已经转换", file.getAbsolutePath());
}
}
}
//将图片插入PPT
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(path, allDirPathName);
for (String dirPathName : allDirPathName) {
File dir = new File(dirPathName);
//判断ppt是否存在
if (dir.getName().endsWith("_图片")) {
List<String> allPptPathFileName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allPptPathFileName);
if (!allPptPathFileName.contains(dir.getAbsolutePath().replace("_图片", ".pptx"))) {
List<String> filePathNames = FileUtil.getAllFilePathName(dir.getAbsolutePath());
//转换
logger.info("开始转换图片:{} 为pptx", dir.getName());
long begin = System.currentTimeMillis();
Png2Pptx.createPicturePptx(filePathNames, dir);
long end = System.currentTimeMillis();
logger.info("转换图片:{} 为pptx完成,耗时:{}ms", dir.getName(), end - begin);
} else {
logger.info("文件:{} 已经存在", dir.getName() + ".pptx");
List<String> threads = new ArrayList<>();
threads.add("1");
threads.add("2");
threads.add("3");
threads.add("4");
Map<String, List<String>> allot = allotOfAverage(threads, allDirPathName);
ExecutorService executorService = Executors.newFixedThreadPool(4);
for (int i = 1; i <= 4; i++) {
if (allot.get(String.valueOf(i)) != null) {
executorService.execute(new PdfTask(allot.get(String.valueOf(i)), path));
}
}
while (exeFlag) {
if (((ThreadPoolExecutor) executorService).getActiveCount() == 0) {
executorService.shutdown();
exeFlag=false;
System.out.println("转换ppt已经完成");
logger.info("转换所有ppt已经完成");
}
}
long endTime = System.currentTimeMillis();
logger.info("总耗时:{}ms", endTime - startTime);
//删除图片文件夹
}
/**
* 平均分配
*
* @param users
* @param tasks
* @return
*/
public static Map<String, List<String>> allotOfAverage(List<String> users, List<String> tasks) {
//保存分配的信息
Map<String, List<String>> allot = new ConcurrentHashMap<String, List<String>>();
if (users != null && users.size() > 0 && tasks != null && tasks.size() > 0) {
for (int i = 0; i < tasks.size(); i++) {
int j = i % users.size();
if (allot.containsKey(users.get(j))) {
List<String> list = allot.get(users.get(j));
list.add(tasks.get(i));
allot.put(users.get(j), list);
} else {
List<String> list = new ArrayList<String>();
list.add(tasks.get(i));
allot.put(users.get(j), list);
}
}
}
return allot;
}
public static void main(String[] args) {
//读取指定文件夹下所有文件并转换为图片
// conver(Constant.SOURCE_PATH);
}
}
......@@ -5,7 +5,7 @@ public class Constant {
/**
* 待读取文件根目录
*/
// public static String SOURCE_PATH = "F:\\测试文档";
public static String SOURCE_PATH = "F:\\高二";
public static String SOURCE_PATH = "F:\\测试文档\\高二";
// public static String SOURCE_PATH = "F:\\高二";
}
package com.jtyjy.pdf2pptx.support;
import com.jtyjy.pdf2pptx.converter.Pdf2Png;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class MyTask implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(MyTask.class);
private int startPage;
private int endPage;
private String imgFolderPath;
private String imagePDFName;
private File file;
@Override
public void run() {
PDDocument pdDocument = null;
try {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
StringBuffer imgFilePath;
for (int i = startPage; i < endPage; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i + 1));
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, 300);
logger.info("线程:{} 开始转换第{}页pdf", Thread.currentThread().getName(), i);
ImageIO.write(image, "png", dstFile);
}
}catch (IOException e) {
logger.error("转换图片出错:{}", e);
}finally {
if (pdDocument != null) {
try {
pdDocument.close();
} catch (IOException e) {
logger.error("错误:{}", e);
}
}
}
logger.info("PDF文档:{} 转PNG图片成功!{}页-{}页", file.getAbsolutePath(), startPage , endPage);
}
public int getStartPage() {
return startPage;
}
public void setStartPage(int startPage) {
this.startPage = startPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public MyTask(int startPage, int endPage,String imgFolderPath,String imagePDFName,File file) {
this.startPage = startPage;
this.endPage = endPage;
this.imgFolderPath = imgFolderPath;
this.imagePDFName = imagePDFName;
this.file = file;
}
public MyTask() {
}
}
package com.jtyjy.pdf2pptx.support;
import com.jtyjy.pdf2pptx.converter.Png2Pptx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class PdfTask implements Runnable {
private static final Logger logger = LoggerFactory.getLogger(PdfTask.class);
private List<String> allDirPathName;
private String path;
@Override
public void run() {
for (String dirPathName : allDirPathName) {
File dir = new File(dirPathName);
//判断ppt是否存在
if (dir.getName().endsWith("_图片")) {
List<String> allPptPathFileName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allPptPathFileName);
if (!allPptPathFileName.contains(dir.getAbsolutePath().replace("_图片", ".pptx"))) {
List<String> filePathNames = FileUtil.getAllFilePathName(dir.getAbsolutePath());
//转换
logger.info("开始转换图片:{} 为pptx", dir.getName());
long begin = System.currentTimeMillis();
Png2Pptx.createPicturePptx(filePathNames, dir);
long end = System.currentTimeMillis();
logger.info("转换图片:{} 为pptx完成,耗时:{}ms", dir.getName(), end - begin);
} else {
logger.info("文件:{} 已经存在", dir.getName() + ".pptx");
}
}
}
}
public PdfTask(){
}
public PdfTask(List<String> allDirPathName, String path) {
this.allDirPathName = allDirPathName;
this.path = path;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment