Create an image with Blur effect in Xamarin.Android

Asked

Viewed 101 times

2

How can I create an image with Blur effect in Xamarin.Android?

I just found solutions in Java and nothing really functional and agile for Xamarin.Android.

What I tried to use unsuccessfully was presented in this question: Link

Any idea?

1 answer

0

I just converted the Java code to Xamarin.Android. It’s agile and perfectly functional.

I tested other methods, which said they were more agile in Java and, at least for me, were slower than the one presented below.

private const float BITMAP_SCALE = 0.4f;
private const float BLUR_RADIUS = 7.5f;

/// <summary>
/// Cria uma imagem com efeito de blur
/// </summary>
/// <param name="context">Contexto</param>
/// <param name="image">Objeto 'Bitmap' com a imagem</param>
/// <returns></returns>
private Android.Graphics.Bitmap CreateBlurredImage(Context context, Android.Graphics.Bitmap image)
{
      int width = (int)Math.Round(image.Width * BITMAP_SCALE);
      int height = (int)Math.Round(image.Height * BITMAP_SCALE);

      Android.Graphics.Bitmap inputBitmap = Android.Graphics.Bitmap.CreateScaledBitmap(image, width, height, false);
      Android.Graphics.Bitmap outputBitmap = Android.Graphics.Bitmap.CreateBitmap(inputBitmap);

      RenderScript rs = RenderScript.Create(context);
      ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
      Allocation tmpIn = Allocation.CreateFromBitmap(rs, inputBitmap);
      Allocation tmpOut = Allocation.CreateFromBitmap(rs, outputBitmap);
      theIntrinsic.SetRadius(BLUR_RADIUS);
      theIntrinsic.SetInput(tmpIn);
      theIntrinsic.ForEach(tmpOut);
      tmpOut.CopyTo(outputBitmap);

      return outputBitmap;
}

If you want to change the way Blur behaves, just change the BLUR_RADIUS constant.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.