在java中模糊图像的最快方法

时间:2013-09-30 19:09:37

标签: java android image-processing blur

编辑我正在使用Eclipse ADT捆绑包但是当我尝试在上面建议的答案中导入库时,它不起作用。

我正在使用LibGDX和Java(90-99%java)的组合来模糊图像。模糊可行,但需要将近一秒的时间来截取,保存,访问,模糊,保存和重新访问它。我无法进行太多的预处理,因为它会让游戏的屏幕截图模糊不清。以下是我目前用于模糊的内容:(如果您需要查看它们,我会将绘图和屏幕截图放好,但我认为问题在于模糊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);
    }
}

0 个答案:

没有答案