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 is a versatile, open source Java imaging API that provides a comprehensive toolkit for developers. As a pure Java imaging library, it enables efficient read/write images in Java across numerous formats like PNG, JPEG, BMP, and GIF without external dependencies. Core functions include image to PNG conversion, the ability to convert image to JPEG, and the power to create PNG, JPEG, BMP, and GIF image files from scratch. The library also excels in extract image metadata and supports advanced custom image rendering for tailored visual outputs.
This robust Java Image processing API simplifies complex image manipulation in Java with built-in analytical tools. Developers can perform color space conversion, calculate histograms, and apply essential filters for blur, sharpen, or edge detection. Its self-contained, efficient design makes it ideal for resource-sensitive environments, offering a streamlined approach to format handling, analysis, and pixel-level editing for both practical applications and experimental projects.
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);