private static BufferedImage resizeImage(BufferedImage image, int width, int height)
BufferedImage resizedImage = new BufferedImage(width, height,
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
This is enough in order to resize the image, but before calling this function we need to set the proper width and height according to the proportional resize we will make.
//imageFile is any file taken from the buffer or a disk path. It should be of type java.io.File.
//thumbnailWidth & thumbnailHeight should be defined as values in order to have the new image's sizes.
BufferedImage originalImage = ImageIO.read(imageFile);
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
float widthRatio = originalWidth/thumbnailWidth;
float heightRatio = originalHeight/thumbnailHeight;
BufferedImage resizeImageJpg = null;
if (widthRatio > heightRatio)
float newThumbnailHeight = (float)originalHeight/(float)originalWidth * thumbnailWidth;
resizeImageJpg = resizeImage(originalImage, thumbnailWidth, (int) newThumbnailHeight);
float newThumbnailWidth = (float)originalWidth/(float)originalHeight * thumbnailHeight;
resizeImageJpg = resizeImage(originalImage, (int) newThumbnailWidth, thumbnailHeight);
ImageIO.write(resizeImageJpg, "jpg", new File("filename..."));