Java BufferedImage Split splitImage(final Image img, final int rows, final int cols)

Here you can find the source of splitImage(final Image img, final int rows, final int cols)

Description

Splits an image into a number of rows and columns

License

Apache License

Parameter

Parameter Description
img The image to be split
rows The number of rows
cols The number of columns

Return

The array of split images in the vertical order

Declaration

public static BufferedImage[] splitImage(final Image img, final int rows, final int cols) 

Method Source Code

//package com.java2s;
/**//from   w  w w. j ava2s  . c o m
 * Copyright 2013 Maxime Bossard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.Transparency;
import java.awt.image.BufferedImage;

public class Main {
    /**
     * Splits an image into a number of rows and columns
     * 
     * @param img
     *            The image to be split
     * @param rows
     *            The number of rows
     * @param cols
     *            The number of columns
     * @return The array of split images in the vertical order
     */
    public static BufferedImage[] splitImage(final Image img, final int rows, final int cols) {
        // Determine the width of each part
        final int w = img.getWidth(null) / cols;
        // Determine the height of each part
        final int h = img.getHeight(null) / rows;
        // Determine the number of BufferedImages to be created
        final int num = rows * cols;
        // The count of images we'll use in looping
        int count = 0;
        // Create the BufferedImage array
        final BufferedImage[] imgs = new BufferedImage[num];
        // Start looping and creating images [splitting]
        for (int x = 0; x < rows; x++) {
            for (int y = 0; y < cols; y++) {
                // The BITMASK type allows us to use bmp images with coloured
                // text and any background
                imgs[count] = new BufferedImage(w, h, Transparency.BITMASK);
                // Get the Graphics2D object of the split part of the image
                final Graphics2D g = imgs[count++].createGraphics();
                // Draw only the required portion of the main image on to the
                // split image
                g.drawImage(img, 0, 0, w, h, w * y, h * x, w * y + w, h * x + h, null);
                // Now Dispose the Graphics2D class
                g.dispose();
            }
        }
        return imgs;
    }
}

Related

  1. splitByWidth(BufferedImage img, int width)
  2. splitImage(BufferedImage image, int row, int col)
  3. splitImage(BufferedImage image, int rows, int cols)
  4. splitImage(BufferedImage img, int cols, int rows)
  5. splitImage(BufferedImage img, int rows, int cols)
  6. splitImage2D(BufferedImage img, int cols, int rows)
  7. splitImageIntoTiles(final BufferedImage imageWithTiles, final int numberOfTilesAcross, final int numberOfTilesDown)
  8. splitVertically(BufferedImage top, int elements)
  9. tileStretchPaint(Graphics g, JComponent comp, BufferedImage img, Insets ins)