Working with Images in Java: From Reading to Processing

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: Libraries and Practical Examples



Java provides powerful built-in tools for working with raster images. In this article, we will cover the standard javax.imageio library, the java.awt.image package, and the popular third-party library Thumbnailator. You will learn how to read, save, scale, apply filters, and convert image formats.



1. Basics: javax.imageio Package and BufferedImage



The key class for representing an image in memory is BufferedImage. It allows you to get and modify pixels, work with different color models (RGB, ARGB), and data types.



Reading an Image from a File



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

public class ImageReadExample { public static void main(String[] args) { try { File inputFile = new File("input.jpg"); BufferedImage image = ImageIO.read(inputFile); System.out.println("Width: " + image.getWidth()); System.out.println("Height: " + image.getHeight()); System.out.println("Type: " + image.getType()); } catch (IOException e) { e.printStackTrace(); } }}


Writing an Image to a File



File outputFile = new File("output.png");ImageIO.write(image, "png", outputFile); // Format is determined by the extension


Supported formats: JPEG, PNG, BMP, WBMP, GIF (read only). A full list can be obtained via ImageIO.getReaderFormatNames().



2. Scaling and Transformations with Java 2D



The java.awt.Graphics2D package allows you to perform high-quality scaling, rotation, reflection, and image overlaying.



Scaling with Aspect Ratio Preservation



import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.image.BufferedImage;

public class ImageResizer { public static BufferedImage resize(BufferedImage original, int targetWidth, int targetHeight) { BufferedImage resized = new BufferedImage(targetWidth, targetHeight, original.getType()); Graphics2D g2d = resized.createGraphics(); // Enable anti-aliasing for better 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; }}


Rotating an Image by 90 Degrees



public static BufferedImage rotate(BufferedImage image, double angleDegrees) {    double radians = Math.toRadians(angleDegrees);    int w = image.getWidth();    int h = image.getHeight();        // New size after rotation    int newW = (int) Math.abs(w * Math.cos(radians)) + (int) Math.abs(h * Math.sin(radians));    int newH = (int) Math.abs(h * Math.cos(radians)) + (int) Math.abs(w * Math.sin(radians));        BufferedImage rotated = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);    Graphics2D g2d = rotated.createGraphics();    g2d.translate((newW - w) / 2.0, (newH - h) / 2.0);    g2d.rotate(radians, w / 2.0, h / 2.0);    g2d.drawImage(image, 0, 0, null);    g2d.dispose();    return rotated;}


3. Applying Filters: Blur, Contrast, Brightness



For filtering, use BufferedImageOp (from the java.awt.image package). The most useful implementations are:



    \

Recommendations