problem with ImageMagick And Java Runtime Exec
Asked Answered
V

4

0

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

Vennieveno answered 27/1, 2010 at 15:20 Comment(2)
Does it work when you paste the command String value to a shell and execute it from there?Attain
yes and that what blowing my mind :(Vennieveno
M
1

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.

Margarettemargarida answered 28/1, 2010 at 11:3 Comment(1)
@Margarettemargarida what exactly did the input stream contains? I tried to debug the inputStream and the errorStream and no result in any of them. I know is an old answer but maybe somebody can help. ThanksRonald
M
2

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"};
Margarettemargarida answered 27/1, 2010 at 15:26 Comment(6)
+1 to Jahwer's answer: don't ever pass directly a string as-is to Runtime.exec. Split it into an array (ie String[]) and then call Runtime.exec.Hebraic
String[] cmd = {"convert","-size "+ size, "xc:white", "-font /tmp/TITUSCBZ.TTF","-pointsize 12","-draw" ,"'text 300,300 \"emadhohohohohoho\"'", path +"/test.jpg"}; try{ Process proc =Runtime.getRuntime().exec(cmd); i tried as the above but nothing happend the empty image didn't even cameVennieveno
"-font /tmp/TITUSCBZ.TTF" ==> "-font", "/tmp/TITUSCBZ.TTF" "-pointsize 12" ==> "-pointsize", "12" ...Praxiteles
sorry man but still <b> String[] cmd = {"convert","-size "+ size, "xc:white", "-font", "/tmp/TITUSCBZ.TTF","-pointsize", "12","-draw" ,"'text 300,300 \"emadhohohohohoho\"'", path +"/test.jpg"}; <b>Vennieveno
if you tried it and worked for you could you give me the exact line of codeVennieveno
i know i'm bothering but it's really important to me :)Vennieveno
M
1

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.

Margarettemargarida answered 28/1, 2010 at 11:3 Comment(1)
@Margarettemargarida what exactly did the input stream contains? I tried to debug the inputStream and the errorStream and no result in any of them. I know is an old answer but maybe somebody can help. ThanksRonald
T
0

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.

Tonsil answered 27/1, 2010 at 15:26 Comment(0)
P
0

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 )

Praxiteles answered 27/1, 2010 at 15:26 Comment(2)
i don't think that java don't find the convert command cause it's create an empty image in the specific location .. but the thing is the text part is not putted for some reason..you can forget about the process thing it's kinda mistake i'm not going to wait any return just need the photo to come out with some text on it :)Vennieveno
but you have to read the result anyhow ... if you don't do it you risk deadlock (from the docs: "Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock."Praxiteles

© 2022 - 2024 — McMap. All rights reserved.