开发者

Java Process with Imagemagick

I want to resize a image, for example: convert 1.jpg -resize 250x240> 1.jpg I write some code like this:

File f=new File(request.getRealPath("/")+"pics/"+d.getTime()+"_"+ttt+suffix);    
StringBuffer sb=new StringBuffe开发者_运维问答r();
sb.append("'convert "+f.getAbsolutePath()+" -resize 250x240\\> "+f.getAbsolutePath()+"'");
String command=sb.toString();
Process p=Runtime.getRuntime().exec(command);
int returnv=p.waitFor();
System.out.println("command:"+command+" returnV:"+returnv);

but I found that, when I add the '>' flag to it, the command fails. How can I solve this?


Consider using im4java which is a object oriented java wrapper around the ImageMagic command line tools. It solves exactly those problems that you are facing.

JMagic is another alternativ. It provides a thin JNI layer around the ImageMagic C-API.


The > character is no flag to the convert command but a redirection of the output into the file 1.jpg. You could just call convert with an output file parameter and avoid the redirection. Instead of:

convert 1.jpg -resize 250x240> 1.jpg

use

convert -resize 250x240 1.jpg 1_resized.jpg

BTW, you are escaping you backslashes -resize 250x240\\>, maybe this is not necessary and you could simply write -resize 250x240 >.

Update: Maybe you could use JMagick, a Java library for ImageMagick, instead of executing a system process to convert your image.


Runtime doesn't fire up a fully-fledged shell. You can of course invoke bash to get one.

Or you can create a thread to sit on the process output stream and write it to an output file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜