views:

482

answers:

5

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

+1  A: 

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"};
Jawher
+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.
Webinator
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 came
Mohamed Emad Hegab
"-font /tmp/TITUSCBZ.TTF" ==> "-font", "/tmp/TITUSCBZ.TTF""-pointsize 12" ==> "-pointsize", "12"...
Arne
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>
Mohamed Emad Hegab
if you tried it and worked for you could you give me the exact line of code
Mohamed Emad Hegab
i know i'm bothering but it's really important to me :)
Mohamed Emad Hegab
A: 

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.

amarillion
A: 

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 )

Arne
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 :)
Mohamed Emad Hegab
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."
Arne
A: 

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.

Jawher
that's work sir thank you so much you saved me :D
Mohamed Emad Hegab
A: 

Is the command executed on a unix system ? If so, the process you start requires the same amount of memory as the parent process. This may cause an IOException, "cannot allocate memory". Is an exception thrown in your case, and what does is it show ? At the moment I have the same problems, I will return if I found a solution.

athkaptah