Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

problem with ImageMagick And Java Runtime Exec

I Have a bit of a strange problem no java expert i know could solve ..

i need to used imagemagick on my application to make the emails on my website converted to images so no pot can take the emails easily .. the problem solved with image magick command line as following convert -size 200x30 xc:transparent -font /home/emad/TITUSCBZ.TTF -fill black -pointsize 12 -draw "text 5,15 '[email protected]'" /home/emad/test.png

and it work like magic really and so i tried to put that on the java to run it with Runtime.getRuntime().exec(command) but the result is sadly disappointing .. i have now image as output ..but with no text inside.. i do a sys out to see the command and took the command that outed and put it in the terminal and it worked..so the problem in the Runtime some how.. the code of java is .. in case you are asking

=================

            String size = ("1000x1030");

    String path = System.getProperty("user.home");
    String command="convert -size "+ size +" xc:white -font /tmp/TITUSCBZ.TTF -pointsize 12 -draw 'text 300,300 \"[email protected]\"' "+path +"/test.jpg";
    try{
    Process proc =Runtime.getRuntime().exec(command);

    System.out.println(command);
    }catch(Exception e){
        System.out.println("error");
    }

=================

it'll give you blank image .. do any one have a solution

Upvotes: 0

Views: 5953

Answers (4)

Jawher
Jawher

Reputation: 7097

This works for me :

String size = "1024x768";
ProcessBuilder pb = new ProcessBuilder("convert", "-size", size,
        "xc:white", "-font",
        "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf",
        "-pointsize", "12", "-draw",
        "text 300,300 \"*****@hotmail.com\"",
        "/home/djo/Pictures/rainy.jpeg");
pb.redirectErrorStream(true);

Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line=br.readLine())!=null){
    System.out.println(line);
}
System.out.println(p.waitFor());

Note that I took off the single quotation marks from the draw part.

Upvotes: 1

Jawher
Jawher

Reputation: 7097

You need to pass the command and it's args as a String array, not a String concatenation.

String[] cmd = {"convert",  "-size", "size", "c:white", ..., path +"/test.jpg"};

Upvotes: 2

Arne Deutsch
Arne Deutsch

Reputation: 14769

You should:

  1. Create a thread that reads the output of the process. Maybe the (platform dependent) buffer for the answere of your process fills up (the JVM might dead lock then).

  2. Maybe java could not find the "convert" command ... use an overloaded version of "exec" that takes a current dir as parameter ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String,%20java.lang.String[],%20java.io.File )

Upvotes: 0

amarillion
amarillion

Reputation: 24917

Is this java program run by you or by the web server?

Because if it's the latter, it's likely that the property user.home does not have the value you expect.

Also, the position (300, 300) and the font location (/tmp/TITUSCBZ.TTF) are different than in the example you give first. Perhaps you should double-check that.

Upvotes: 0

Related Questions