Fastest method for blurring an image in java

拈花ヽ惹草 提交于 2020-01-05 07:23:30

问题


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);
    }
}

来源:https://stackoverflow.com/questions/19101908/fastest-method-for-blurring-an-image-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!