Android Box模糊算法

时间:2011-11-21 20:57:01

标签: java android algorithm blur

我一直在尝试在android中实现盒子模糊算法。 代码看起来很好但是当试图应用它时,模糊图像中的某些区域在模糊照片上都有大的黄色和白色污迹。 任何人都可以帮我找出我做错了什么? 感谢:

这就是我所拥有的:

public static Bitmap boxBlur(Bitmap bmp, int range) {
    assert (range & 1) == 0 : "Range must be odd.";

    Bitmap blurred = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
            Config.ARGB_8888);
    Canvas c = new Canvas(blurred);

    int w = bmp.getWidth();
    int h = bmp.getHeight();

    int[] pixels = new int[bmp.getWidth() * bmp.getHeight()];
    bmp.getPixels(pixels, 0, w, 0, 0, w, h);

    boxBlurHorizontal(pixels, w, h, range / 2);
    boxBlurVertical(pixels, w, h, range / 2);

    c.drawBitmap(pixels, 0, w, 0.0F, 0.0F, w, h, true, null);

    return blurred;
}

private static void boxBlurHorizontal(int[] pixels, int w, int h,
        int halfRange) {
    int index = 0;
    int[] newColors = new int[w];

    for (int y = 0; y < h; y++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        for (int x = -halfRange; x < w; x++) {
            int oldPixel = x - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixel];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = x + halfRange;
            if (newPixel < w) {
                int color = pixels[index + newPixel];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (x >= 0) {
                newColors[x] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }
        }

        for (int x = 0; x < w; x++) {
            pixels[index + x] = newColors[x];
        }

        index += w;
    }
}

private static void boxBlurVertical(int[] pixels, int w, int h,
        int halfRange) {

    int[] newColors = new int[h];
    int oldPixelOffset = -(halfRange + 1) * w;
    int newPixelOffset = (halfRange) * w;

    for (int x = 0; x < w; x++) {
        int hits = 0;
        long r = 0;
        long g = 0;
        long b = 0;
        int index = -halfRange * w + x;
        for (int y = -halfRange; y < h; y++) {
            int oldPixel = y - halfRange - 1;
            if (oldPixel >= 0) {
                int color = pixels[index + oldPixelOffset];
                if (color != 0) {
                    r -= Color.red(color);
                    g -= Color.green(color);
                    b -= Color.blue(color);
                }
                hits--;
            }

            int newPixel = y + halfRange;
            if (newPixel < h) {
                int color = pixels[index + newPixelOffset];
                if (color != 0) {
                    r += Color.red(color);
                    g += Color.green(color);
                    b += Color.blue(color);
                }
                hits++;
            }

            if (y >= 0) {
                newColors[y] = Color.argb(0xFF, (byte) (r / hits),
                        (byte) (g / hits), (byte) (b / hits));
            }

            index += w;
        }

        for (int y = 0; y < h; y++) {
            pixels[y * w + x] = newColors[y];
        }
    }
}

1 个答案:

答案 0 :(得分:3)

发现问题!
这一行:
newColors[x] = Color.argb(0xFF, (byte) (r / hits), (byte) (g / hits), (byte) (b / hits));

我将平均值转换为字节,它们应该是整数  将其更改为:

newColors[x] = Color.argb(0xFF, (int) (r / hits), (int) (g / hits), (int) (b / hits));