How to combine multiple PNGs into one big PNG file?

后端 未结 9 2033
天涯浪人
天涯浪人 2020-11-28 08:26

I have approx. 6000 PNG files (256*256 pixels) and want to combine them into a big PNG holding all of them programmatically.

What\'s the best/fastest way to do that?

9条回答
  •  温柔的废话
    2020-11-28 09:07

    Combing Images

    private static void combineALLImages(String screenNames, int screens) throws IOException, InterruptedException {
        System.out.println("screenNames --> D:\\screenshots\\screen   screens --> 0,1,2 to 10/..");
        int rows = screens + 1;
        int cols = 1;
        int chunks = rows * cols ; 
    
         File[] imgFiles = new File[chunks];
        String files = "";
        for (int i = 0; i < chunks; i++) {
            files = screenNames + i + ".jpg";
            imgFiles[i] = new File(files);          
            System.out.println(screenNames + i + ".jpg"+"\t Screens : "+screens);    
    
        }
    
        BufferedImage sample = ImageIO.read(imgFiles[0]);
        //Initializing the final image
        BufferedImage finalImg = new BufferedImage(sample.getWidth() * cols, sample.getHeight() * rows, sample.getType());
    
        int index = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                BufferedImage temp = ImageIO.read(imgFiles[index]);
                finalImg.createGraphics().drawImage(temp, sample.getWidth() * j, sample.getHeight() * i, null);
                System.out.println(screenNames + index + ".jpg");
                index++;
            }
        }
        File final_Image = new File("D:\\Screenshots\\FinalImage.jpg");
        ImageIO.write(finalImg, "jpeg", final_Image);
    
    }
    

提交回复
热议问题