【发布时间】:2014-09-30 05:34:00
【问题描述】:
我需要在 image magick 工具的帮助下使用进程构建器在文件夹内进行图像转换。我需要在转换后删除原始文件,但是当我使用等待它需要很长时间才能完成。我使用了以下代码
for (int h = 0; h < convimagefolder.size(); h++) {
/* CMYK conversion for jpg and tiff and also .psd conversion*/
destpathfinalconvs = new File(tempdir + "/" + convimagefolder.get(h));
// JOptionPane.showMessageDialog(null, "CMYK jpg conversion - folder assign");
ProcessBuilder pb = new ProcessBuilder("mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.jpg");
pb.redirectErrorStream(true);
try {
Process p = pb.start();
System.out.println("CMYK to RGB jpg done for "+convimagefolder.get(h));
// try {
// p.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
}
// JOptionPane.showMessageDialog(null, "tif conversion - folder assign");
ProcessBuilder pb1 = new ProcessBuilder("mogrify", "-colorspace", "RGB", destpathfinalconvs.toString() + "\\" + "*.tif");
pb1.redirectErrorStream(true);
try {
Process p1 = pb1.start();
System.out.println("cmyk tif to rgb done " + convimagefolder.get(h));
// try {
// p1.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
}
// JOptionPane.showMessageDialog(null, "psd conversion - folder assign");
ProcessBuilder pb2 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.psd[0]");
pb2.redirectErrorStream(true);
try {
Process p2 = pb2.start();
System.out.println("psd to jpg done" + convimagefolder.get(h));
// try {
// p2.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
}
/* CMYK conversion for jpg and tiff and also .psd conversion*/
/* tif to jpg conversion*/
ProcessBuilder pb3 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.tif");
pb3.redirectErrorStream(true);
try {
Process p3 = pb3.start();
// try {
// p3.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
System.out.println(".tif to jpg done successfully"+ convimagefolder.get(h));
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Error in tif to jpg conversion " + ex.toString());
}
/* pdf to jpg*/
ProcessBuilder pb4 = new ProcessBuilder("mogrify", "-format", "jpg", destpathfinalconvs.toString() + "\\" + "*.pdf");
pb4.redirectErrorStream(true);
try {
Process p4 = pb4.start();
// try {
// p4.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
System.out.println(".pdf to jpg done successfully"+ convimagefolder.get(h));
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Error in tif to jpg conversion " + ex.toString());
}
/* image size reduction*/
ProcessBuilder pb5 = new ProcessBuilder("mogrify", "-strip", "-quality", "50%", destpathfinalconvs.toString() + "\\" + "*.jpg");
// ProcessBuilder pb5 = new ProcessBuilder("mogrify", "-path ", destinationpath.toString(), "-strip", "-quality", "50%", destinationpath.toString() + "\\" + "*.jpg");
pb5.redirectErrorStream(true);
try {
Process p5 = pb5.start();
// try {
// p5.waitFor();
// } catch (InterruptedException ex) {
// Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
// }
System.out.println("image conversion done successfully"+ convimagefolder.get(h));
} catch (IOException ex) {
Logger.getLogger(DefineTask.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Error in jpg compression " + ex.toString());
}
}
JOptionPane.showMessageDialog(null, "Image compression and conversion done using Image Magick");
是否可以通过线程来做到这一点。请提出任何缩短转换时间的想法
【问题讨论】:
-
是的,这是可能的,但它不会让它更快,你仍然需要在
Thread中运行整个块或作为一系列链式任务......它是什么你希望改进吗? -
在此转换之后,我正在删除文件,但是当该过程未完成时,没有任何文件被添加到我的数组列表中删除
-
我需要减少转换时间。这里需要2-4分钟。我需要减少这个时间
-
最好的希望是交错执行进程,以便一次可以运行多个进程,这实际上可能会增加转换时间,因为它们不会竞争机器资源...
标签: java multithreading imagemagick image-conversion