Here you can find the source of splitImage(BufferedImage image, int rows, int cols)
public static BufferedImage[] splitImage(BufferedImage image, int rows, int cols) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2015, Daniel Ludin//from www .j a v a 2 s . c om * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Daniel Ludin (ludin@hispeed.ch) - initial implementation *******************************************************************************/ import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class Main { public static BufferedImage[] splitImage(BufferedImage image, int rows, int cols) throws Exception { 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 int imageType = image.getType(); if (imageType == 0) { imageType = 5; } imgs[count] = new BufferedImage(chunkWidth, chunkHeight, imageType); // 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(); } } //writing mini images into image files for (int i = 0; i < imgs.length; i++) { ImageIO.write(imgs[i], "jpg", new File("D:\\tmp", "img" + i + ".jpg")); } return imgs; } }