Split BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Resize

Description

Split BufferedImage

Demo Code


//package com.java2s;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.*;

public class Main {
    private static int rows = 2;
    private static int cols = 2;

    public static void chunk(BufferedImage image) throws IOException {

        int chunks = rows * cols;

        int chunkWidth = image.getWidth() / cols; // determines the chunk width and height  
        int chunkHeight = image.getHeight() / rows;
        int count = 0;
        BufferedImage imgs[] = new BufferedImage[chunks]; //Image array to hold image chunks  
        for (int x = 0; x < rows; x++) {
            for (int y = 0; y < cols; y++) {
                //Initialize the image array with image chunks  
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight,
                        image.getType());

                // draws the image chunk  
                Graphics2D gr = imgs[count++].createGraphics();
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight,
                        chunkWidth * y, chunkHeight * x, chunkWidth * y
                                + chunkWidth,
                        chunkHeight * x + chunkHeight, null);
                gr.dispose();/* w  w  w. j a  v a2  s.c om*/
            }
        }
        System.out.println("Splitting done");

        //writing mini images into image files  
        for (int i = 0; i < imgs.length; i++) {
            ImageIO.write(imgs[i], "jpg", new File("tmp/img" + i + ".jpg"));
        }
        System.out.println("Mini images created");
    }
}

Related Tutorials