1. Products
  2.   Image
  3.   Java
  4.   Apache Commons Imaging
 
  

Open Source Java Image Processing and Manipulation

Leading Open Source Java Image Editing and Manipulation Library allows to Read, Write, Manipulate and Convert Widely used Formats like PNG, JPEG, BMP, TIFF, ICO, and more.

What is Apache Commons Imaging?

Apache Commons Imaging, formerly known as Sanselan, is a powerful, open-source Java library developed under the Apache Commons project. It is part of the broader Apache Commons project and offers software developers an efficient way to read and write a variety of image formats without relying on external libraries. Software developers can handle a wide variety of image formats, simplifying tasks like image reading and writing, conversion to other formats, image metadata handling, custom image rendering, image color models, image manipulation, and so on. The library's flexibility makes it ideal for applications requiring image processing without relying on external dependencies. The library provides a variety of image filters, including blur, sharpen, and edge detection.

Apache Commons Imaging is a robust and versatile library that simplifies the process of working with image files in Java applications. It is a Java-based library that supports multiple image formats such as PNG, JPEG, BMP, GIF, TIFF, ICO, and so on. Moreover, it offers tools for image analysis, including histogram calculation, color space conversion, and image segmentation. It’s an ideal choice for developers, researchers, and enthusiasts alike. The library offers an efficient and simple-to-use API for image manipulation, which can be especially useful in resource-constrained environments. Whether you're working on a project that requires image processing, or simply want to experiment with image manipulation, Apache Commons Imaging is definitely worth exploring.

Previous Next

Getting Started with Apache Commons Imaging

The recommend way to install Apache Commons Imaging is using Maven Repository. Please use the following command for a smooth installation.

Maven repository for Apache Commons Imaging

 

<repositories>
	<repository>
	<id>AsposeJavaAPI</id>
	<name>Aspose Java API</name>
	<url>https://releases.aspose.com/java/repo/</url>
	</repository>
</repositories>

//Define Aspose.PDF for Java API Dependency

<dependencies>
	<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-imaging</artifactId>
	<version>1.0.0-alpha5</version>
	</dependency>
</dependencies>

Install Apache Commons Imaging via GitHub

go get -u https://github.com/apache/commons-imaging.git 

You can download the library directly from GitHub product page

Image Conversion inside java Apps

Diagrams stored in one format (e.g., PNG) might need to be converted to another format (e.g., BMP or JPEG) for compatibility or optimization purposes. Apache Commons Imaging offers conversion features, allowing developers to convert various image file formats between supported formats effortlessly. The following example shows how easily software developers can convert a PNG file to the BMP format. The process is straightforward and can handle conversions between any supported formats, making it ideal for applications needing to save images in multiple formats for compatibility purposes.

How to Convert a PNG File to BMP File Format via Java API?


File inputFile = new File("diagram.png");
BufferedImage image = Imaging.getBufferedImage(inputFile);
File outputFile = new File("converted_image.bmp");
Imaging.writeImage(image, outputFile, ImageFormats.BMP, null);

Image Manipulation via Java Library

Apache Commons Imaging can be used in combination with other Java libraries like BufferedImage to perform common image transformations such as resizing, cropping, and scaling. The Scalr.resize() method from another Java image-processing library (imgscalr) is used for resizing, followed by saving the resized image using Apache Commons Imaging. This allows for seamless integration with other Java libraries for advanced image manipulation.

How to Resize an Image inside Java Apps?

BufferedImage originalImage = Imaging.getBufferedImage(new File("sample_image.png"));
BufferedImage resizedImage = Scalr.resize(originalImage, 300); // Resize width to 300px

File outputFile = new File("resized_image.png");
Imaging.writeImage(resizedImage, outputFile, ImageFormats.PNG, null);

Image Metadata Extraction via Java API

One of the key features of Apache Commons Imaging is its ability to extract metadata from images inside Java applications. For diagrams, this metadata could contain useful information such as creation date, modification history, or author details. This capability allows developers to process diagram images in ways that extend beyond mere display. The following example shows how software developers can extract metadata from images inside Java applications.

How to Extract Metadata from Images via Java API?

File imageFile = new File("diagram.png");
ImageInfo imageInfo = Imaging.getImageInfo(imageFile);
System.out.println("Format: " + imageInfo.getFormat());
System.out.println("Width: " + imageInfo.getWidth());
System.out.println("Height: " + imageInfo.getHeight());

Custom Image Rendering in Java Apps

Software developers can use Apache Commons Imaging to generate image on the fly or modify existing ones inside java applications. By combining the library with Java’s native Graphics2D, it’s possible to draw on images programmatically or add annotations. The following code example creates a blank image, adds text to it, and saves it as a PNG file. It demonstrates how Apache Commons Imaging can work with Java’s drawing APIs to generate images from scratch.

How to Generate Images from Scratch via Java?

BufferedImage image = new BufferedImage(400, 300, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setPaint(Color.WHITE);
graphics.fillRect(0, 0, image.getWidth(), image.getHeight());

graphics.setPaint(Color.BLACK);
graphics.setFont(new Font("Arial", Font.BOLD, 20));
graphics.drawString("Custom Diagram", 100, 150);

graphics.dispose();
File outputFile = new File("custom_image.png");
Imaging.writeImage(image, outputFile, ImageFormats.PNG, null);
 English