Android 高斯模糊 RenderScript封装工具类
来源:互联网 发布:淘宝货到付款 编辑:程序博客网 时间:2024/06/11 22:57
使用RenderScript对图片进行高斯模糊处理封装工具类
样例演示
用法
QuickBlur.with(getApplicationContext()).bitmap(srcBitmap).blur();
兼容API 17以下,在build.gradle中配置
android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 9 targetSdkVersion 19 renderscriptTargetApi 18 renderscriptSupportModeEnabled true }}
Builder构建
public class Builder { Builder(Bitmap bitmap) { mBitmap = bitmap; } /** * 指定模糊前缩小的倍数,默认为8,即1/8的缩放 * * @param scale 缩放的系数 * @return Builder构建模式 */ public Builder scale(int scale) { mScale = 1.0f / scale; return this; } /** * 模糊半径,默认为5 * * @param radius radius值域: (0,25] * @return Builder构建模式 */ public Builder radius(int radius) { mRadius = radius; return this; } public Bitmap blur() { if (mBitmap == null) { throw new RuntimeException("Bitmap can not be null"); } if (mRadius == 0 || mRadius > 25) { throw new RuntimeException("radius must between 0 < r <= 25 "); } return rsBlur(mContext, mBitmap, mRadius, mScale); }}
高斯模糊api
/** * 使用RenderScript 模糊图片 * * @param context 上下文 * @param source 传入的需要被模糊的原图片 * @return 模糊完成后的bitmap */private Bitmap rsBlur(Context context, Bitmap source, int radius, float scale) { Log.d(TAG, "origin size:" + source.getWidth() + "*" + source.getHeight()); // 计算图片缩小后的长宽 int width = Math.round(source.getWidth() * scale); int height = Math.round(source.getHeight() * scale); if (width <= 0 || height <= 0) { Log.d(TAG, "rsBlur: width and height must be > 0"); return source; } // 将缩小后的图片做为预渲染的图片。 //java.lang.IllegalArgumentException: width and height must be > 0 Bitmap inputBmp = Bitmap.createScaledBitmap(source, width, height, false); // 创建RenderScript内核对象 RenderScript rs = RenderScript.create(context); Log.d(TAG, "scale size:" + inputBmp.getWidth() + "*" + inputBmp.getHeight()); // 创建一个模糊效果的RenderScript的工具对象 Element e = Element.U8_4(rs); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, e); // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。 // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。 final Allocation input = Allocation.createFromBitmap(rs, inputBmp); Type type = input.getType(); final Allocation output = Allocation.createTyped(rs, type); // 设置blurScript对象的输入内存 blurScript.setInput(input); // 设置渲染的模糊程度, 25f是最大模糊度 blurScript.setRadius(radius); // 将输出数据保存到输出内存中 blurScript.forEach(output); // 将数据填充到Allocation中 output.copyTo(inputBmp); //Destroy everything to free memory input.destroy(); output.destroy(); blurScript.destroy(); rs.destroy(); return inputBmp;}
RenderScript官方文档
https://developer.android.google.cn/guide/topics/renderscript/compute.html
GitHub地址
https://github.com/fendoudebb/QuickBlur
阅读全文
0 0
- Android 高斯模糊 RenderScript封装工具类
- Android高斯模糊(RenderScript)
- 【android开发】RenderScript 实现高斯模糊。
- 低版本 高斯模糊 android.support.v8.renderscript
- Android学习------使用RenderScript ,ScriptIntrinsicBlur实现高斯模糊
- RenderScript-图片的高斯模糊
- 高斯模糊实现&RenderScript(渲染脚本)
- Android 高斯模糊 renderscript 兼容4.0以下报错android.support.v8.renderscript.RSRuntimeException: Error loadin
- Android 高斯模糊 renderscript 兼容4.0以下报错androi
- Android RenderScript 简单高效实现图片的高斯模糊效果
- Android利用RenderScript对图片处理实现高斯模糊效果。
- 高斯模糊工具类
- 利用RenderScript对图像快速高斯模糊(一)
- 使用RenderScript来实现高斯模糊,兼容低版本
- 使用RenderScript实现高斯模糊(毛玻璃/磨砂)效果
- 使用RenderScript实现高斯模糊(毛玻璃/磨砂)效果
- android毛玻璃效果,模糊视图工具类!类 高斯模糊
- Android RenderScript 高性能计算
- 正则表达式和通配符的区别
- echo命令有趣的颜色参数
- Android通过Enable动态设置color—color@res@Android
- 微信的图片查看
- maven 编译 注意
- Android 高斯模糊 RenderScript封装工具类
- Linux下安装Git
- java反射详解
- Android输入子系统分析(1)
- Git学习
- 进程间通讯的方式
- jQuery实战之单击改变行的内容
- Expected int32, got list containing Tensors of type '_Message' instead.
- MSEC是什么