当前位置: 首页 > 工具软件 > im4java > 使用案例 >

im4java使用_im4java使用总结

阎修杰
2023-12-01

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();

}

}

 类似资料: