使用RenderScript对图片进行高斯模糊处理封装工具类

样例演示

Android 高斯模糊 RenderScript封装工具类_第1张图片

用法

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

更多相关文章

  1. android 如何查找内存泄漏
  2. Android获取手机本地图片缩略图
  3. android之内存管理的文章收集
  4. android imageswitcher gallery 根据数据库内图片名字进行查看/
  5. 初学Android,五大布局对象(六)
  6. Android OkHttp3 上传多张图片
  7. Android中TextView中加图片,超链接,一部分字或者背景变色。不断补
  8. flutter包名、应用名称、图标、启动图片修改
  9. 关于Android堆内存的设置

随机推荐

  1. 全新 Android(安卓)注入器:Dagger 2(一)
  2. android system setup and building (2)
  3. 配置android环境
  4. Android(安卓)组件 ContentProvider 内容
  5. Android中focusable属性的妙用——底层按
  6. android 开发环境搭建
  7. adroid 现行布局水平居中位置
  8. TextView设置字间距和行间距
  9. Android(安卓)AIDL使用详解
  10. Android(安卓)运行崩溃找不到so包解决方