Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ScriptIntrinsicBlur doesn't blur the whole image and generates a transparent Edge

I have some problems with the RenderScript ScriptIntrinsic Blur - on some devices it doesn't blur the whole image. I downscale the input image and make sure that the width is a multiple of 4 (because it's suggested by Roman Nurik: https://plus.google.com/+RomanNurik/posts/TLkVQC3M6jW)

@SuppressLint("NewApi")
private Bitmap blurRenderScript(Bitmap smallBitmap) {

    Bitmap output = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(), smallBitmap.getConfig());

    RenderScript rs = RenderScript.create(getContext());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation inAlloc = Allocation.createFromBitmap(rs, smallBitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE);
    Allocation outAlloc = Allocation.createFromBitmap(rs, output);
    script.setRadius(BLUR_RADIUS);
    script.setInput(inAlloc);
    script.forEach(outAlloc);
    outAlloc.copyTo(output);

    rs.destroy();

    MutableBitmap.delete(smallBitmap);

    return output;
}

It is working on a Nexus 4:

Nexus 4 before blurringNexus 4 after blurring

But on a Galaxy S4 the right side has a transparent edge:

Galaxy S4 before blurringGalaxy S4 after blurring

I hope you can see what I mean - if you open the picture in gimp or so you can see it better. It doesn't depend on the picture size. I Also tried it with bigger and smaller images, and the result was always the same. It also happens on a Nexus 7 2012 for example. Also, the transparent artifacts are sometimes on the bottem or left edge. Thanks in advance for your help!

Nexus 4: 4.4.2/Build Number KOT49H Galaxy S4: 4.2.2/Build Number JDQ39.I9505XXUDMGG

like image 578
Romanski Avatar asked Mar 13 '14 09:03

Romanski


2 Answers

I'm not sure why this might work, but for me it did..

Try instead of

    Bitmap output = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(), smallBitmap.getConfig());

pass in Bitmap.Config.ARGB_8888 like so

    Bitmap output = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(), Bitmap.Config.ARGB_8888);
like image 200
Matthew Runo Avatar answered Nov 15 '22 02:11

Matthew Runo


I am assuming you are using android.renderscript instead of the support lib android.v8.renderscript. (or a old support lib?)

If that is true, then it is a known bug that can only be fixed by a system upgrade, which may not be feasible to the users.

One way to workaround this is to use RenderScript Support lib: you can find the detailed instructions here:http://developer.android.com/guide/topics/renderscript/compute.html#access-rs-apis

Basically you just need to change the import:

import android.support.v8.renderscript.*;

And config your build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19

        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true

    }
}

There are several advantages of using support lib:

  1. Support devices back to API 8.
  2. Can be updated with Build-Tools to incorporate bugs fixes and workaround.

On the downside, to achieve this, it needs to bundle several shared libraries which will make your app beefier.

like image 2
Miao Wang Avatar answered Nov 15 '22 02:11

Miao Wang