我本质上是在尝试使用itext将tiff文件转换为pdf,这非常简单.
但是从我可以看到TiffImage.getTiffImage花费大量时间来执行较大的文件.
我的要求是使用FutureTask和ExecutorService提供多线程解决方案.这是我当前的代码:
import java.util.concurrent.Callable;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;
public class ProcessTiffImage implements Callable{
RandomAccessFileOrArray tiffFile;
int pageNo;
public ProcessTiffImage(RandomAccessFileOrArray tiffFile, int pageNo){
this.tiffFile = tiffFile;
this.pageNo = pageNo;
}
public Image call() throws Exception {
Image image = TiffImage.getTiffImage(tiffFile, pageNo);
return image;
}
}
而convert方法是
public boolean convert(Document document) {
int numOfThreads = Runtime.getRuntime().availableProcessors() ;
ExecutorService service = Executors.newFixedThreadPool(numOfThreads );
List> taskList = new ArrayList>();
List imageList = new ArrayList();
for (int page = 1; page <= numOfPages; page++) {
FutureTask futureTask = new FutureTask(new ProcessTiffImage(tiffFile, page));
taskList.add(futureTask);
service.execute(futureTask);
}
try {
// Wait until all results are available
for (FutureTask future : taskList) {
imageList.add(future.get());
}
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
service.shutdown();
boolean success = generatePdf(document, imageList);
return success;
}
但是我在future.get()处收到了NullPointerException.问题是执行没有等待TiffImage.getTiffImage(tiffFile,pageNo)完成.因此,我无法创建图像列表.
任何帮助将不胜感激.
堆栈跟踪
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:188)
at com.app.convertor.TiffParser.convert(TiffParser.java:107)
at com.app.start.TiffToPdf.main(TiffToPdf.java:40)
Caused by: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.itextpdf.text.pdf.codec.TIFFDirectory.getFieldAsLong(TIFFDirectory.java:467)
at com.itextpdf.text.pdf.codec.TIFFDirectory.getFieldAsLong(TIFFDirectory.java:477)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:124)
at com.itextpdf.text.pdf.codec.TiffImage.getTiffImage(TiffImage.java:106)
at com.app.processor.ProcessTiffImage.call(ProcessTiffImage.java:20)
at com.app.processor.ProcessTiffImage.call(ProcessTiffImage.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)