Pergunta

I am trying to blur bitmaps in my app using RenderScript framework. I am using the following code:

public static Bitmap apply(Context context, Bitmap sentBitmap, int radius)
{
    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    final RenderScript rs = RenderScript.create(context);
    final Allocation input = Allocation.createFromBitmap(rs, sentBitmap,
                                                         Allocation.MipmapControl.MIPMAP_NONE,
                                                         Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(bitmap);
    return bitmap;
}

Unfortunately all I get with the code is black bitmaps. How can I fix the issue?

Bitmaps passed to the apply method are created in the following way:

Bitmap b = Bitmap.createBitmap(thisView.getWidth(),
                               thisView.getHeight(),
                               Bitmap.Config.ARGB_8888);

The width and height of these bitmaps are multiples of 4.

There are also some errors reported by RenderScript but I don't know what they means and how should I fix them (the documentation for ScriptIntrinsicBlur is rather thin). Here are these errors:

20305-20391/com.xxx E/RenderScript﹕ rsi_ScriptIntrinsicCreate 5
20305-20391/com.xxx E/RenderScript﹕ rsAssert failed: mUserRefCount > 0, in 
frameworks/rs/rsObjectBase.cpp at 112

EDIT:

The radius is 5 and I am running the app on Samsung Galaxy Nexus with Android 4.2.1.

Foi útil?

Solução 2

Thanks to @Tim Murray, I fixed the issue (there were two actually)

I switched to using the support library and now I hope Android Studio with gradle projects will eventually learn to handle the library symbols.

Another major source of problems was the fact I used completely transparent bitmaps as input for the ScriptIntrinsicBlur. My bad.

EDIT from March-07-2013

Android Studio 0.5 fixes issues with support RenderScript in Gradle-powered projects.

Outras dicas

Use this function to blur your input bitmap image:

Bitmap BlurImage(Bitmap input) {
            RenderScript rsScript = RenderScript.create(this);
            Allocation alloc = Allocation.createFromBitmap(rsScript, input);

            ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rsScript, alloc.getElement());
            blur.setRadius(12);
            blur.setInput(alloc);

            Bitmap result = Bitmap.createBitmap(input.getWidth(), input.getHeight(), input.getConfig());
            Allocation outAlloc = Allocation.createFromBitmap(rsScript, result);
            blur.forEach(outAlloc);
            outAlloc.copyTo(result);

            rsScript.destroy();
            return result;
        }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top