1

Edit I'm using Eclipse ADT bundle but when I try importing the library in the suggested answer above it doesn't work.

I'm using a combination of LibGDX and Java (90-99% java) to blur an image. The blur works but it takes nearly a second to take a screenshot, save it, access it, blur it, save it, and re-access it. I can't do much pre-processing because it takes a screenshot of the game to blur. Here is what I'm using currently for blurring: (I'll put drawing and screenshots up if you need to see them, but I think the issue lies in the blurring of an 800x480 png.)

import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;


public class Blur {
    private static BufferedImage mshi;
    private BufferedImage databuf;
    private static int blurRad = 300;

    public static void createBlur(FileHandle file) throws IOException {
        mshi = ImageIO.read(file.file());
        BufferedImage databuf = new BufferedImage(mshi.getWidth(null),
                mshi.getHeight(null),
                BufferedImage.TYPE_INT_BGR);

        java.awt.Graphics g = databuf.getGraphics();
        g.drawImage(mshi, 455, 255, null);

        float[] blurKernel = new float[blurRad*blurRad];
        for(int i =0; i<blurRad*blurRad; i++) {
            blurKernel[i] = 1.0f/256.0f;
        }

        BufferedImageOp op = new ConvolveOp( new Kernel(20, 20, blurKernel), ConvolveOp.EDGE_ZERO_FILL, null );
        mshi = op.filter(mshi, databuf);

        g.dispose();    

        File outputfile = Gdx.files.local("Blur.png").file();
        ImageIO.write(mshi, "png", outputfile);
    }
}
Chris
  • 2,435
  • 6
  • 26
  • 49
  • 1
    possible duplicate of [Android: fast bitmap blur?](http://stackoverflow.com/questions/14988990/android-fast-bitmap-blur) – Geobits Sep 30 '13 at 19:19
  • 1
    First off, it looks like you're allocating 90000 elements for your kernel then using only 400 of them which will be wasting some time. Also, averaging filters are separable. That means you can do 1D convolution on each row, then 1D convolution on each column and get the same effect as convolving with the 2D averaging filter. This is the difference between 400 ops per pixel and 40 ops per pixel. – jodag Oct 01 '13 at 01:35
  • I don't think ImageIO and Java 2D work on Android? Or am I missing something? – Harald K Oct 01 '13 at 11:16
  • try http://www.jhlabs.com/ip/blurring.html – code_fish Oct 08 '13 at 07:28

0 Answers0