使用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

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android]Android(安卓)线程优先级修改
  6. Android的Listview用法总结和Adapter的使用
  7. Android中使用cmwap接入点访问互联网的问题及解决办法
  8. android代码分析,及Terminal使用
  9. Android中知识点整理

随机推荐

  1. Android(安卓)小项目之--数据存储【Files
  2. Android(安卓)html5和Android之间的交互
  3. Android人與iPhone人大不同
  4. android下歌曲名称乱码的解决办法
  5. 详细讲解如何安装原生安卓电子市场(androi
  6. android ”三分屏”视频播放器
  7. android 开发布局之AbsoluteLayout
  8. Android Training - 网络操作(3) - 解析X
  9. Android市场动态呈现 最新数据抢先看
  10. 八款开源Android游戏引擎(转)