DEV Community

IDRSolutions
IDRSolutions

Posted on

Blurring Images in Java: Simple & Gaussian Blur Code Examples

Blurring is a fundamental image processing technique used to reduce noise, smooth features, and produce aesthetic effects. In Java, you can easily apply different types of blur using image processing libraries like JDeli.

This post will guide you through basic blurring and Gaussian blurring of images in Java, explaining their differences and practical implementations.

What is Image Blurring?

Blurring involves averaging the colours of neighbouring pixels to produce a softer, smoother appearance. This is often used to de-emphasize background elements, eliminate noise, or prepare images for further processing such as edge detection.

Think of blurring as “softening the hard edges” in a picture. When you blur an image, you’re basically averaging out the colours of neighbouring pixels so everything looks a bit smoother and gentler.

Java Code to Blur an Image with JDeli

Let’s jump right in. With JDeli, you can get a basic blur applied in just a few lines of code:

ImageProcessingOperations operations = new ImageProcessingOperations();

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

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

Enter fullscreen mode Exit fullscreen mode

You’ve also got options for working with images from files, streams, or even byte arrays. Here are a few quick examples:

Using Files

File inputFile = new File("path/to/file");
File outputFile = new File("path/to/output-blurred-file");
JDeli.convert(inputFile, outputFile, operations);

Enter fullscreen mode Exit fullscreen mode

Using Streams

final InputStream inputStream = new FileInputStream(inputFile);
final OutputStream outputStream = new FileOutputStream(outputFile);
final String outputFormat = "format"; // format of the output file eg. png, jpeg,…;
JDeli.convert(inputStream, outputStream, outputFormat, operations);

Enter fullscreen mode Exit fullscreen mode

Using Files

byte[] inputData = Files.readAllBytes(Paths.get("/path/to/file"));
final String outputFormat = "format"; // format of the output file eg. png, jpeg,…;
byte[] outputData = JDeli.convert(inputData, outputFormat, operations);

Enter fullscreen mode Exit fullscreen mode

What is a Gaussian Blur?

Instead of just averaging nearby pixels equally, Gaussian blur uses a bell-shaped curve (the famous Gaussian function!) to weigh nearby pixels, ones closer to the centre have a bigger impact on the result.

How to Use Gaussian Blur in Java with JDeli

Doing a Gaussian blur in JDeli is just as simple as the basic blur:

ImageProcessingOperations operations = new ImageProcessingOperations();

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

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

Enter fullscreen mode Exit fullscreen mode

As with the basic blur, you can use this operation on files, streams, or byte arrays. The only real difference in your code is replacing blur() with gaussianBlur().

Which Blur Should You Choose?

Blur vs Gaussian Blur

JDeli’s API design allows you to experiment with either method easily, so you can choose the result that best fits your needs. It also helps you other ways to manipulate images using a few lines of Java.

If you’d like to explore these techniques further, the JDeli documentation provides more in-depth examples and technical details.

Top comments (0)