ImageMagick和im4java处理图片[转]

注意事项:如果是在windows下运行,则需要配置ImageMagick的路径: 
我是配置在config.properties文件里了,内容如下所示: 
imageMagickPath=C:\\ImageMagick-6.6.5-Q16 
如果是在linux平台下,则不需要配置。

import javax.servlet.http.HttpServletRequest;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

/**
 * ImageMagick和im4java处理图片
 * @author sunlightcs
 * 2011-6-1
 * http://hi.juziku.com/sunlightcs/
 */
public class ImageTools {
	
	/**
	 * ImageMagick的路径
	 */
	//public static String imageMagickPath = null;
	
	static{
		/**
		 * 获取ImageMagick的路径
		 */
		//Properties prop = new PropertiesFile().getPropertiesFile();
		//linux下不要设置此值,不然会报错
		//imageMagickPath = prop.getProperty("imageMagickPath");	
	}
	
	
	/**
	 * 根据坐标裁剪图片
	 * 
	 * @param srcPath   要裁剪图片的路径
	 * @param newPath   裁剪图片后的路径
	 * @param x   起始横坐标
	 * @param y   起始挫坐标
	 * @param x1  结束横坐标
	 * @param y1  结束挫坐标
	 */
	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);
		
		/**
		 * width:裁剪的宽度
		 * height:裁剪的高度
		 * x:裁剪的横坐标
		 * y:裁剪的挫坐标
		 */
		op.crop(width, height, x, y);
		
		op.addImage(newPath);
		
		ConvertCmd convert = new ConvertCmd();
		
		//linux下不要设置此值,不然会报错
		//convert.setSearchPath(imageMagickPath);
		

		convert.run(op);
	}
	
	/**
	 * 根据尺寸缩放图片
	 * @param width  缩放后的图片宽度
	 * @param height  缩放后的图片高度
	 * @param srcPath   源图片路径
	 * @param newPath   缩放后图片的路径
	 */
	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();
		
		//linux下不要设置此值,不然会报错
		//convert.setSearchPath(imageMagickPath);
		
		convert.run(op);
	}
	
	
	/**
	 * 根据宽度缩放图片
	 * @param width  缩放后的图片宽度
	 * @param srcPath   源图片路径
	 * @param newPath   缩放后图片的路径
	 */
	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();
		
		//linux下不要设置此值,不然会报错
		//convert.setSearchPath(imageMagickPath);
		
		convert.run(op);
	}
	
	
	
	
	/**
	 * 给图片加水印
	 * @param srcPath   源图片路径
	 */
	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();
		
		//linux下不要设置此值,不然会报错
		//convert.setSearchPath(imageMagickPath);

		convert.run(op,srcPath,srcPath);
	}
	
	
	public static void main(String[] args) throws Exception{
		//cutImage("D:\\apple870.jpg", "D:\\apple870eee.jpg", 98, 48, 370, 320);
		//cutImage(200,300, "/home/steven/a.jpg", "/home/steven/aa.jpg");
		addImgText("//home//steven//a.jpg");
	}
}

posted @ 2011-06-28 09:25  `X.x|嚨  阅读(5506)  评论(1编辑  收藏  举报