commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ioannis latousakis <latus...@gmail.com>
Subject [exec] command with single quotes and pipe
Date Thu, 19 Dec 2013 09:29:56 GMT
I have been trying to execute a command for imagemagick using apache exec, although I cannot
get the command to construct properly. A well formatted SO question on this is here: http://stackoverflow.com/questions/20664301/executing-complex-command-with-commons-exec

The command I need to execute is this: 

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
This has been tested directly into terminal and works. 

With my code the following command is constructed:

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
Notice that one quote is missing from “Copyright”. I am using [ .addArgument( "text 10,10
'Copyright’") ] to construct that part.
 Also part of this command is a pipe character, which I believe is not working correctly as
well. [ .addArgument("miff:- |",false);]


This is the output I get in my java application: 

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.
Any suggestions on what I need to change in my code for the above command to work? 

Complete code:



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();
        }
    }
}
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message