Create a blur effect image on Xamarin.Android

2

How can I create a blur effect image on Xamarin.Android?

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

What I tried to use without success was the one presented in this question: Link

Any ideas?

    
asked by anonymous 27.07.2017 / 00:04

1 answer

0

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

I tested other methods, which were said to be more agile in Java and, at least for me, were slower than the one 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 the blur behaves, just change the BLUR_RADIUS constant.

    
27.07.2017 / 00:08