gpt4 book ai didi

java - 使用 commons-exec 执行复杂命令

转载 作者:行者123 更新时间:2023-12-01 23:20:46 26 4
gpt4 key购买 nike

我一直在努力通过一个简单的 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.

如果有人知道我需要在代码中更改什么才能构建正确的命令,或者知道更好的方法,请告诉我。

The parts of the code that definetly cause trouble are ' ' around Copyright which are not printed in the generated command, and the pipe command at .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();
}
}
}

最佳答案

遵循 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 );

关于java - 使用 commons-exec 执行复杂命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com