Java BufferedImage Merge mergeImages(List images, int space, Color bg)

Here you can find the source of mergeImages(List images, int space, Color bg)

Description

Merge images

License

Open Source License

Parameter

Parameter Description
images list of images
space space between images
bg background color

Return

merged image

Declaration

public static Image mergeImages(List images, int space, Color bg) 

Method Source Code


//package com.java2s;
/*/* ww w.  j av  a 2  s  .c  o m*/
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.util.List;

public class Main {
    /**
     * Merge images
     *
     * @param images list of images
     * @param space space between images
     * @param bg background color
     *
     * @return  merged image
     */
    public static Image mergeImages(List images, int space, Color bg) {
        return gridImages(images, space, bg, 1);
    }

    /**
     * Merge images
     *
     * @param images list of images
     * @param space space between images
     * @param bg background color
     * @param columns number of columns
     *
     * @return  merged image
     */
    public static Image gridImages(List images, int space, Color bg, int columns) {
        if (images.size() == 1) {
            return (Image) images.get(0);
        }

        int maxHeight = 0;
        int maxWidth = 0;
        int rows = (int) (images.size() / (double) columns + 1);

        if (rows == 0) {
            rows = 1;
        }

        for (int i = 0; i < images.size(); i++) {
            Image image = (Image) images.get(i);
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);

            maxHeight = Math.max(maxHeight, imageHeight);
            maxWidth = Math.max(maxWidth, imageWidth);
        }

        if (columns > images.size()) {
            columns = images.size();
        }

        BufferedImage bImage = new BufferedImage(maxWidth * columns + (columns - 1) * space,
                maxHeight * rows + (rows - 1) * space, BufferedImage.TYPE_INT_RGB);
        Graphics g = bImage.getGraphics();

        if (bg != null) {
            g.setColor(bg);
            g.fillRect(0, 0, bImage.getWidth(null), bImage.getHeight(null));
        }

        int colCnt = 0;
        int rowCnt = 0;

        for (int i = 0; i < images.size(); i++) {
            Image image = (Image) images.get(i);

            g.drawImage(image, colCnt * (maxWidth + space), rowCnt * (maxHeight + space), null);
            colCnt++;

            if (colCnt >= columns) {
                colCnt = 0;
                rowCnt++;
            }
        }

        return bImage;
    }
}

Related

  1. mergeImage(BufferedImage a, BufferedImage b)
  2. mergeImage(BufferedImage origImage, Vector fadedImages)