Java合并png图片

package org.jimmy.autosearch2019.test;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;

public class TestMergePic2019080801 {

    private static final String DIR_PATH = "D:/Document/201908/20190808/Pic/Test/";
    private static final String PIC_NAME = "test201908081221";
    private static final String MERGED_PIC_PATH = DIR_PATH + "testPic2019080801.png";
    private static final String SUFFIX = "png";
    
    public static void main(String[] args) {
        test();
    }
    
    public static ArrayList<File> getAllFiles(String path,String fileName, String suffix, int size) {
        ArrayList<File> fileList = new ArrayList<File>();
        for(int i = 1; i <= size; i++) {
            File file = new File(path + fileName + "_" + i + "." + suffix);
            if(file.exists()) {
                fileList.add(file);
            }
        }
        return fileList;
    }
    
    public static void mergeImagetogeter(String path, ArrayList<BufferedImage> picList, String suffix) {         
        try {
            BufferedImage firstImage = picList.get(0);
            int w1 = firstImage.getWidth();
            int h1 = firstImage.getHeight();
            int width = w1;
            int height = h1 * picList.size();
            BufferedImage newImage = new BufferedImage(width, height, firstImage.getType());
            Graphics g = newImage.getGraphics();
            int x = 0;
            int y = 0;
            for(int i = 0; i < picList.size(); i++) {
                BufferedImage currentImage = picList.get(i);
                y = i * h1;
                g.drawImage(currentImage, x, y, w1, h1, null);
            }
            ImageIO.write(newImage, suffix, new FileOutputStream(path));
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
    public static BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
    
    public static void test() {
        ArrayList<BufferedImage> picList = new ArrayList<BufferedImage>();
        ArrayList<File> fileList = getAllFiles(DIR_PATH, PIC_NAME, SUFFIX, 52);
        for(int i = 0; i < fileList.size(); i++) {
            BufferedImage currentPic = loadImageLocal(fileList.get(i).getAbsolutePath());
            picList.add(currentPic);
        }
        mergeImagetogeter(MERGED_PIC_PATH, picList, SUFFIX);  
        //将多张图片合在一起  
        System.out.println("Successfully!");
    }

}

合并前:

 

效果图:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值