Here you can find the source of crop(BufferedImage src, int width, int height)
public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.imageio.ImageIO; public class Main { public static ByteArrayOutputStream crop(ByteArrayInputStream bais, int width, int height) throws IOException { BufferedImage src = ImageIO.read(bais); BufferedImage clipping = crop(src, width, height); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(clipping, "JPG", baos); return baos; }//from w ww .ja v a 2 s .c o m public static BufferedImage crop(BufferedImage src, int width, int height) throws IOException { int x = src.getWidth() / 2 - width / 2; int y = src.getHeight() / 2 - height / 2; // System.out.println("---" + src.getWidth() + " - " + src.getHeight() + " - " + x + " - " + y); BufferedImage clipping = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//src.getType()); Graphics2D area = (Graphics2D) clipping.getGraphics().create(); area.drawImage(src, 0, 0, clipping.getWidth(), clipping.getHeight(), x, y, x + clipping.getWidth(), y + clipping.getHeight(), null); area.dispose(); return clipping; } }