Android RenderScript高斯模糊


          今天看代码的时候,看到了其中有.rs结尾的文件,不是很明白,还有RenderScript类,看的一脸蒙蔽,不知所云,然后百度了一下,收货还真不少,这东西在图形处理这块用处挺大的。

             今天先说说ScriptIntrinsicBlur,这个类不需要定义rs文件,从这个Intrinsic单词可以看的出来,它是API17以后内置的类,专门用来处理图像的,让图片变模糊。


public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) {
        //创建渲染脚本上下文
        RenderScript rs = RenderScript.create(context);

        //为位图分配内存
        Allocation allocation = Allocation.createFromBitmap(rs, bitmap);

        Type t = allocation.getType();

        //用同样的类型创建内存,一般用这两种方式创建 Allocation
        Allocation blurredAllocation = Allocation.createTyped(rs, t);

        //创建高斯渲染脚本  
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        //设置模糊半径 (maximum 25.0)
        blurScript.setRadius(radius);
        //为脚本设置输入参数 
        blurScript.setInput(allocation);
        //调用脚本 结果存入 blurredAllocation中
        blurScript.forEach(blurredAllocation);

        //把脚本结果存入位图中  因为为native层渲染,所以结果需要复制到上层
        blurredAllocation.copyTo(bitmap);

        //Destroy everything to free memory
        allocation.destroy();
        blurredAllocation.destroy();
        blurScript.destroy();
        t.destroy();

        return bitmap;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值