Android 高斯模糊 RenderScript封装工具类

使用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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值