DEV Community

IDRSolutions
IDRSolutions

Posted on

How to Manipulate Images in Java using JDeli

In this post, we’ll take a quick look at how JDeli simplifies image editing for Java developers. The ideal image processing Java library should get you optimal results and modify images without compromising quality.

Introduction

Image manipulation is a common requirement for many Java developers working with images, but finding a reliable, high-performance solution isn’t always straightforward. You need a Java Image processing library that modifies images with minimal code and maintains the integrity of the content.

JDeli offers a powerful enterprise alternative to standard libraries, making it easier to read, write, and manipulate images while supporting a wide range of formats. The Java Image processing library also supports formats not supported elsewhere such as Apple’s HEIC.

Why JDeli?

JDeli stands out for Java developers who need reliable image manipulation as it supports a wide range of formats and delivers high performance without relying on third-party dependencies. Its straightforward integration and clear documentation ensure peace of mind for enterprise users.

Its pure Java implementation, flexible licensing, and efficient handling of both simple and advanced image tasks make it a smart, future-proof choice for any Java imaging project. Its straightforward integration and clear documentation ensure peace of mind for teams.

Image Manipulation in Java

Below are all the features and capabilities for Java developers that JDeli offers to programmatically modify images.

Image Processing Features in JDeli

JDeli includes a large number of defined image processing operations, which you can apply to images. These are listed below.

You can also write your own in Java. You can even combine multiple operations very easily like so!

operations.scale(1.2).sharpen().rotate(90);

Enter fullscreen mode Exit fullscreen mode

All this makes JDeli a very powerful tool for Image Processing in Java.

Brighten an Image in Java

The brighten operation will make the image brighter by the percent given.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.brighten(10); // Make the image 10% brighter

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Clip an Image in Java

The clip operation clips the image either to the specified shape or out the specified shape.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.clip(new Arc2D.Double(new Rectangle(0, 0, 100, 100), 0, 360, Arc2D.PIE), true); // Clip the image to a circle

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Create an Image thumbnail in Java

Generate a resized version of the image using the given dimensions. The scaling method is optimized to deliver the best quality when downsizing from the original image.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.thumbnail(64, 64); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Emboss an Image in Java

The emboss operation applies a 3×3 kernel matrix to give your image a raised, three-dimensional appearance.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.emboss(); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Invert colours an Image in Java

This class takes the input image and produces a version with inverted colours by applying negative ARGB values, giving it a visually reversed appearance.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.invertColors(); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Sharpen an Image in Java

The sharpen operation enhances image details by applying a 3×3 kernel matrix, making edges and textures appear more defined.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.sharpen(); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Add a text watermark to an Image in Java

The watermark operation adds text, a shape, or an image as a watermark. You can customize it using various parameters, including position options like FitToImage, Center, or USE_SHAPE_COORDINATES.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.watermark("Watermark Text", Color.BLUE, new Font("Arial", Font.BOLD, 30), FitToImage); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Mirror an Image in Java

The mirror operation creates a flipped version of an image, either horizontally or vertically, depending on the selected direction.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.mirror(MirrorOperations.HORIZONTAL); // use MirrorOperations to specify the mirroring operation to use

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Stretch an Image to Fill in Java

The stretch to fill operation expands an image to completely occupy a specified pixel rectangle. It does not preserve aspect ratio, so the image is distorted to match both the defined width and height.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.stretchToFill(100, 100); // make image fill into a box 100×100 pixels

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Apply Edge detection to an Image in Java

The edge detection operation highlights the boundaries within an image by applying a 3×3 kernel matrix to identify changes in intensity.

ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.edgeDetection(); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

Custom Operations in JDeli

The custom operation lets you integrate your own image processing code, enabling tailored functionality.

private class MyCustomImageOperation implements ImageOperation {

    private Object myArgs;

    public MyCustomImageOperations(Object myArgs) {

        this.myArgs = myArgs;
    }

    @Override
    public BufferedImage apply(BufferedImage image) {

        //process code here
        return image;
    }
}

Enter fullscreen mode Exit fullscreen mode
ImageProcessingOperations operations = new ImageProcessingOperations();

// You can chain several operations here such as scale, blur, etc
operations.custom(new MyCustomImageOperations()); 

// Apply the operations to a BufferedImage
BufferedImage modifiedImage = operations.apply(BufferedImage originalImage);

Enter fullscreen mode Exit fullscreen mode

In this tutorial you learned how to modify images in Java using our image processing Java library. To test on your own files, you can download the JDeli trial jar and understand how well it integrates with your system.

Top comments (0)