Here you can find the source of cutImage(String src, String dest, int x, int y, int w, int h)
@SuppressWarnings("unchecked") public static boolean cutImage(String src, String dest, int x, int y, int w, int h)
//package com.java2s; //License from project: Open Source License import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; public class Main { @SuppressWarnings("unchecked") public static boolean cutImage(String src, String dest, int x, int y, int w, int h) { try {/*from w w w .j a v a 2 s .c o m*/ Iterator iterator = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = (ImageReader) iterator.next(); InputStream in = new FileInputStream(src); ImageInputStream iis = ImageIO.createImageInputStream(in); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); x = (x > 0 ? x : 0); y = (y > 0 ? y : 0); Rectangle rect = new Rectangle(x, y, w, h); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, "jpg", new File(dest)); return true; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return false; } } }