使用ProcessBuilder运行imagemagick转换

时间:2014-07-16 08:33:55

标签: java processbuilder imagemagick-convert

我想在Windows命令提示符下运行以下命令:

convert -density 50 -pointsize 70 -font Arial label:"Text with spaces" output.gif

此命令使用imagemagick并使用文本“Text with spaces”创建一个gif文件并将其保存到output.gif

这很好地直接来自Windows 7命令提示符。现在,我想从我的Java应用程序运行此命令。考虑:

private void runConvert()
{
    try
    {
        ProcessBuilder pb = new ProcessBuilder("convert","-density","50","-pointsize","70"
        ,"font","Arial","label:","\"Text with spaces\"","output.gif");

        pb.redirectErrorStream(true);
        Process process = pb.start();
        BufferedReader inStreamReader = new BufferedReader(newInputStreamReader(process.getInputStream()));

        while(inStreamReader.readLine() != null){
            System.out.println(inStreamer.readLine());
        }

     } catch (Exception e){
         e.printStackTrace();
     }
}

问题是,这会使convert回显:

convert.exe: no decode delegate for this image format `' @ error/constitue.c/ReadImage/501

如果我尝试将runConvert函数与没有空格的文本参数一起使用,例如“BurgerKing”,它就像魅力一样。如何修复多空间参数?

1 个答案:

答案 0 :(得分:0)

您应指定 label:"带空格" 的文字作为单个参数:

ProcessBuilder pb = new ProcessBuilder("convert","-density","50","-pointsize",
"70","-font","Arial","label:\"Text with spaces\"","output.gif");