Working with images in Java: resizing and cropping
In modern web and desktop development with Java, there is often a need to programmatically process images: reduce them for thumbnails, crop excess edges, or fit them to specified proportions. The standard Java library (Java SE) does not have a single high-level class for these operations, but there is a powerful set of tools in the java.awt, java.awt.image, and javax.imageio packages. In this article, we will thoroughly examine how to perform resize and crop operations using built-in Java tools.
Main classes for working with images
Before diving into the code, let's get acquainted with the key classes:
javax.imageio.ImageIO– reading and writing images (supports PNG, JPEG, BMP, GIF, etc.).java.awt.image.BufferedImage– raster image with pixel access.java.awt.Graphics2D– class for drawing (scaling, cropping, overlaying).java.awt.geom.AffineTransform– transformation matrix for precise scaling.
1. Resizing an image
The most common method is to redraw the original image onto a new canvas with the desired dimensions. For high-quality results, use RenderingHints.
import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;
public class ImageResizer {
public static BufferedImage resize(BufferedImage original, int targetWidth, int targetHeight) { BufferedImage resized = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = resized.createGraphics();
// Configure scaling quality g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(original, 0, 0, targetWidth, targetHeight, null); g2d.dispose(); return resized; }
public static void main(String[] args) throws IOException { File input = new File("input.jpg"); BufferedImage source = ImageIO.read(input);
// Reduce to 300x200 BufferedImage output = resize(source, 300, 200); ImageIO.write(output, "jpg", new File("resized.jpg")); System.out.println("Resize completed!"); }}Important notes:
- If you do not set
RenderingHints, the result will be low quality (nearest neighbor). - For PNG with transparency, use
BufferedImage.TYPE_INT_ARGB. - Aspect ratio is not preserved automatically – calculate the ratio manually if needed.
2. Preserving aspect ratio
To avoid distorting the image, calculate the new size while preserving the aspect ratio:
public static BufferedImage resizeKeepRatio(BufferedImage original, int maxWidth, int maxHeight) { int origWidth = original.getWidth(); int origHeight = original.getHeight();
double widthRatio = (double) maxWidth / origWidth; double heightRatio = (double) maxHeight / origHeight; double ratio = Math.min(widthRatio, heightRatio);
int newWidth = (int) (origWidth * ratio); int newHeight = (int) (origHeight * ratio);
return resize(original, newWidth, newHeight);}3. Cropping an image
Cropping in Java is done using the getSubi
Also in library
Working with YAML in Go (Golang): go-yaml and yaml.v3 libraries
Working with Excel in Go (Golang): exelize and tealeg/xlsx libraries
Gson for Java: Complete Guide with Examples
Testing in PHP: Overview of Frameworks and Tools (PHPUnit, Pest, Codeception)
Gorilla WebSocket library for Go (Golang) — Complete Guide