【问题标题】:Blurring part of the image模糊图像的一部分
【发布时间】:2017-09-20 20:18:06
【问题描述】:

我尝试使用 RenderScript 来模糊图像,它确实有效。我想知道如何使用 RenderScript 来模糊图像的一部分。我尝试了下面的代码,但它不起作用:

 Bitmap overlay = Bitmap.createBitmap(
            mWidth,
            mHeight,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(overlay);

    canvas.drawBitmap(bitmap, -mletf,
            -mTop, null);

    RenderScript rs = RenderScript.create(mContext);

    Allocation overlayAlloc = Allocation.createFromBitmap(
            rs, overlay);

    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(
            rs, overlayAlloc.getElement());

    blur.setInput(overlayAlloc);

    blur.setRadius(mRadius);

    blur.forEach(overlayAlloc);

    overlayAlloc.copyTo(overlay);




    rs.destroy();
    return  overlay;

变量mHeightmWidth 是要模糊的部分的高度和宽度,其mTopmletf 是应该开始模糊的位置。

【问题讨论】:

标签: android renderscript blurry


【解决方案1】:

使用 LaunchOptions API:

LaunchOptions lo = new LaunchOptions();
lo.setX(mLeft, mLeft+mWidth);
lo.setY(mTop, mTop+mHeight);

blur.forEach(overlayAlloc, lo);

【讨论】:

    【解决方案2】:

    使用这个库enter link description here

    defaultConfig {
    applicationId "com.javatechig"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    
    // Add the following two lines
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
    

    }

    以下代码 sn-ps 可用于在 Android 中使用 RenderScript API 创建位图模糊效果。

    //Set the radius of the Blur. Supported range 0 < radius <= 25
    private static final float BLUR_RADIUS = 25f;
    
    public Bitmap blur(Bitmap image) {
        if (null == image) return null;
    
        Bitmap outputBitmap = Bitmap.createBitmap(image);
        final RenderScript renderScript = RenderScript.create(this);
        Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
        Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);
    
        //Intrinsic Gausian blur filter
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
        return outputBitmap;
    }
    

    你可以使用上面的代码sn-p来模糊一个ImageView,如下所示。

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nature);
    Bitmap blurredBitmap = blur(bitmap);
    imageView.setImageBitmap(blurredBitmap);
    

    也关注这个链接enter link description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-03
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2017-12-21
      相关资源
      最近更新 更多