码迷,mamicode.com
首页 > 编程语言 > 详细

java 合并图片:将多个图片竖向合并在一起

时间:2015-04-24 10:36:00      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:合并   图片   java   rgb   buffer   

代码:

/**
 * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同
 * 
 * @param piclist
 *            文件路径列表
 * @param outPath
 *            输出路径
 */
public static void yPic(List<String> piclist, String outPath) {// 纵向处理图片
	if (piclist == null || piclist.size() <= 0) {
		System.out.println("图片数组为空!");
		return;
	}
	try {
		int height = 0, // 总高度
		width = 0, // 总宽度
		_height = 0, // 临时的高度 , 或保存偏移高度
		__height = 0, // 临时的高度,主要保存每个高度
		picNum = piclist.size();// 图片的数量
		File fileImg = null; // 保存读取出的图片
		int[] heightArray = new int[picNum]; // 保存每个文件的高度
		BufferedImage buffer = null; // 保存图片流
		List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB
		int[] _imgRGB; // 保存一张图片中的RGB数据
		for (int i = 0; i < picNum; i++) {
			fileImg = new File(piclist.get(i));
			buffer = ImageIO.read(fileImg);
			heightArray[i] = _height = buffer.getHeight();// 图片高度
			if (i == 0) {
				width = buffer.getWidth();// 图片宽度
			}
			height += _height; // 获取总高度
			_imgRGB = new int[width * _height];// 从图片中读取RGB
			_imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width);
			imgRGB.add(_imgRGB);
		}
		_height = 0; // 设置偏移高度为0
		// 生成新图片
		BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int i = 0; i < picNum; i++) {
			__height = heightArray[i];
			if (i != 0) _height += __height; // 计算偏移高度
			imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中
		}
		File outFile = new File(outPath);
		ImageIO.write(imageResult, "jpg", outFile);// 写图片
	} catch (Exception e) {
		e.printStackTrace();
	}
}


调用:

public static void main(String[] args) {
	List<String> list = new ArrayList<String>();
	list.add("f:/bak/image/1.jpg");
	list.add("f:/bak/image/2.jpg");
	list.add("f:/bak/image/1.jpg");
	yPic(list, "f:/bak/image/3.jpg");
}



以后再慢慢写别的


java 合并图片:将多个图片竖向合并在一起

标签:合并   图片   java   rgb   buffer   

原文地址:http://blog.csdn.net/u013934914/article/details/45242873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!