nihasmata
nihasmata

Reputation: 692

More Blurred With RenderScript In Android

For my project i want to use blurred backgrounds.When i use the following method,it does my background blur but it is not blurred enough for me,i want to do more blurry background.I set the radius as its maximum value 25.Someone can help me ?

private static final float BITMAP_SCALE = 0.9f;
private static final float BLUR_RADIUS = 25.0f;

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, 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);

    return outputBitmap;


}

Upvotes: 1

Views: 2025

Answers (1)

Miao Wang
Miao Wang

Reputation: 1130

If using a blur radius of 25 is still not enough, one cheap way to do blur is to resize the background image down and then resize it up.

private static final float BITMAP_SCALE = 0.9f;
private static final float RESIZE_SCALE = 1.f/5.f;
private static RenderScript rs;

public static Bitmap blur(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE);
    int height = Math.round(image.getHeight() * BITMAP_SCALE);

    Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    if (rs == null) {
        // Creating a RS context is expensive, better reuse it.
        rs = RenderScript.create(context);
    }
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    Type t = Type.createXY(mRS, tmpIn.getElement(), width*RESIZE_SCALE, height*RESIZE_SCALE);
    Allocation tmpScratch = Allocation.createTyped(rs, t);

    ScriptIntrinsicResize theIntrinsic = ScriptIntrinsicResize.create(rs);
    // Resize the original img down.
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach_bicubic(tmpScratch);
    // Resize smaller img up.
    theIntrinsic.setInput(tmpScratch);
    theIntrinsic.forEach_bicubic(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

Upvotes: 1

Related Questions