How to rewrite RenderScript code in Android

81 views Asked by At

I have this piece of code in Android that given an int value, performs some image processing (blur effect) using RenderScript. While it is super fast and efficient, it is deprecated so I am looking for other alternatives. What's another efficient way to rewrite it? Probably a GPU-based solution with similar or even faster performance?

I tried RenderEffect but couldn't find a way to apply it on a Bitmap and not ImageView.

private void processImage (int value)
{
    try
    {
        RenderScript rsScript = RenderScript.create(context);
        Allocation alloc = Allocation.createFromBitmap(rsScript, bitmap);

        ScriptIntrinsicBlur blurEffect = ScriptIntrinsicBlur.create(rsScript, Element.U8_4(rsScript));
        blurEffect.setRadius(value);
        blurEffect.setInput(alloc);

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Allocation outAlloc = Allocation.createFromBitmap(rsScript, output);

        blurEffect.forEach(outAlloc);
        outAlloc.copyTo(output);

        rsScript.destroy();
        output.getPixels(bitmap, 0, width, 0, 0, width, height);
    }
    catch (Exception e) {
    }
}
1

There are 1 answers

3
Stephen Hines On

https://developer.android.com/guide/topics/renderscript/migrate#built-in is a great way to migrate off of the ScriptIntrinsic functionality to something that is supported. There's other information on that page too about how to convert more complex examples. Good luck! (And thank you for actually heeding the deprecation warning)