split Image BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage

Description

split Image BufferedImage

Demo Code


//package com.java2s;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main {
    /**//from  w w w  . j av a2 s.c o m
     * @param img: A BufferedImage that will be split
     * @param cols: Amount of columns
     * @param rows: Amount of rows
     * @return An array with the images
     */
    public static BufferedImage[] splitImage(BufferedImage img, int cols,
            int rows) {
        int w = img.getWidth() / cols;
        int h = img.getHeight() / rows;
        int num = 0;
        BufferedImage imgs[] = new BufferedImage[w * h];
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                imgs[num] = new BufferedImage(w, h, img.getType());
                // Tell the graphics to draw only one block of the image
                Graphics2D g = imgs[num].createGraphics();
                g.drawImage(img, 0, 0, w, h, w * x, h * y, w * x + w, h * y
                        + h, null);
                g.dispose();
                num++;
            }
        }
        return imgs;
    }
}

Related Tutorials