Image processing with IM4java

记得以前做图片相关需求的时候,使用的是Jmagick+ImageMagick,一直有一个难以解决的问题,就是使用Jmagick之后,往往运行数天后就会导致jboss的jvm崩溃掉。
这几天发现了一个新东西,或能解决这个老问题,记录一下,留备后用,那就是ImageMagick的另外一种java接口:IM4java。
IM4Java和Jmagick不同之处,在于前者不是通过JNI的方式来调用ImageMagick,而是生成命令来调用,据官网上说的,这样做有几个好处,
Advantages of im4java: the interface of the IM commandline is quite stable, so your java program (and the im4java-library) will work across many versions of IM. im4java also provides a better OO interface (the "language" of the IM-commandline with it's postfix-operation notation translates very easily into OO-notation). And most important: you can use im4java everywhere JMagick can't be used because of the JNI hazard (e.g. java application servers).
im4java的优点:im命令行接口相当稳定,可以适应多种版本的IM,它还提供了友好的OO接口,最重要的是,它不存在“JNI风险”
about  JNI hazard
Running native code using JNI from within java always imposes additional risks. The JVM is no longer sandboxed, so there might be some security issues. In addition, there could be errors like memory leaks or memory corruption within the JNI-layer (JMagick) or within the native code (ImageMagick).
使用JNI运行本地代码始终面临额外风险,JVM不在被封闭于沙箱内,因此有可能会有一些安全问题,此外,还有可能会在JNI层引起内存泄漏和内存破坏。
This is especially dangerous for long running processes (typically web-application-servers). Memory corruption or a segmentation fault (maybe triggered by a intentionally manipulated image) might bring down the whole server.
特别是对于一些要长期运行的进程(典型如web 服务器),因为操作图片啥的有可能引起的内存错误可能会把整个服务给down掉。

ps:
配合im4java使用的,也可以是GraphicsMagick,这是从ImageMagick的分支发展而来的图片处理工具,据它官网上自豪的说,它是处理图片的瑞士军刀(GraphicsMagick is the swiss army knife of image processing. ),看来相当值得一试。
ps2:
windows下使用的话,安装好GraphicsMagick后,需要重启电脑才行哦,这点问题也折腾了我一个小时,呵呵,给后来人一点提示。

评论

此博客中的热门博文

AVR Tutorial - Input / Output

EJB - Intro to Session beans

AVR ADC Analog to Digital Converter