M的主要目标是接收一系列.eps文件,并使用ImageMagick和GhostScript将它们转换为.jpg。我在Windows环境中安装了ImageMagick和GhostScript。我正在使用Java中的Process引用ImageMagick的转换命令而没有运气。使用Window的cmd
工具,我通过导航到 C:\ Program Files \ ImageMagick-6.8.9-Q16 并使用以下命令成功地将EPS转换为JPEG: / p>
convert Raw\R_GiftcardSizeNew3x5.eps Converted\R_GiftcardSizeNew3x5.jpg
在Java中,我在以下代码中使用了几乎完全相同的命令:
public void convertEPStoJPG()
{ //commands
ArrayList<String> cmds = new ArrayList<String>();
//absolute file paths of eps files retrieved using a helper method
ArrayList<String> filePaths = this.getFilePaths();
//beginning cmd line calls
cmds.add("cmd.exe");
cmds.add("/c");
cmds.add("cd C:\\Program Files\\ImageMagick-6.8.9-Q16\\");
for (int i = 0; i < filePaths.size(); i++)
{
//conversion calls
String tempPath = filePaths.get(i);
//shortening path name
tempPath = tempPath.substring(tempPath.lastIndexOf("\\") + 1, tempPath.length());
//adding command of "convert Raw\\image.eps Converted\\image.jpg"
cmds.add("convert \\Naked Wines\\Raw\\" + tempPath + " \\Naked Wines\\Converted\\" +
tempPath.substring(0,tempPath.length() - 3) + "jpg");
}
//building process with commands
ProcessBuilder pb = new ProcessBuilder(cmds);
Process process;
try {
pb.redirectErrorStream(true);
//executing commands
process = pb.start();
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
//print output from command execution
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我试图抓取的文件是 C:\ Program Files \ ImageMagick-6.8.9-Q16 \ Naked Wines \ Raw 我要转换的目的地是 C:\ Program Files \ ImageMagick-6.8.9-Q16 \ Naked Wines \ Converted 。
我收到错误,指出&#34;系统无法找到指定的路径&#34; 。查看以前回答的问题,例如How to override Windows' convert command by ImageMagick's one?,人们建议您必须覆盖Windows convert
命令。这会是错误的原因,还是我缺少的东西?我对ImageMagick很新,可能错过或误解了一些东西。
答案 0 :(得分:1)
我最终使用Im4Java以不同的方式解决了这个问题,Im4Java是ImageMagick命令行的纯java接口。我通过http://im4java.sourceforge.net/#download安装了库。这是我将eps转换为jpg的代码:
public void convertESPtoJPG()
{
//initialize ImageMagick operation
IMOperation op = new IMOperation();
//setting my path allows us to use ImageMagicks "convert" vs. Windows "convert"
String myPath="C:\\Program Files\\ImageMagick-6.8.9-Q16";
ProcessStarter.setGlobalSearchPath(myPath);
op.addImage(); //in
op.addImage(); //out
ConvertCmd cmd = new ConvertCmd();
//filter out files for eps files, and load the files using included FilenameLoader
ExtensionFilter filter = new ExtensionFilter("eps");
FilenameLoader loader = new FilenameLoader(filter);
List<String> files = loader.loadFilenames("C:\\Program Files\\ImageMagick-6.8.9-
Q16\\NakedWines\\Raw\\");
//what we plan on converting our eps files to
FilenamePatternResolver resolver = new FilenamePatternResolver("%P/%f.jpg");
//iterate through loaded files
for (String img: files)
{
try {
//execute our convert commands
cmd.run(op,img,resolver.createName(img));
} catch (Exception e) {
e.printStackTrace();
}
}
}
我发现这种方法更容易理解,也更容易前进。