1、安装GraphicsMagick
2、项目引入Im4java的jar
org.im4java
1.4.0
代码样例:
/**
* 旋转
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
public static void rotate() throws IOException, InterruptedException, IM4JavaException{
IMOperation op = new IMOperation();
op.addImage("d:\\a.jpg");
op.rotate(45d);
op.addImage("d:\\efg.jpg");
ConvertCmd cmd = new ConvertCmd(true);
cmd.setSearchPath("D:\\GraphicsMagick-1.3.18-Q8");
cmd.run(op);
}
/**
* 裁剪图片
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
public static void cutPic() throws IOException, InterruptedException, IM4JavaException{
IMOperation op = new IMOperation();
op.addImage("d:\\a.jpg");
op.crop(200, 200, 20, 20);
op.addImage("d:\\efg.jpg");
ConvertCmd cmd = new ConvertCmd(true);
cmd.setSearchPath("D:\\GraphicsMagick-1.3.18-Q8");
cmd.run(op);
}
/**
* 获取图片信息
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
public static void getInfo() throws IOException, InterruptedException, IM4JavaException{
IMOperation op = new IMOperation();
op.format("%w,%h,%d,%f,%b");
op.addImage("d://test.jpg");
IdentifyCmd cmd = new IdentifyCmd(true);
cmd.setSearchPath("D:\\GraphicsMagick-1.3.18-Q8");
ArrayListOutputConsumer output = new ArrayListOutputConsumer();
cmd.setOutputConsumer(output);
cmd.run(op);
ArrayList cmdOutput = output.getOutput();
if (cmdOutput.size() != 1) return;
String line = cmdOutput.get(0);
System.out.println(line);
}
/**
* 等比例缩放
* @throws IOException
* @throws InterruptedException
* @throws IM4JavaException
*/
public static void equalScaling() throws IOException, InterruptedException, IM4JavaException{
GMOperation op = new GMOperation();
op.resize(5000);
op.addImage("d://a.jpg");
op.addImage("d://efgg.jpg");
ConvertCmd cmd = new ConvertCmd(true);
cmd.setSearchPath("D:\\GraphicsMagick-1.3.18-Q8");
cmd.run(op);
}