Raz Cohen
Raz Cohen

Reputation: 79

Blur images using Java?

I want to blur a picture and then slowly to make it focus.

I want to do it with java, there is any open source that do this?

I found some open sources that blur the picture but none that do the reverse.

Upvotes: 0

Views: 1546

Answers (3)

Cactus Coder
Cactus Coder

Reputation: 63

I wrote a little code for you:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Blur 
{
    
    static BufferedImage img;
    
    public static void main(String a[]) throws IOException
    {
        img = ImageIO.read(new File("res/image.png"));
        
        JFrame f = new JFrame("blur");
        
        f.setSize(img.getWidth() * 2, img.getHeight());
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setIconImage(img);
        
        JPanel panel = new JPanel() {
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                g.drawImage(img, 0, 0, null);
                g.drawImage(blur(10, 69), img.getWidth(), 0, null);
            }
        };
        f.add(panel);
        f.setVisible(true);
        
    }
    
    private static int clamp(int var, int min, int max)
    {
        if(var <= min) return min;
        else if(var >= max) return max;
        else return var;
    }
    
    public static BufferedImage blur(int range, int angle)
    {
        BufferedImage b = new BufferedImage(img.getWidth() * 2, img.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g = b.createGraphics();
        
        for(int x = 0; x < img.getWidth(); x++)
        {
            for(int y = 0; y < img.getHeight(); y++)
            {
                
                //horizontal
                
                int red[] = new int[range * 2], green[] = new int[range * 2], blue[] = new int[range * 2];
                int pixels[] = new int[range * 2];
                
                for(int i = 0; i < pixels.length; i++)
                {
                    pixels[i] = img.getRGB(clamp(x - clamp(range / 2, 0, range) + i, 0, img.getWidth() - 1), clamp(y - clamp(range / 2, 0, range) + (int)(i * Math.toRadians(angle)), 0, img.getHeight() - 1));
                    
                    red[i] = (pixels[i] >> 16) & 0xff;
                    green[i] = (pixels[i] >> 8) & 0xff;
                    blue[i] = (pixels[i]) & 0xff;
                }
                
                int red_t = 0, green_t = 0, blue_t = 0;
                
                for(int i = 0; i < pixels.length; i++)
                {
                    red_t += red[i];
                    green_t += green[i];
                    blue_t += blue[i];
                }
                
                int r = red_t / (range * 2);
                int gr = green_t / (range * 2);
                int bl = blue_t / (range * 2);
                
                //System.out.println(r + ", " + gr + ", " + bl);
                
                g.setColor(new Color(r, gr, bl));
                g.fillRect(x, y, 1, 1);
                
            }
        }
        g.dispose();
        
        return b;
    }

}

As you have probably guessed, the most important part is the public static BufferedImage blur(int range, int angle) method. Most of the code is just for displaying the image in the JFrame but if you just copy the method it should work as well. You need a reference to img tho, but you can just create that in a variable as I did. If you want to know more about how the code works, or if you have any questions just ask me. this is a linear blur, the most common blur is a gaussian blur. If you want that, you just have to make the pixels, red, green and blue variable a 2-dimensional array. The end result of only this code should look like this: image

Upvotes: 0

assylias
assylias

Reputation: 328923

Java FX 2 has some methods to apply a blur effect - see for example this tutorial. The example can be adapted to blur an image instead of text.

You can blur your image then progressively "unblur" it to make it look more focused.

Upvotes: 0

dsgriffin
dsgriffin

Reputation: 68626

Have a read about (or Google) Convolution Filters - it's basically a way of changing a pixels value based on the values of the pixels around it. So apart from blurring, you can also do things like image sharpening, line-finding etc.

Upvotes: 3

Related Questions