我可以在android中模糊位图的特定区域吗?

时间:2015-12-16 13:26:53

标签: android bitmap blur

我想模糊位图的特定区域。对于例如我有200x400分辨率的位图,并希望从中间模糊它,如从100x300点到50点。

有人有想法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用 RenderScript 来模糊Android中的位图。

RenderScript对于执行图像处理,计算摄影或计算机视觉的应用程序尤其有用。我们可以通过两种方式访问​​Android RenderScript框架API:

直接使用android.renderscript API类。这些类可从Android 3.0(API级别11)及更高版本获得。 或者,您可以使用android.support.v8.renderscript支持包类。支持库类适用于运行Android 2.2(API级别8)及更高版本的设备。

要使用支持库RenderScript API,您必须拥有Android SDK Tools版本22.2或更高版本以及SDK Build-tools版本18.1.0或更高版本

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

//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;
}

您可以使用上面的代码段来模糊ImageView,如下所示。

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

希望它会对你有所帮助。

答案 1 :(得分:0)

渲染脚本不会在每个API级别都有效,因此请使用此库在图像上https://android-arsenal.com/details/1/2192

进行模糊处理