Here you can find the source of cut2(String srcImageFile, String descDir, int rows, int cols)
public static final void cut2(String srcImageFile, String descDir, int rows, int cols)
//package com.java2s; //License from project: Apache License import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.awt.image.ImageFilter; import java.io.File; import javax.imageio.ImageIO; public class Main { public static final void cut2(String srcImageFile, String descDir, int rows, int cols) { try {//from w w w .j av a 2 s . c om if ((rows <= 0) || (rows > 20)) { rows = 2; } if ((cols <= 0) || (cols > 20)) { cols = 2; } BufferedImage bi = ImageIO.read(new File(srcImageFile)); int srcWidth = bi.getHeight(); int srcHeight = bi.getWidth(); if ((srcWidth > 0) && (srcHeight > 0)) { Image image = bi.getScaledInstance(srcWidth, srcHeight, 1); int destWidth = srcWidth; int destHeight = srcHeight; if (srcWidth % cols == 0) { destWidth = srcWidth / cols; } else { destWidth = (int) Math.floor(srcWidth / cols) + 1; } if (srcHeight % rows == 0) { destHeight = srcHeight / rows; } else { destHeight = (int) Math.floor(srcWidth / rows) + 1; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { ImageFilter cropFilter = new CropImageFilter(j * destWidth, i * destHeight, destWidth, destHeight); Image img = Toolkit.getDefaultToolkit() .createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(destWidth, destHeight, 1); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); ImageIO.write(tag, "JPEG", new File(descDir + "_r" + i + "_c" + j + ".jpg")); } } } } catch (Exception e) { e.printStackTrace(); } } }