Image Processing in Java: Resizing and Cropping (resize, crop)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in Java: Resizing and Cropping (resize, crop)



In modern web application and desktop program development, there is often a need to process graphic files. Java provides a powerful built-in toolkit for working with images, requiring no installation of third-party libraries. In this article, we will thoroughly examine two basic operations: resizing and cropping images using the standard javax.imageio and java.awt classes.



Key Java Classes for Image Processing



Before moving to the examples, let's get acquainted with the main components of the Java 2D API that will be needed for the work:



  • BufferedImage — the main class for representing an image with pixel access.
  • Graphics2D — a drawing context that allows performing transformations, scaling, and drawing.
  • AffineTransform — a class for affine transformations (scaling, rotation, translation).
  • ImageIO — a utility class for reading and writing images in various formats (JPEG, PNG, BMP, etc.).


All these classes are included in the standard JDK distribution, so no additional dependencies (Maven/Gradle) are required to use them.



1. Resizing an Image (resize)



The resizing (scaling) operation can be performed in two main ways: using Graphics2D (the most flexible method) or via AffineTransformOp (for more precise control of interpolation). Let's look at both approaches.



Method 1: Via Graphics2D (recommended for most tasks)



This method is suitable when you need to quickly change the size while maintaining quality. We draw the original image onto a new BufferedImage canvas of the desired size.



import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import javax.imageio.ImageIO;

public class ImageResizer {

public static BufferedImage resize(BufferedImage originalImage, int targetWidth, int targetHeight) { // Create a new image with the specified dimensions and RGB type BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB); // Get the drawing context Graphics2D graphics2D = resizedImage.createGraphics(); // Enable anti-aliasing for a higher quality result graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Draw the original image, scaling it to the new dimensions graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null); graphics2D.dispose(); // Release resources return resizedImage; }

public static void main(String[] args) throws Exception { // Read the original image BufferedImage original = ImageIO.read(new File("input.jpg")); // Resize to 800x600 pixels BufferedImage resized = resize(original, 800, 600); // Save the result ImageIO.write(resized, "jpg", new File("output_resized.jpg")); System.out.println("Image resized and saved."); }}


Important: Use RenderingHints to improve quality. VALUE_INTERPOLATION_BILINEAR provides a good balance between speed and quality, while VALUE_INTERPOLATION_BICUBIC provides maximum

Recommendations