zoukankan      html  css  js  c++  java
  • 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!"); } }

    合并前:

    效果图:

    2015年10月-2016年3月 总计:5个月.
    2016年11月-2017年6月 总计:7个月.
    2017年7月-2018年4月 总计:9个月.
    2018年5月-2018年5月 总计:1个月.
    2018年6月-2018年12月 总计:6个月.
    2019年1月-2019年12月 总计11个月.
    2020年2月-2021年2月 总计13个月.
    所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
    本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
    英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
  • 相关阅读:
    巴洛克式和哥特式的区别
    推荐阅读书籍,是时候再行动起来了。
    AtCoder ABC 159F Knapsack for All Segments
    AtCoder ABC 159E Dividing Chocolate
    AtCoder ABC 158F Removing Robots
    AtCoder ABC 158E Divisible Substring
    AtCoder ABC 157F Yakiniku Optimization Problem
    AtCoder ABC 157E Simple String Queries
    AtCoder ABC 157D Friend Suggestions
    AtCoder ABC 156F Modularness
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/11320446.html
Copyright © 2011-2022 走看看