Jmagick java 处理图片

jopen 10年前

在处理图片的时候用到jmagick,起初以为引入一个pom依赖,就可以用java代码直接操作了,谁知道一直报no JMagick in java.library.path错误。后来在网上查了一番资料发现,并不是那样的,jmagick只是imagemagick的一个java封装,imagemagick是用c++编写的,所以这就用到了java的JNI进行本地库的调用。

1.jmagick下载地址:http://downloads.jmagick.org/6.4.0 (目前最新版本是6.4.0),不过说实话里面只有doc,没有demo,如果没有资料,还真不知道怎么用,还好google够强大,搜出了前辈们是怎么操作的。
2.imagemagick Windows下载地址:http://www.imagemagick.org/script/binary-releases.php#windows

这里我下的是Jmagick java 处理图片

下好,解压,将.DLL文件访问System32的文件里,在把jar放入项目里,在测试当前类的时候写一个静态块用来初始化java库,System.setProperty(“jmagick.systemclassloader”, “no”);
下面上传下图片处理的java代码:

/**处理图片 */  public static void processImage(File source, File dest, double width, double height)                                                                                      throws MagickException {      MagickImage sourceImage = null;      MagickImage destImage = null;      try {          ImageInfo info = new ImageInfo(source.getPath());          //设置图片质量          info.setQuality(IMG_QUALITY);          sourceImage = new MagickImage(info);          Dimension dimension = sourceImage.getDimension();            Double sourceWidth = dimension.getWidth();          Double sourceHeight = dimension.getHeight();          Double destWidth = null;          Double destHeight = null;            if (!dest.getParentFile().exists()) {              dest.getParentFile().mkdirs();          }          if (width == 0 && height == 0) {              width = sourceWidth;              height = sourceHeight;          }          if (sourceWidth > sourceHeight) {              destWidth = width;              destHeight = width / sourceWidth * sourceHeight;              if (destHeight > height && height != 0) {                  destHeight = height;                  destWidth = height / sourceHeight * sourceWidth;              }          } else {              destHeight = height;              destWidth = height / sourceHeight * sourceWidth;              if (destWidth > width || height == 0) {                  destWidth = width;                  destHeight = width / sourceWidth * sourceHeight;              }          }          destImage = sourceImage.zoomImage(destWidth.intValue(), destHeight.intValue());          // .sharpenImage(SHARPEN_IMG_RADUIS, SHARPEN_IMG_SIGMA);          //水平分辨率DPI          destImage.setXResolution(IMG_XRES);          //垂直分辨率DPI          destImage.setYResolution(IMG_YRES);          destImage.profileImage("*", null);          //设置图片压缩          destImage.setCompression(CompressionType.JPEGCompression);          //设置对比度          destImage.contrastImage(true);          destImage.setFileName(dest.getPath());          destImage.writeImage(info);          info = null;      } finally {          if (destImage != null) {              destImage.destroyImages();          }          if (sourceImage != null) {              sourceImage.destroyImages();          }      }  }

这样在windows 32bit下面,用java处理jmagick就完成了。但是有个问题,如果你是64bitjdk的话就有问题,Can't load IA 32-bit .dll on a AMD 64-bit platform这个错误就会出现,经过搜索,说是卸掉64bit换个32bit就可以了。但是我觉得有点不合适,所以又查了下,有个im4java,是imagemagick的另外一个java接口,im4java只是封装ImageMagick的命令,所以不需要依赖dll,记录完linux的实现方式之后,在回来说xiaim4java。

linux安装:下载ImageMagick源码包解压

tar zvxf ImageMagick-6.4.0-0.tar.gz    cd ImageMagick-6.4.0-0    ./configure --prefix=/opt360/ImageMagick-6.4.0 --with-quantum-depth=8   --enable-share

configure参数说明

–enable-shared 编译成共享库
–disable-static 不编译成静态库
–disable-openmp 禁用多线程,使用多线程性能并没有提高,但CPU占用达到了100%,所以禁用了。
–with-quantum-depth=8 使用8位色深。我的1200万像素数码相机,照出的图片就是8位色深。
–with-windows-font-dir=目录 ,指明字体文件的目录(后面将人工复制中文字体文件到这个目录)
关于Q8,Q16,Q32的说明:(建议使用8,现在数码相机照的相片,每一种颜色就是8位深,3种颜色就是24位,所以选8)
支持的参数有3种,分别是 8, 16, or 32。默认值是16。
Q8表示: 8-bits per pixel quantum
Q16表示:16-bits per pixel quantum
Q32表示:32-bits per pixel quantum
安装命令

make    make install

计算方式
使用16-bit per pixel quantums在处理图片时比8-bit慢15%至50%,并须要更多的内存。
处理一张1024x768像素的图片8-bit要使用3.6M内存,16-bit要使用7.2M内存。计算方法是: (5 * Quantum Depth * Rows * Columns) / 8
验证支持的图片格式:identify -list format
安装jmagick
修改文件执行权限:chmod -R 777 filename

tar zvxf jmagick-6.4.0-src.tar.gz    cd jmagick-6.4.0-src    ./configure --prefix=/opt360/jmagick-6.4.0 --with-java-home=/home/jdk/jdk1.6.0_12 -with-magick-home=/opt360/ImageMagick-6.4.0    make all    make install

记住jmagick-6.4.0.jar是使用前面你指定的jdk1.6编译出来的,所以不能在jdk为1.5的环境下使用jmagick-6.4.0.jar
下面java代码跟windows是一样的。

参考以下文章:windows、linux利用jmagick处理图片im4java与jmagick比较