Here you can find the source of divideImage(BufferedImage bi, int rows, int cols, int width, int height)
public static List<BufferedImage> divideImage(BufferedImage bi, int rows, int cols, int width, int height)
//package com.java2s; //License from project: Open Source License import java.awt.*; import java.awt.image.*; import java.util.ArrayList; import java.util.List; import javax.swing.*; public class Main { public static List<BufferedImage> divideImage(Object obj, int rows, int cols) { BufferedImage bi;/* www . java 2 s . co m*/ if (obj instanceof Image) { bi = toBufferedImage((Image) obj); } else if (obj instanceof ImageIcon) { bi = toBufferedImage(((ImageIcon) obj).getImage()); } else { bi = (BufferedImage) obj; } int altoBi = bi.getHeight() / rows; int anchoBi = bi.getWidth() / cols; List<BufferedImage> imagenes = new ArrayList<>(); BufferedImage subImagen; for (int f = 0; f < rows; f++) { for (int c = 0; c < cols; c++) { subImagen = bi.getSubimage(c * anchoBi, f * altoBi, anchoBi, altoBi); imagenes.add(subImagen); } } return imagenes; } public static List<BufferedImage> divideImage(BufferedImage bi, int rows, int cols, int width, int height) { int x, y; List<BufferedImage> imagenes = new ArrayList<>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { x = j * width; y = i * height; imagenes.add(bi.getSubimage(x, y, width, height)); } } return imagenes; } /** * Convierte un objeto Image a un BufferedImage * * @param img El objeto Image a convertir * @return El BufferedImage obtenido */ public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Crea un buffered image con trasparencia BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Dibuja la imagen en el buffered image Graphics2D bGr = bimage.createGraphics(); bGr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Retorna el buffered image return bimage; } /** * Obtiene el alto de una Image */ public static int getHeight(Image imagen) { int height = new ImageIcon(imagen).getIconHeight(); return height; } /** * Obtiene el alto de un IconImage */ public static int getHeight(ImageIcon imagen) { return imagen.getIconHeight(); } /** * Obtiene el ancho de una Image */ public static int getWidth(Image imagen) { int width = new ImageIcon(imagen).getIconWidth(); return width; } /** * Obtiene el ancho de un IconImage */ public static int getWidth(ImageIcon imagen) { return imagen.getIconWidth(); } }