模糊函数(ScriptIntrinsicBlur)将模糊应用于比想要的更多位图

时间:2016-11-26 16:35:13

标签: android bitmap android-bitmap

编辑:我找到了解决方案。尽管它更像是一种奇怪的解决方法。答案如下。

我有两个Bitmap副本。其中一个变得模糊和扩大。另一个应该保持它原来的方式,但有些东西正在应用相同的模糊。我不太清楚问题是什么。

public static Bitmap blur(Bitmap image, float radius, int runs, Context context) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(context);

    for (int x = 0; x < runs; x++) {
        Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
        Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

        //Intrinsic Gausian blur filter
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        theIntrinsic.setRadius(radius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
    }
    return outputBitmap;
}

这是我使用此功能的地方: bitmapS是一个,它应该保持不变,但它也会变得模糊。

                bitmapS = bitmap;
                bitmapHeight = bitmap.getHeight();
                bitmapWidth = bitmap.getWidth();

                int scale = Math.round((screenHeight / bitmapHeight) + 0.5f);
                bitmapL = bitmap;
                bitmapL = blur(bitmapL, 25f, 3, WallpaperService.this);
                bitmapL = blur(
                        Bitmap.createScaledBitmap(bitmapL
                                , bitmapWidth * scale, bitmapHeight * scale, true),
                        25f, 1, WallpaperService.this);

1 个答案:

答案 0 :(得分:0)

我仍然不知道导致问题的原因,但我找到了解决方法。

由于我想更多地模糊图像,我只是缩小了位图,然后模糊了它。然后我又把它缩放了。重新缩放似乎以某种方式解决了我的问题。

bitmapL = Bitmap.createScaledBitmap(bitmap, bitmapWidth / 3, bitmapHeight / 3, true);
bitmapL = blur(bitmapL, 25f, 2, WallpaperService.this);
bitmapL = blur(Bitmap.createScaledBitmap(
    bitmapL, bitmapWidth * scaleL, bitmapHeight * scaleL, true), 
    25f, 1, WallpaperService.this);
相关问题