Android 模糊处理RenderScript

时间:2019-03-25
本文章向大家介绍Android 模糊处理RenderScript,主要包括Android 模糊处理RenderScript使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

RendScript 是一种低级的高性能编程语言,用于3D渲染和处理密集型计算(3D播放等和关于CPU密集型的计算)。功能很强大,玩得好要熟悉计算机图像。但自带模糊接口。

public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) {
    //Create renderscript
    RenderScript rs = RenderScript.create(context);
    //Create allocation from Bitmap
    Allocation allocation = Allocation.createFromBitmap(rs, bitmap);
    Type t = allocation.getType();
    //Create allocation with the same type
    Allocation blurredAllocation = Allocation.createTyped(rs, t);
    //Create script
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    //Set blur radius (maximum 25.0)
    blurScript.setRadius(radius);
    //Set input for script
    blurScript.setInput(allocation);
    //Call script for output allocation
    blurScript.forEach(blurredAllocation);
    //Copy script result into bitmap
    blurredAllocation.copyTo(bitmap);
    //Destroy everything to free memory
    allocation.destroy();
    blurredAllocation.destroy();
    blurScript.destroy();
    //t.destroy();原本是有这一行的。添加直接报错。网上没找到说法,去掉能直接运行。
    rs.destroy();
    return bitmap;
}

上面是抄的代码,实测有效。红色部分略有疑问。