im4java使用总结

1.首先需要安装ImageMagick-6.8.4-Q16

2.

package img4java;

import java.io.IOException;

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.im4java.core.Info;
import org.im4java.core.InfoException;
import org.im4java.core.MogrifyCmd;
import org.im4java.process.ProcessStarter;

public class TestImg4Java {
		
		private final static String myPath="D:\\Program Files (x86)\\ImageMagick-6.8.4-Q16";
	
		static{
			ProcessStarter.setGlobalSearchPath(myPath);
		}
		
		/**
		 * 裁剪图片
		 * @throws IOException
		 * @throws InterruptedException
		 * @throws IM4JavaException
		 */
		public void cropImg() throws IOException, InterruptedException, IM4JavaException{
			ConvertCmd cmd = new ConvertCmd();
			IMOperation opertation = new IMOperation();
			opertation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\5.jpg");
			//宽  高 起点横坐标 起点纵坐标
			opertation.crop(400, 300, 34, 100);
			opertation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\img\\5.jpg");
			cmd.run(opertation);
		}
		
		/**
		 * 得到图片的信息
		 * @throws InfoException
		 */
		public void getImgInfo() throws InfoException{
			Info info = new Info("C:\\Users\\zyl\\Desktop\\imgs\\1.jpg");
			System.out.println(info.getImageHeight());
			System.out.println(info.getImageWidth());
		}
		
		/**
		 * 等比缩放图片
		 * @throws IOException
		 * @throws InterruptedException
		 * @throws IM4JavaException
		 */
		public void resizeImg() throws IOException, InterruptedException, IM4JavaException{
			ConvertCmd cmd = new ConvertCmd();
			IMOperation opertion = new IMOperation();
			opertion.addImage("C:\\Users\\zyl\\Desktop\\imgs\\1.jpg");
			//等比缩放图片
			opertion.resize(400, 400);
			opertion.addImage("C:\\Users\\zyl\\Desktop\\imgs\\img\\1.jpg");
			cmd.run(opertion);
		}
		
		/**
		 * 旋转图片
		 * @throws IOException
		 * @throws InterruptedException
		 * @throws IM4JavaException
		 */
		public void rotateImg() throws IOException, InterruptedException, IM4JavaException{
			ConvertCmd cmd = new ConvertCmd();
			IMOperation operation = new IMOperation();
			operation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\2.jpg");
			operation.rotate(90.0);
			operation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\2.jpg");
			cmd.run(operation);
		}
		
		/**
		 * 将图片编程黑白图片
		 * @throws IOException
		 * @throws InterruptedException
		 * @throws IM4JavaException
		 */
		public void monochrome() throws IOException, InterruptedException, IM4JavaException{
			ConvertCmd cmd = new ConvertCmd();
			IMOperation operation = new IMOperation();
			operation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\3.jpg");
			operation.monochrome();
			operation.addImage("C:\\Users\\zyl\\Desktop\\imgs\\3.jpg");
			cmd.run(operation);
		}
		
		public void annotate() throws IOException, InterruptedException, IM4JavaException{
			MogrifyCmd cmd = new MogrifyCmd();
			IMOperation operation = new IMOperation();
			operation.encoding("UTF-8");
			//gravity 设置方位 NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast   annotate 设置偏移量
			operation.font("C:\\Windows\\Fonts\\simsun.ttc").gravity("CENTER").pointsize(40).fill("red").annotate(10,10,10,10,"测试水印");
			operation.addImage();
			cmd.run(operation,"C:\\Users\\zyl\\Desktop\\imgs\\1.jpg");
		}
		
		public static void main(String[] args) throws IOException, InterruptedException, IM4JavaException{
			TestImg4Java test = new TestImg4Java();
//			test.getImgInfo();
//			test.resizeImg();
//			test.rotateImg();
//			test.monochrome();
			test.annotate();
		}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值