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 'emadhegab@hotmail.com'" /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 \"emadhegab@hotmail.com\"' "+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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mohamed Emad Hegab
  • 2,665
  • 6
  • 39
  • 64

4 Answers4

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"};
Jawher
  • 6,937
  • 1
  • 17
  • 12
  • +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. – SyntaxT3rr0r Jan 27 '10 at 15:41
  • 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 Jan 27 '10 at 15:43
  • "-font /tmp/TITUSCBZ.TTF" ==> "-font", "/tmp/TITUSCBZ.TTF" "-pointsize 12" ==> "-pointsize", "12" ... – Arne Deutsch Jan 27 '10 at 15:51
  • sorry man but still String[] cmd = {"convert","-size "+ size, "xc:white", "-font", "/tmp/TITUSCBZ.TTF","-pointsize", "12","-draw" ,"'text 300,300 \"emadhohohohohoho\"'", path +"/test.jpg"}; – Mohamed Emad Hegab Jan 27 '10 at 16:15
  • if you tried it and worked for you could you give me the exact line of code – Mohamed Emad Hegab Jan 27 '10 at 16:16
  • i know i'm bothering but it's really important to me :) – Mohamed Emad Hegab Jan 28 '10 at 09:22
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.

Jawher
  • 6,937
  • 1
  • 17
  • 12
  • @Jawher 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. Thanks – bogdan.rusu Jul 29 '15 at 08:55
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.

amarillion
  • 24,487
  • 15
  • 68
  • 80
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 )

Arne Deutsch
  • 14,629
  • 5
  • 53
  • 72
  • 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 Jan 27 '10 at 15:49
  • 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 Deutsch Jan 27 '10 at 16:18