java 大图片处理GraphicsMagick + im4java [缩放,旋转,裁剪]

java 大图片处理GraphicsMagick + im4java [缩放,旋转,裁剪]php

ImageMagickhtml

主页:http://www.imagemagick.org/script/index.phpjava

GraphicsMagickwindows

主页:http://www.graphicsmagick.org/api

两个图片处理软件我就不说了,由于我没那个评论的本事,其实这些软件都会有命令行的指令,而后咱们用java调用来对图片进行编辑,调用什么指令可能学一下才知道,不过咱们也不用本身写指令吧,由于别人已经封装好了那些指令的接口(JNI),下面就是那些JNI编辑器

jmagickide

主页:http://www.jmagick.org/index.html工具

下载地址:http://downloads.jmagick.org/测试

缺点:实地测试后发现,速度果真提升了很多,可是质量却大大降低了,在大量测试数据下,每生成100张图片约会有5张图片生成出现错误,还会出现down机的状况。 jmagick.net

im4java

主页:http://im4java.sourceforge.net/

下载地址:http://sourceforge.net/projects/im4java/files/

API:http://im4java.sourceforge.net/api/

im4java

用那个不用说吧,看更新时间,不知道大家会选择什么

因此我选用了 GraphicsMagick +im4java

下载GraphicsMagick http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.17/ 其实他就是一个软件,而后windows版本会自动将安装目录加入到path变量,因此在指令窗口能够调用,即安装好就无论它了

下载im4java来方便咱们在java调用 http://sourceforge.net/projects/im4java/files/

而后将里面的im4java-1.3.2加入到咱们的项目引用里面就能够了 jar包

根据别人的代码,本身再封装了一下接口吧,基本够用,那些水印的就没加上去了,如今不须要 工具很强大,都是靠指令,因此多看看API和软件自己的使用吧

好吧,我忽悠完了,把时间交给大家了. 附上代码:[好纠结的代码编辑器,只能这样了]

<!-- lang: java -->
package test;

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList;

import org.im4java.core.ConvertCmd; import org.im4java.core.IM4JavaException; import org.im4java.core.IMOperation; import org.im4java.core.IdentifyCmd; import org.im4java.process.ArrayListOutputConsumer;

public class TestGm { /** * * 得到图片文件大小[小技巧来得到图片大小] * * @param filePath * 文件路径 * * * @return 文件大小 */

public int getSize(String imagePath) {
	int size = 0;
	FileInputStream inputStream = null;
	try {
		inputStream = new FileInputStream(imagePath);
		size = inputStream.available();
		inputStream.close();
		inputStream = null;
	} catch (FileNotFoundException e) {
		size = 0;
		System.out.println("文件未找到!");
	} catch (IOException e) {
		size = 0;
		System.out.println("读取文件大小错误!");
	} finally {
		// 可能异常为关闭输入流,因此须要关闭输入流
		if (null != inputStream) {
			try {
				inputStream.close();
			} catch (IOException e) {
				System.out.println("关闭文件读入流异常");
			}
			inputStream = null;

		}
	}
	return size;
}

/**
 * 得到图片的宽度
 * 
 * @param filePath
 *            文件路径
 * @return 图片宽度
 */
public int getWidth(String imagePath) {
	int line = 0;
	try {
		IMOperation op = new IMOperation();
		op.format("%w"); // 设置获取宽度参数
		op.addImage(1);
		IdentifyCmd identifyCmd = new IdentifyCmd(true);
		ArrayListOutputConsumer output = new ArrayListOutputConsumer();
		identifyCmd.setOutputConsumer(output);
		identifyCmd.run(op, imagePath);
		ArrayList<String> cmdOutput = output.getOutput();
		assert cmdOutput.size() == 1;
		line = Integer.parseInt(cmdOutput.get(0));
	} catch (Exception e) {
		line = 0;
		System.out.println("运行指令出错!");
	}
	return line;
}

/**
 * 得到图片的高度
 * 
 * @param imagePath
 *            文件路径
 * @return 图片高度
 */
public int getHeight(String imagePath) {
	int line = 0;
	try {
		IMOperation op = new IMOperation();

		op.format("%h"); // 设置获取高度参数
		op.addImage(1);
		IdentifyCmd identifyCmd = new IdentifyCmd(true);
		ArrayListOutputConsumer output = new ArrayListOutputConsumer();
		identifyCmd.setOutputConsumer(output);
		identifyCmd.run(op, imagePath);
		ArrayList<String> cmdOutput = output.getOutput();
		assert cmdOutput.size() == 1;
		line = Integer.parseInt(cmdOutput.get(0));
	} catch (Exception e) {
		line = 0;
		System.out.println("运行指令出错!"+e.toString());
	}
	return line;
}

/**
 * 图片信息
 * 
 * @param imagePath
 * @return
 */
public static String getImageInfo(String imagePath) {
	String line = null;
	try {
		IMOperation op = new IMOperation();
		op.format("width:%w,height:%h,path:%d%f,size:%b%[EXIF:DateTimeOriginal]");
		op.addImage(1);
		IdentifyCmd identifyCmd = new IdentifyCmd(true);
		ArrayListOutputConsumer output = new ArrayListOutputConsumer();
		identifyCmd.setOutputConsumer(output);
		identifyCmd.run(op, imagePath);
		ArrayList<String> cmdOutput = output.getOutput();
		assert cmdOutput.size() == 1;
		line = cmdOutput.get(0);

	} catch (Exception e) {
		e.printStackTrace();
	}
	return line;
}

/**
 * 裁剪图片
 * 
 * @param imagePath
 *            源图片路径
 * @param newPath
 *            处理后图片路径
 * @param x
 *            起始X坐标
 * @param y
 *            起始Y坐标
 * @param width
 *            裁剪宽度
 * @param height
 *            裁剪高度
 * @return 返回true说明裁剪成功,不然失败
 */
public boolean cutImage(String imagePath, String newPath, int x, int y,
		int width, int height) {
	boolean flag = false;
	try {
		IMOperation op = new IMOperation();
		op.addImage(imagePath);
		/** width:裁剪的宽度 * height:裁剪的高度 * x:裁剪的横坐标 * y:裁剪纵坐标 */
		op.crop(width, height, x, y);
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd(true);
		convert.run(op);
		flag = true;
	} catch (IOException e) {
		System.out.println("文件读取错误!");
		flag = false;
	} catch (InterruptedException e) {
		flag = false;
	} catch (IM4JavaException e) {
		flag = false;
	} finally {

	}
	return flag;
}

/**
 * 根据尺寸缩放图片[等比例缩放:参数height为null,按宽度缩放比例缩放;参数width为null,按高度缩放比例缩放]
 * 
 * @param imagePath
 *            源图片路径
 * @param newPath
 *            处理后图片路径
 * @param width
 *            缩放后的图片宽度
 * @param height
 *            缩放后的图片高度
 * @return 返回true说明缩放成功,不然失败
 */
public boolean zoomImage(String imagePath, String newPath, Integer width,
		Integer height) {

	boolean flag = false;
	try {
		IMOperation op = new IMOperation();
		op.addImage(imagePath);
		if (width == null) {// 根据高度缩放图片
			op.resize(null, height);
		} else if (height == null) {// 根据宽度缩放图片
			op.resize(width);
		} else {
			op.resize(width, height);
		}
		op.addImage(newPath);
		ConvertCmd convert = new ConvertCmd(true);
		convert.run(op);
		flag = true;
	} catch (IOException e) {
		System.out.println("文件读取错误!");
		flag = false;
	} catch (InterruptedException e) {
		flag = false;
	} catch (IM4JavaException e) {
		flag = false;
	} finally {

	}
	return flag;
}

/**
 * 图片旋转
 * 
 * @param imagePath
 *            源图片路径
 * @param newPath
 *            处理后图片路径
 * @param degree
 *            旋转角度
 */
public boolean rotate(String imagePath, String newPath, double degree) {
	boolean flag = false;
	try {
		// 1.将角度转换到0-360度之间
		degree = degree % 360;
		if (degree <= 0) {
			degree = 360 + degree;
		}
		IMOperation op = new IMOperation();
		op.addImage(imagePath);
		op.rotate(degree);
		op.addImage(newPath);
		ConvertCmd cmd = new ConvertCmd(true);
		cmd.run(op);
		flag = true;
	} catch (Exception e) {
		flag = false;
		System.out.println("图片旋转失败!");
	}
	return flag;
}

public static void main(String[] args) throws Exception {
	TestGm imageUtil = new TestGm();
	
	System.out.println("原图片大小:" + imageUtil.getSize("d://test.jpg") + "Bit");
	System.out.println("原图片宽度:" + imageUtil.getWidth("d://test.jpg"));
	System.out.println("原图片高度:" + imageUtil.getHeight("d://test.jpg"));
	if (imageUtil.zoomImage("d://test.jpg", "d://test1.jpg", 500, null)) {
		if (imageUtil.rotate("d://test.jpg", "d://test2.jpg", 15)) {
			if (imageUtil.cutImage("d://test2.jpg", "d://test3.jpg", 32,
					105, 200, 200)) {
				System.out.println("编辑成功");
			} else {
				System.out.println("编辑失败03");
			}
		} else {
			System.out.println("编辑失败02");
		}
	} else {
		System.out.println("编辑失败01");
	}
	
}

}

相关文章:

ImageMagick图片处理工具的安装 --- http://elf8848.iteye.com/blog/455675

选择ImageMagick仍是GraphicsMagick(翻译)--- http://co63oc.blog.51cto.com/904636/328997

相关文章
相关标签/搜索