图片*合并*追加*横*竖*

/**
* jointPic
* @param files
* @param path
*/
public static void jointPic(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();

// 横向
// 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 height = 0;
for(int i=0; i< imgs.size(); i++){
g.drawImage(imgs.get(i), 0, height, null);
height += imgs.get(i).getHeight();
}

// 横向合成
// 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 main(String[] args) throws Exception{
List<File> files = new ArrayList<>();
File file1 = new File("C:/Desktop/NO1.jpg");
File file2 = new File("C:/Desktop/NO2.jpg");
files.add(file1);
files.add(file2);
String path = "C:/Users/Administrator/Desktop/new.jpg";
jointPic(files, path);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值