Image Processing in Java: Libraries, Tools, and Code Examples

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, Tools, and Code Examples



Java provides powerful built-in tools for working with graphics, as well as a rich ecosystem of third-party libraries. In this article, we will look at the main ways to load, process, transform, and save images in Java — from the standard javax.imageio and java.awt classes to popular libraries that simplify a developer's life.



1. Standard Tools: javax.imageio and java.awt.image



Basic work with raster images in Java is built on the javax.imageio package (loading/saving) and java.awt.image (working with pixels). The main class is BufferedImage, which stores the image in RAM.



Loading and Saving an Image



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

public class ImageExample { public static void main(String[] args) throws IOException { // Loading an image from a file BufferedImage image = ImageIO.read(new File("input.jpg")); System.out.println("Width: " + image.getWidth() + ", Height: " + image.getHeight());

// Saving in a different format ImageIO.write(image, "png", new File("output.png")); }}


ImageIO supports formats: JPEG, PNG, BMP, WBMP, GIF (read only). To extend the list of formats, use ImageIO plugins (e.g., TwelveMonkeys).



Resizing (Manual Method)



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

public static BufferedImage resize(BufferedImage original, int newWidth, int newHeight) { BufferedImage resized = new BufferedImage(newWidth, newHeight, original.getType()); Graphics2D g2d = resized.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.drawImage(original, 0, 0, newWidth, newHeight, null); g2d.dispose(); return resized;}


2. Java 2D API: Drawing and Filters



The Graphics2D class allows not only scaling but also applying effects, rotating, cropping images, adding text and geometric shapes.



Adding a Watermark



public static BufferedImage addWatermark(BufferedImage source, String text) {    BufferedImage result = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());    Graphics2D g2d = result.createGraphics();    g2d.drawImage(source, 0, 0, null);    g2d.setColor(new Color(255, 255, 255, 128));    g2d.setFont(new Font("Arial", Font.BOLD, 40));    g2d.drawString(text, 20, source.getHeight() - 20);    g2d.dispose();    return result;}


Applying a Color Filter (Sepia)



public static BufferedImage applySepia(BufferedImage img) {    int width = img.getWidth();    int height = img.getHeight();    BufferedImage sepia = new BufferedImage(width, height, img.getType());

for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int rgb = img.getRGB(x, y); int r = (rgb >> 16) & 0xFF; int g = (rgb >> 8) & 0xFF; int b = rgb & 0xFF;

int newR = (int) (0.393 * r + 0.769 * g + 0.189 * b); int newG = (int) (0.349 * r + 0.686 * g + 0.168 * b); int newB = (int) (0.272 * r + 0.534 * g + 0.131 * b);

newR = Math.min(255, newR); newG = Math.min(255, newG); newB = Math.min(255, newB);

int newPixel = (0xFF << 24) | (newR << 16) | (newG << 8) | newB; sepia.setRGB(x, y, newPixel); } } return sepia;}


3. Thumbnailator — Simple Gen

Recommendations