分享

ImageMagick使用for java(im4java)

 旭龙 2013-04-18

简介:用于读、写、处理图片文件,支持89种格式的图片文件,利用imageMagick可以根据web应用程序动态生成图片,也可以将一个或者一组图片改变大小、旋转、锐化、减色、增加特效等操作,并对操作结果进行保存(可以设置保存格式)。ImageMagick是免费软件:全部源码开放,可以自由使用,复制,修改,发布。

ImageMagick命令:http://wenku.baidu.com/view/078062b069dc5022aaea007f.html

各种语言接口:(见http://www./script/api.php

Ada:G2F         C:MagickWand 和MagickCore          Ch:ChMagick          COM+:ImageMagickObject      C++:Magick++          java:JMagick和Im4java

开源中国社区中源码查看:http://www.oschina.net/code/explore/ImageMagick-6.6.6-6

使用方法:首先要安装ImageMagick这个工具,安装好这个工具后,再下载im4java包放到项目lib目录里就行了。

ImageMagick  java 接口(im4java api):http://im4java./api/   (如果进不了就从http://www./script/api.php =>java=>im4java 进入)

im4java源码下载:http:///projects/sfnet_im4java/downloads/im4java-1.3.2/im4java-1.3.2-src.tar.bz2/

  1. public class ImageTools {  
  2.   
  3.     /** 
  4.      * ImageMagick的路径 
  5.      */  
  6.     public static String imageMagickPath = null;  
  7.     static {  
  8.         /** 
  9.          *  
  10.          * 获取ImageMagick的路径 
  11.          */  
  12.         Properties prop = new PropertiesFile().getPropertiesFile();  
  13.         //linux下不要设置此值,不然会报错  
  14.         imageMagickPath = prop.getProperty("imageMagickPath");  
  15.     }  
  16.   
  17.     /** 
  18.      *  
  19.      * 根据坐标裁剪图片 
  20.      *  
  21.      * @param srcPath   要裁剪图片的路径 
  22.      * @param newPath   裁剪图片后的路径 
  23.      * @param x         起始横坐标 
  24.      * @param y         起始纵坐标 
  25.      * @param x1        结束横坐标 
  26.      * @param y1        结束纵坐标 
  27.      */  
  28.   
  29.     public static void cutImage(String srcPath, String newPath, int x, int y, int x1,   int y1) throws Exception {  
  30.         int width = x1 - x;  
  31.         int height = y1 - y;  
  32.         IMOperation op = new IMOperation();  
  33.         op.addImage(srcPath);  
  34.         /** 
  35.          * width:  裁剪的宽度 
  36.          * height: 裁剪的高度 
  37.          * x:       裁剪的横坐标 
  38.          * y:       裁剪的挫坐标 
  39.          */  
  40.         op.crop(width, height, x, y);  
  41.         op.addImage(newPath);  
  42.         ConvertCmd convert = new ConvertCmd();  
  43.   
  44.         // linux下不要设置此值,不然会报错  
  45.         convert.setSearchPath(imageMagickPath);  
  46.   
  47.         convert.run(op);  
  48.     }  
  49.   
  50.     /** 
  51.      *  
  52.      * 根据尺寸缩放图片 
  53.      * @param width             缩放后的图片宽度 
  54.      * @param height            缩放后的图片高度 
  55.      * @param srcPath           源图片路径 
  56.      * @param newPath           缩放后图片的路径 
  57.      */  
  58.     public static void cutImage(int width, int height, String srcPath,  String newPath) throws Exception {  
  59.         IMOperation op = new IMOperation();  
  60.         op.addImage(srcPath);  
  61.         op.resize(width, height);  
  62.         op.addImage(newPath);  
  63.         ConvertCmd convert = new ConvertCmd();  
  64.         // linux下不要设置此值,不然会报错  
  65.         convert.setSearchPath(imageMagickPath);  
  66.         convert.run(op);  
  67.   
  68.     }  
  69.   
  70.     /** 
  71.      * 根据宽度缩放图片 
  72.      *  
  73.      * @param width            缩放后的图片宽度 
  74.      * @param srcPath          源图片路径 
  75.      * @param newPath          缩放后图片的路径 
  76.      */  
  77.     public static void cutImage(int width, String srcPath, String newPath)  throws Exception {  
  78.         IMOperation op = new IMOperation();  
  79.         op.addImage(srcPath);  
  80.         op.resize(width, null);  
  81.         op.addImage(newPath);  
  82.         ConvertCmd convert = new ConvertCmd();  
  83.         // linux下不要设置此值,不然会报错  
  84.         convert.setSearchPath(imageMagickPath);  
  85.         convert.run(op);  
  86.     }  
  87.   
  88.     /** 
  89.      * 给图片加水印 
  90.      * @param srcPath            源图片路径 
  91.      */  
  92.     public static void addImgText(String srcPath) throws Exception {  
  93.         IMOperation op = new IMOperation();  
  94.         op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")  
  95.                 .draw("text 5,5 juziku.com");  
  96.         op.addImage();  
  97.         op.addImage();  
  98.         ConvertCmd convert = new ConvertCmd();  
  99.         // linux下不要设置此值,不然会报错  
  100.         convert.setSearchPath(imageMagickPath);  
  101.         convert.run(op, srcPath, srcPath);  
  102.     }  
  103.   
  104.     public static void main(String[] args) throws Exception {  
  105.         // cutImage("D:\\test.jpg", "D:\\new.jpg", 98, 48, 370,320);  
  106.         // cutImage(200,300, "/home/1.jpg", "/home/2.jpg");  
  107.         addImgText("//home//1.jpg");  
  108.     }  
  109. }  


 

注意事项:如果是在windows下运行,则需要配置ImageMagick的路径(现在很多安装程序都不需要设置,已经自动帮你设置好了):

在环境变量path中添加(C:\Program Files\ImageMagick-6.7.5-Q16;) 

或者

  1. public static String imageMagickPath;  
  2. Properties prop = new PropertiesFile().getPropertiesFile();   
  3. imageMagickPath = prop.getProperty("imageMagickPath");  
  4. ConvertCmd convert = new ConvertCmd();  
  5. convert.setSearchPath(imageMagickPath);   

在config.properties文件里了,内容如下所示: imageMagickPath=C:\\Program Files\\ImageMagick-6.7.5-Q16;

如果是在linux平台下,千万不需要配置,设置了会报错。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多