packageorg.jimmy.autosearch2019.test;importjava.awt.Graphics;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileOutputStream;importjava.util.ArrayList;importjavax.imageio.ImageIO;public classTestMergePic2019080801 {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 voidmain(String[] args) {
test();
}public static ArrayList getAllFiles(String path,String fileName, String suffix, intsize) {
ArrayList fileList = new ArrayList();for(int i = 1; i <= size; i++) {
File file= new File(path + fileName + "_" + i + "." +suffix);if(file.exists()) {
fileList.add(file);
}
}returnfileList;
}public static void mergeImagetogeter(String path, ArrayListpicList, 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= newBufferedImage(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,newFileOutputStream(path));
g.dispose();
}catch(Exception e) {
System.out.println(e.getMessage());
}
}public staticBufferedImage loadImageLocal(String imgName) {try{return ImageIO.read(newFile(imgName));
}catch(Exception e) {
System.out.println(e.getMessage());
}return null;
}public static voidtest() {
ArrayList picList = new ArrayList();
ArrayList 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!");
}
}