Here you can find the source of cutAndScaleToSquare(BufferedImage src, int size)
public static BufferedImage cutAndScaleToSquare(BufferedImage src, int size)
//package com.java2s; //License from project: Open Source License import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; public class Main { public static BufferedImage cutAndScaleToSquare(BufferedImage src, int size) { BufferedImage square = cutToSquare(src); int width = square.getWidth(); double scale = size * 1.0 / width; BufferedImage result = scale(square, scale); return result; }/*from w w w . j a va 2s. c om*/ public static BufferedImage cutToSquare(BufferedImage src) { int width = src.getWidth(); int height = src.getHeight(); if (width == height) return src; BufferedImage result = null; if (width > height) { int cut = (width - height) / 2; result = src.getSubimage(cut, 0, height, height); } else { int cut = (height - width) / 2; result = src.getSubimage(0, cut, width, width); } return result; } /** * Reference to http://stackoverflow.com/questions/4216123/how-to-scale-a-bufferedimage */ public static BufferedImage scale(BufferedImage src, double scale) { int width = src.getWidth(); int height = src.getHeight(); AffineTransform at = new AffineTransform(); at.setToScale(scale, scale); AffineTransformOp ato = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR); BufferedImage dest = new BufferedImage((int) Math.round(width * scale), (int) Math.round(height * scale), BufferedImage.TYPE_INT_ARGB); dest = ato.filter(src, dest); return dest; } }