简介:用于读、写、处理图片文件,支持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/
- public class ImageTools {
-
-
-
-
- public static String imageMagickPath = null;
- static {
-
-
-
-
- Properties prop = new PropertiesFile().getPropertiesFile();
-
- imageMagickPath = prop.getProperty("imageMagickPath");
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void cutImage(String srcPath, String newPath, int x, int y, int x1, int y1) throws Exception {
- int width = x1 - x;
- int height = y1 - y;
- IMOperation op = new IMOperation();
- op.addImage(srcPath);
-
-
-
-
-
-
- op.crop(width, height, x, y);
- op.addImage(newPath);
- ConvertCmd convert = new ConvertCmd();
-
-
- convert.setSearchPath(imageMagickPath);
-
- convert.run(op);
- }
-
-
-
-
-
-
-
-
-
- public static void cutImage(int width, int height, String srcPath, String newPath) throws Exception {
- IMOperation op = new IMOperation();
- op.addImage(srcPath);
- op.resize(width, height);
- op.addImage(newPath);
- ConvertCmd convert = new ConvertCmd();
-
- convert.setSearchPath(imageMagickPath);
- convert.run(op);
-
- }
-
-
-
-
-
-
-
-
- public static void cutImage(int width, String srcPath, String newPath) throws Exception {
- IMOperation op = new IMOperation();
- op.addImage(srcPath);
- op.resize(width, null);
- op.addImage(newPath);
- ConvertCmd convert = new ConvertCmd();
-
- convert.setSearchPath(imageMagickPath);
- convert.run(op);
- }
-
-
-
-
-
- public static void addImgText(String srcPath) throws Exception {
- IMOperation op = new IMOperation();
- op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8")
- .draw("text 5,5 juziku.com");
- op.addImage();
- op.addImage();
- ConvertCmd convert = new ConvertCmd();
-
- convert.setSearchPath(imageMagickPath);
- convert.run(op, srcPath, srcPath);
- }
-
- public static void main(String[] args) throws Exception {
-
-
- addImgText("//home//1.jpg");
- }
- }
注意事项:如果是在windows下运行,则需要配置ImageMagick的路径(现在很多安装程序都不需要设置,已经自动帮你设置好了):
在环境变量path中添加(C:\Program Files\ImageMagick-6.7.5-Q16;)
或者
- public static String imageMagickPath;
- Properties prop = new PropertiesFile().getPropertiesFile();
- imageMagickPath = prop.getProperty("imageMagickPath");
- ConvertCmd convert = new ConvertCmd();
- convert.setSearchPath(imageMagickPath);
在config.properties文件里了,内容如下所示: imageMagickPath=C:\\Program Files\\ImageMagick-6.7.5-Q16;
如果是在linux平台下,千万不需要配置,设置了会报错。
|