Android自带Rendenscript高效率处理图像

RenderScript是Android系统中能高效处理大量计算任务的框架,特别适用一些需要处理图片和加载图片以及计算机视觉的方面应用。它是Android平台的一种类C脚本语言,文件名为xxx.rs。后期google 会不断扩展这个引擎让其支持更多的精密计算。

github代码直通车

引入方式:

    defaultConfig {
        applicationId "com.example.renderscript.renderscriptuse"
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        renderscriptTargetApi 18
        renderscriptSupportModeEnabled true
    }
  • renderscriptTargetApi – 指定生成的字节码版本。
  • renderscriptSupportModeEnabled – 如果运行的设备不支持该目标版本,那么可以指定生成的字节码回落到一个兼容的版本。
  • 考虑到兼容性,在使用RenderScript的类中,要引入:import android.support.v8.renderscript.*;
高斯模糊功能:

效果:

《Android自带Rendenscript高效率处理图像》 我的女神李若彤

    public static Bitmap handleGlassblur(Context context, Bitmap originBitmap, int radius){
        RenderScript renderScript = RenderScript.create(context);
        Allocation input = Allocation.createFromBitmap(renderScript,originBitmap);
        Allocation output = Allocation.createTyped(renderScript,input.getType());
        ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        scriptIntrinsicBlur.setRadius(radius);
        scriptIntrinsicBlur.setInput(input);
        scriptIntrinsicBlur.forEach(output);
        output.copyTo(originBitmap);

        renderScript.destroy();
        input.destroy();
        output.destroy();

        return originBitmap;
    }

radius设置模糊度,范围1-25

  1. 创建一个原图Allcation(配置,相当于容器),创建一个新图Allcation
  2. 创建ScriptIntrinsicBlur处理类,设置模糊度radius,将Allocation中的产品输出
  3. 释放资源

调用:

    private void blur() {
        imageviewBlur = (ImageView) findViewById(R.id.imageview);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.test);
        Bitmap blurBitmap = HandleUtils.handleGlassblur(getApplicationContext(),bitmap,15);
        imageviewBlur.setImageBitmap(blurBitmap);
    }

最早我使用的是fastblur,那计算速度明显感觉到倒数了3秒才出来的。rendenscript处理速度是fastblur的8倍。

加强版高斯模糊

默认使用的模糊度不够,可以通过缩小原图的像素值,达到更大的模糊度。调用handleGlassblur()方法之前生成更小比例的bitmap。scaleRatio为缩小比例

调用:

    private void scale(){
        imageviewScale = (ImageView) findViewById(R.id.imageview2);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.test);
        Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 8, bitmap.getHeight() / 8, false);
        Bitmap blurBitmap = HandleUtils.handleGlassblur(getApplicationContext(),newBitmap,15);
        imageviewScale.setImageBitmap(blurBitmap);
    }

效果:

《Android自带Rendenscript高效率处理图像》 我的女神李若彤

放大镜效果

《Android自带Rendenscript高效率处理图像》 giphy.gif

我们编写完rs文件之后,就会在gen目录下,生成一个ScriptC_xxx.java文件,如下

《Android自带Rendenscript高效率处理图像》 rs文件位置

简单自定义Imageview,点击放大区域,也可以改成ACTION_MOVE事件,拖动实时放大查看。

public class MirrorScaleImageView extends AppCompatImageView{
    private Bitmap bitmap;


    public MirrorScaleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.test);
        setImageBitmap(bitmap);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                int x = (int) event.getX();
                int y = (int) event.getY();
                bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.mipmap.test);
                Bitmap newBitmap = scaleMirror(bitmap,x,y,200,2,getContext());
                setImageBitmap(newBitmap);
                break;
        }
        return true;
    }

    /**
     *
     * @param bitmap
     * @param x  圆心x
     * @param y  圆心y
     * @param radius  圆半径
     * @param scale  放大倍数
     * @param context
     * @return
     */
    private Bitmap scaleMirror(Bitmap bitmap, int x, int y, int radius, int scale, Context context){
        RenderScript rs = RenderScript.create(context);

        Allocation in = Allocation.createFromBitmap(rs, bitmap);
        Allocation out = Allocation.createTyped(rs,in.getType());


        ScriptC_magnifier magnifier = new ScriptC_magnifier(rs);

        magnifier.set_inputAllocation(in);
        magnifier.set_atX(x);
        magnifier.set_atY(y);
        magnifier.set_radius(radius);
        magnifier.set_scale(scale);


        magnifier.forEach_magnify(in,out);

        out.copyTo(bitmap);

        rs.destroy();
        magnifier.destroy();
        in.destroy();
        out.destroy();

        return bitmap;
    }
}

素描效果:

《Android自带Rendenscript高效率处理图像》 image.png

    public static Bitmap blackHandle(Bitmap bitmap, Context context){
        RenderScript renderScript = RenderScript.create(context);
        ScriptC_sketch sketchScript = new ScriptC_sketch(renderScript);

        Allocation in = Allocation.createFromBitmap(renderScript,bitmap);
        Allocation out = Allocation.createTyped(renderScript,in.getType());

        sketchScript.forEach_invert(in,out);

        out.copyTo(bitmap);

        renderScript.destroy();
        sketchScript.destroy();
        in.destroy();
        out.destroy();

        return bitmap;
    }

RenderScript功能会

    原文作者:奔跑吧李博
    原文地址: https://www.jianshu.com/p/bc5df96c8e4f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞