我一直在努力通过简单的Java应用程序执行命令。 我需要执行的命令位于http://www.imagemagick.org/Usage/annotating/#wmark_text(标题为“带图像的水印”),如下所示:
convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'Copyright'" -gravity SouthEast -draw "text 5,15 'Copyright'" miff:- | composite -tile - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg
(我已经通过将命令直接输入终端来测试命令并且它有效)
我无法正确创建命令。使用我的代码构造以下命令:
convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'Copyright" -gravity SouthEast -draw "text 5,15 'Copyright" miff:- | composite -title - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg
此外,还会返回一些与图像magick相关的错误:
convert: non-conforming drawing primitive definition `text 10,10 'Copyright' @ error/draw.c/DrawImage/3178.
convert: non-conforming drawing primitive definition `text 5,15 'Copyright' @ error/draw.c/DrawImage/3178.
convert: unable to open image `- |': No such file or directory @ error/blob.c/OpenBlob/2643.
convert: unable to open image `composite': No such file or directory @ error/blob.c/OpenBlob/2643.
convert: unable to open image `composite': No such file or directory @ error/blob.c/OpenBlob/2643.
convert: no decode delegate for this image format `composite' @ error/constitute.c/ReadImage/555.
convert: unrecognized option `-title' @ error/convert.c/ConvertImageCommand/2984.
如果有人知道我需要在代码中更改以构建正确的命令,或者知道更好的方法,请告诉我。
代码中可能造成麻烦的部分是“围绕着” 未在生成的命令和管道中打印的版权 命令
.addArgument("miff:- | ",false);
以下是使用的代码:
import org.apache.commons.exec.*;
import java.io.IOException;
class Test {
public static void main( String[] args )
{
applyWatermark(null,null);
}
public static void applyWatermark(String imagePaath,String watermark){
String imagePath = "/Users/latu/Pictures/desert.jpg";
String imagePath2 = "/Users/latu/Pictures/desertCP.jpg";
CommandLine convert_cmd = new CommandLine("convert");
convert_cmd.addArgument("-size")
.addArgument("140x80")
.addArgument("xc:none")
.addArgument("-fill")
.addArgument("grey")
.addArgument("-gravity")
.addArgument("NorthWest")
.addArgument("-draw")
.addArgument( "text 10,10 'Copyright'")
.addArgument("-gravity")
.addArgument("SouthEast")
.addArgument("-draw")
.addArgument("text 5,15 'Copyright'")
.addArgument("miff:- |",false);
// CommandLine wm_cmd = new CommandLine("composite");
convert_cmd.addArgument("composite")
.addArgument("-title")
.addArgument("- "+imagePath,false)
.addArgument(imagePath2);
System.out.println(convert_cmd.toString());
executeCommand(convert_cmd);
// executeCommand(wm_cmd);
/*
http://www.imagemagick.org/Usage/annotating/#wmark_text
correct command: convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw "text 10,10 'Copyright'" -gravity SouthEast -draw "text 5,15 'Copyright'" miff:- | composite -tile - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg
*/
}
private static void executeCommand(CommandLine cmdLine){
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.setWatchdog(watchdog);
try
{
executor.execute(cmdLine, resultHandler);
} catch (ExecuteException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
根据t his question上接受的答案,我已将我的代码更改为以下按预期工作的代码:
String [] cmd ={"-c","convert -size 140x80 xc:none -fill grey -gravity NorthWest -draw \"text 10,10 'Copyright'\" -gravity SouthEast -draw \"text 5,15 'Copyright'\" miff:- | composite -tile - /Users/latu/Pictures/desert.jpg /Users/latu/Pictures/desertCP.jpg"};
CommandLine convert_cmd = new CommandLine("/bin/sh");
convert_cmd.addArguments( cmd,false );