problem with ImageMagick And Java Runtime Exec

别来无恙 提交于 2019-12-08 13:44:47

问题


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


回答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.




回答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"};



回答3:


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.




回答4:


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 )



来源:https://stackoverflow.com/questions/2148043/problem-with-imagemagick-and-java-runtime-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!