How to make an image blur in java

Hi guys, today we will learn how to make an image blur in java.

The Blurring of an image basically refers to reducing the sharpness of that image typically reducing the image noise.

There can be multiple ways to solve this issue as per one understanding. As one can use image processing using OpenCV but I am assuming us at a beginner level. For that, here we will use the AWT package of java rather than OpenCV.

In this tutorial, we will use some in-built methods of BufferedImage class and Color class.

I am assuming that you have a well-known knowledge of shifting operators and some basic knowledge of how images are handled using java’s awt package as they have been used in the following code. If you don’t know about them then I will suggest first read about them before going to the code.

How to make an image blur: Java Code

import java.awt.Image;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main {

public static void main(String[] args) throws IOException, InterruptedException {
    
      Color color[];
      //read the image in the form of file from directory
        File fin = new File("/home/shivank/Desktop/owl.jpg");
        //convert into image form
        BufferedImage input = ImageIO.read(fin);

        BufferedImage output = new BufferedImage(input.getWidth(), input.getHeight(), BufferedImage.TYPE_INT_RGB);
        int i = 0;
        int max = 400, rad = 10;
        int a1 = 0, r1 = 0, g1 = 0, b1 = 0;
        color = new Color[max];

        //core section of code responsible for blurring of an image
        int x = 1, y = 1, x1, y1, ex = 5, d = 0;
        for (x = rad; x < input.getHeight() - rad; x++) {
            for (y = rad; y < input.getWidth() - rad; y++) {

                for (x1 = x - rad; x1 < x + rad; x1++) {
                    for (y1 = y - rad; y1 < y + rad; y1++) {
                        color[i++] = new Color(input.getRGB(y1, x1));
                    }
                }
                i = 0;

                for (d = 0; d < max; d++) {
                    a1 = a1 + color[d].getAlpha();
                }
                a1 = a1 / (max);

                for (d = 0; d < max; d++) {
                    r1 = r1 + color[d].getRed();
                }
                r1 = r1 / (max);

                for (d = 0; d < max; d++) {
                    g1 = g1 + color[d].getGreen();
                }
                g1 = g1 / (max);

                for (d = 0; d < max; d++) {
                    b1 = b1 + color[d].getBlue();
                }
                b1 = b1 / (max);
                int sum1 = (a1 << 24) + (r1 << 16) + (g1 << 8) + b1;
                output.setRGB(y, x, (int) (sum1));

            }
        }
        //write the image on the disc
        ImageIO.write(output, "jpg", new File("/home/shivank/Desktop/owlblurr.jpg"));
        System.out.println("Done!");
    }
}

Input image:

How to make an image blur in Java

Output image:

This is a blur image in Java awt

Be patient with the output it may take some time. Since this process is not that much efficient but it is perfect for a beginner to understand the concept. Also, efficiency can be achieved by using OpenCV.

Also, you have to provide your own directory as I have provided mine at lines 13 & 60.

Hence, now you know how to blur an image using java. Feel free to comment in case of any query.

You may also read:

How to crop an image in Java

Leave a Reply

Your email address will not be published. Required fields are marked *