Here you can find the source of cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)
public static BufferedImage cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics; import java.awt.image.BufferedImage; public class Main { public static BufferedImage cutImage(final BufferedImage bufferedImage, final int targetW, final int targetH) { int sourceWidth = bufferedImage.getWidth(); int sourceHeight = bufferedImage.getHeight(); int dw = targetW, dh = targetH; if (targetW > sourceWidth) { dw = sourceWidth;/*www .j a v a2s.co m*/ } if (targetH > sourceHeight) { dh = sourceHeight; } int x = 0, y = 0, sw = 0, sh = 0; x = (sourceWidth / 2) - (dw / 2); y = (sourceHeight / 2) - (dh / 2); sw = (sourceWidth / 2) + (dw / 2); sh = (sourceHeight / 2) + (dh / 2); BufferedImage destination = null; destination = new BufferedImage(targetW, targetH, BufferedImage.TYPE_INT_RGB); Graphics graphics = destination.getGraphics(); graphics.drawImage(bufferedImage, 0, 0, dw, dh, x, y, sw, sh, null); return destination; } }