下载 .tar.gz 的源码包,进行解压
tar -xvzf GraphicsMagick-1.3.12.tar.gz
解压后,原来在的gz文件就变成了tar文件,进入文件夹
cd GraphicsMagick-1.3.12
安装之前,因为是图片处理,所以需要系统中安装了libpng和libjpeg的开发包,否则的话不会安装这两种文件的支持。
使用 configure 来进行自动的配置、build和安装
./configure
当然,可以通过 –prefix=PATH 来指定参数,还可以指定其他编译时的变量,这里使用了一个经过测试的 configure 配置,同时添加了 enable-sybol-prefix ,这样就避免了和系统中已有的 ImageMagick 的冲突,下面是完成的配置参数:
./configure '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr/local/sinasrv2' '--exec-prefix=/usr/local/sinasrv2' '--bindir=/usr/local/sinasrv2/bin' '--sbindir=/usr/local/sinasrv2/sbin' '--sysconfdir=/usr/local/sinasrv2/etc' '--datadir=/usr/local/sinasrv2/share' '--includedir=/usr/local/sinasrv2/include' '--libdir=/usr/local/sinasrv2/lib' '--libexecdir=/usr/local/sinasrv2/libexec' '--localstatedir=/usr/local/sinasrv2/var' '--sharedstatedir=/usr/local/sinasrv2/share/com' '--mandir=/usr/local/sinasrv2/share/man' '--infodir=/usr/local/sinasrv2/share/info' '--enable-libtool-verbose' '--with-included-ltdl' '--enable-shared' '--disable-static' '--with-modules' '--with-frozenpaths' '--without-perl' '--without-magick-plus-plus' '--with-quantum-depth=8' --enable-symbol-prefix
接下来就是安装
make
make install
程序中代码:
001 package imageUtils;
002
003 import java.io.IOException;
004 import java.util.ArrayList;
005
006 import org.im4java.core.ConvertCmd;
007 import org.im4java.core.IMOperation;
008
009 /**
010 * @author hegh E-mail: heguanhua@tjhq.com
011 * @version 创建时间:Mar 13, 2012 10:43:12 AM 类说明
012 */
013 public class ImageMagick {
014
015 /** * ImageMagick的路径 */
016 publicstatic String imageMagickPath =null;
017 static{/**获取ImageMagick的路径 */
018 //Properties prop = new PropertiesFile().getPropertiesFile();
019 //linux下不要设置此值,不然会报错
020 //imageMagickPath = prop.getProperty("imageMagickPath");
021 }
022
023 /** * 根据坐标裁剪图片
024 * @param srcPath 要裁剪图片的路径
025 * @param newPath 裁剪图片后的路径
026 * @param x 起始横坐标
027 * @param y 起始挫坐标
028 * @param x1 结束横坐标
029 * @param y1 结束挫坐标
030 */
031 publicstatic void cutImage(String srcPath, String newPath, intx, int y, int x1, int y1)
032 throwsException {
033 intwidth = x1 - x; intheight = y1 - y;
034 IMOperation op =new IMOperation();
035 op.addImage(srcPath);
036 /**
037 * width:裁剪的宽度
038 * height:裁剪的高度
039 * x:裁剪的横坐标
040 * y:裁剪的挫坐标
041 */
042 op.crop(width, height, x, y);
043 op.addImage(newPath);
044 ConvertCmd convert =new ConvertCmd();
045 //linux下不要设置此值,不然会报错
046 //convert.setSearchPath(imageMagickPath);
047 convert.run(op);
048 }
049
050 /**
051 * 根据尺寸缩放图片
052 * @param width 缩放后的图片宽度
053 * @param height 缩放后的图片高度
054 * @param srcPath 源图片路径
055 * @param newPath 缩放后图片的路径
056 * @param type 1为比例处理,2为大小处理,如(比例:1024x1024,大小:50%x50%)
057 */
058 publicstatic String cutImage(intwidth, int height, String srcPath, String newPath,inttype,String quality) throwsException {
059 IMOperation op =new IMOperation();
060 ConvertCmd cmd =new ConvertCmd(true);
061 op.addImage();
062 String raw ="";
063 if(type ==1){
064 //按像素
065 raw = width+"x"+height+"^";
066 }else{
067 //按像素百分比
068 raw = width+"%x"+height+"%";
069 }
070 op.addRawArgs("-sample", raw );
071 if((quality !=null&& quality.equals(""))){
072 op.addRawArgs("-quality", quality );
073 }
074 op.addImage();
075
076 String osName = System.getProperty("os.name").toLowerCase();
077 if(osName.indexOf("win") != -1) {
078 //linux下不要设置此值,不然会报错
079 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16");
080 }
081
082 try{
083 cmd.run(op, srcPath, newPath);
084 }catch(Exception e){
085 e.printStackTrace();
086 }
087 returnnewPath;
088 }
089
090
091 /**
092 * 给图片加水印
093 * @param srcPath 源图片路径
094 */
095 publicstatic void addImgText(String srcPath) throws Exception {
096 IMOperation op =new IMOperation();
097 op.font("宋体").gravity("southeast").pointsize(18).fill("#BCBFC8").draw("text 100,100 co188.com");
098 op.addImage();
099 op.addImage();
100
101 String osName = System.getProperty("os.name").toLowerCase();
102 ConvertCmd cmd =new ConvertCmd(true);
103 if(osName.indexOf("win") != -1) {
104 //linux下不要设置此值,不然会报错
105 cmd.setSearchPath("C://Program Files//GraphicsMagick-1.3.14-Q16");
106 }
107
108 try{
109 cmd.run(op, srcPath, srcPath);
110 }catch(Exception e){
111 e.printStackTrace();
112 }
113 }
114
115 publicstatic void main(String[] args) throws Exception{
116 //cutImage("D:\\apple870.jpg", "D:\\apple870eee.jpg",98, 48, 370, 320);
117 Long start = System.currentTimeMillis();
118 //cutImage(100,100, "e:\\37AF7D10F2D8448A9A5.jpg","e:\\37AF7D10F2D8448A9A5_bak2.jpg",2,"100");
119 addImgText("e:\\37AF7D10F2D8448A9A5_bak2.jpg");
120 Long end = System.currentTimeMillis();
121 System.out.println("time:"+(end-start)/3600);
122 }
123 }
124 通过GraphicsMagick+im4java实现高质量大图的处理,解决了100M以上,以及图片像素10000以上处理是出现内存溢出的问