Working with Images in Java: Complete Overview of Libraries and Tools
Java provides powerful tools for working with raster images — from simple loading and saving to complex processing using filters and neural networks. In this article, we will cover three main approaches: the built-in Java 2D library, the specialized ImageJ, and the industry standard OpenCV. You will learn how to choose the right tool and see working code examples.
1. Built-in Java 2D Library (javax.imageio, java.awt)
Java 2D is part of the standard Java library (JDK). It requires no additional dependencies and is ideal for basic operations: loading, resizing, cropping, text overlay, and simple filters.
Main Classes:
javax.imageio.ImageIO— reading and writing images (PNG, JPEG, BMP, GIF, WBMP).java.awt.image.BufferedImage— in-memory image representation with pixel access.java.awt.Graphics2D— drawing context (scaling, rotation, overlay).java.awt.image.BufferedImageOp— interface for filters (blur, contrast, color correction).
Example 1: Loading, Resizing, and Saving
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 void main(String[] args) throws IOException { // Load the original image BufferedImage original = ImageIO.read(new File("input.jpg"));
// New dimensions (e.g., resize to 300x200) int newWidth = 300; int newHeight = 200;
// Create a new image with the desired dimensions BufferedImage resized = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = resized.createGraphics();
// Draw the original image with scaling g2d.drawImage(original, 0, 0, newWidth, newHeight, null); g2d.dispose();
// Save the result ImageIO.write(resized, "jpg", new File("output.jpg")); System.out.println("Done! Size changed."); }}Example 2: Applying a Built-in Filter (Blur)
import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.awt.image.ConvolveOp;import java.awt.image.Kernel;import java.io.File;
public class BlurFilter { public static void main(String[] args) throws Exception { BufferedImage img = ImageIO.read(new File("input.jpg"));
// 3x3 blur kernel (box blur) float[] blurKernel = { 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f, 1f/9f }; Kernel kernel = new Kernel(3, 3, blurKernel); ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); BufferedImage blurred = convolve.filter(img, null);
ImageIO.write(blurred, "jpg", new File("blurred.jpg")); }}2. ImageJ Library — for Scientific and Medical Images
ImageJ is a popular image processing tool originally written in Java. It provides hundreds of ready-made plugins, macros, and a powerful API. It is well-suited for microscopy analysis, satellite imagery, and any tasks requiring pixel statistics.
Dependency (Maven):
<dependency> <groupId>net.imagej</groupId> <artifactId>ij</artifactId> <version>1.53t</version></dependency>Example: Histogram Analysis and Thresholding
import ij.IJ;import ij.ImagePlus;import ij.