java 实现横竖向合成图片实例代码

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.zhuoshi.jcbigdata.util.Img2Base64;
/**
 * @author lx
 * @describe 图片合成
 *
 */

public class picCompose {
	    //横向合成图片
		public static void transverseJointPic(List<File> files, String path) {   
			try {
				Integer allWidth = 0;	// 图片总宽度
				Integer allHeight = 0;	// 图片总高度
				List<BufferedImage> imgs = new ArrayList<>(); 
				for(int i=0; i<files.size(); i++){
					imgs.add(ImageIO.read(files.get(i)));
					//横向
				if (i==0) {
					allHeight = imgs.get(0).getHeight();
				}
				allWidth += imgs.get(i).getWidth();
				}
		        BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);  
		        // paint both images, preserving the alpha channels  
		        Graphics g = combined.getGraphics();
		   
		     // 横向合成
		        Integer width = 0;
		        for(int i=0; i< imgs.size(); i++){
		        	g.drawImage(imgs.get(i), width, 0, null);
		        	width +=  imgs.get(i).getWidth();
		        }
		
		        ImageIO.write(combined, "jpg", new File(path));  
		        System.out.println("===合成成功====");
			} catch (Exception e) {
				System.out.println("===合成失败====");
				e.printStackTrace();
			}
	    }
		//竖向合成图图片
		public static void verticalJointPic(List<File> files, String path) {   
			try {
				Integer allWidth = 0;	// 图片总宽度
				Integer allHeight = 0;	// 图片总高度
				List<BufferedImage> imgs = new ArrayList<>(); 
				for(int i=0; i<files.size(); i++){
					imgs.add(ImageIO.read(files.get(i)));
					//竖向
					if (i==0) {
						allWidth = imgs.get(0).getWidth();
					}
					allHeight += imgs.get(i).getHeight();
				}
		        BufferedImage combined = new BufferedImage(allWidth, allHeight, BufferedImage.TYPE_INT_RGB);  
		        // paint both images, preserving the alpha channels  
		        Graphics g = combined.getGraphics();
		         // 竖向合成
		        Integer height = 0;
		        for(int i=0; i< imgs.size(); i++){
	        		g.drawImage(imgs.get(i), 0, height, null);  
	        		height +=  imgs.get(i).getHeight();
		        }
		        ImageIO.write(combined, "jpg", new File(path));  
		        System.out.println("===合成成功====");
			} catch (Exception e) {
				System.out.println("===合成失败====");
				e.printStackTrace();
			}
	    }
	    	public static void main(String[] args) throws Exception{
			List<File> files = new ArrayList<>();	
			File file1 = new File("D:/job/image/basetoimg1-2.jpg");
			File file2 = new File("D:/job/image/basetoimg2-2.jpg");
			File file3 = new File("D:/job/image/basetoimg3-2.jpg");
			files.add(file1);
			files.add(file2);
			files.add(file3);
			String path = "D:/job/image/down.jpg";
			//横向合成图片
			transverseJointPic(files, path);*/
	//竖向合成
			String allpath ="D:/job/image/alltest1.jpg";
			verticalJointPic(files,allpath);

			}

参考:https://blog.csdn.net/qq_34846877/article/details/81318999
本站系本人编辑转载,转载目的在于传递更多信息,并不代表本人赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本人联系,我将在第一时间删除内容![声明]本站文章版权归原作者所有 内容为作者个人观点 本站只提供参考并不构成任何投资及应用建议。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值