0

First i loaded a icon to drawable and converted it to Bitmap, and i blur it by using Renderscript.

defautIcon = ImageUtils.drawableToBitmap(notification.getLargeIcon().loadDrawable(this));
largeIcon = RenderScriptBlur.blur(getContext(), defauticon, 15);

and here is method ImageUtils.drawableToBitmap

public static Bitmap drawableToBitmap(Drawable drawable) {

        if (drawable == null)
            return Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);

        Bitmap bitmap;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null)
                return bitmapDrawable.getBitmap();
        }

        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0)
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        else
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

and here is Renderscript and method blur

public class RenderScriptBlur {

    private static final float BITMAP_SCALE = 0.4f;
    private static final int BLUR_RADIUS = 7;

    public static Bitmap blur(Context context, Bitmap bitmap) {
        return blur(context, bitmap, BITMAP_SCALE, BLUR_RADIUS);
    }

    public static Bitmap blur(Context context, Bitmap bitmap, float bitmap_scale) {
        return blur(context, bitmap, bitmap_scale, BLUR_RADIUS);
    }

    public static Bitmap blur(Context context, Bitmap bitmap, int blur_radius) {
        return blur(context, bitmap, BITMAP_SCALE, blur_radius);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static Bitmap blur(Context context, Bitmap bitmap, float bitmap_scale, int blur_radius) {
        Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * bitmap_scale),
                Math.round(bitmap.getHeight() * bitmap_scale), false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(blur_radius);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
        rs.destroy();
        bitmap.recycle();

        return outputBitmap;
    }


}

and finally i got this broken image. I dont know why...Please help

broken image:

enter image description here

Soptq
  • 1
  • 2
  • Why not using a [faster technique](http://trickyandroid.com/advanced-blurring-techniques/) which doesn't rely on RenderScript? – Phantômaxx Jan 07 '18 at 10:06
  • @NoiseGenerator Thanks for your reply. Because sometimes I need to blur a 2000*2000 bitmap, using Fastblur will get OOM exception easily. What really confused me is that when I blur a bitmap convered from drawable(resource), it works. But when I blur a bitmap that loaded from icon, it makes the bitmap broken. I thought it is a format problem, but I don't know where the problem is. – Soptq Jan 07 '18 at 14:21
  • Are your sure the input bitmap has ARGB_8888 as config ? – Quentin Menini Jan 11 '18 at 11:29
  • @QuentinMenini yes – Soptq Jan 11 '18 at 16:10

0 Answers0