Skip to content

Instantly share code, notes, and snippets.

@jaganjan
Created November 23, 2015 10:49
Show Gist options
  • Save jaganjan/ac6dd3a8f71ce949da99 to your computer and use it in GitHub Desktop.
Save jaganjan/ac6dd3a8f71ce949da99 to your computer and use it in GitHub Desktop.
Picasso blur transformation using render script xamarin android
public class BlurTransformation : Java.Lang.Object, ITransformation
{
/**
* Max blur Radius supported by the Renderscript library
**/
protected static int MaxRadius = 25;
/**
* Min blur Radius supported by the Renderscript library
**/
protected static int MinRadius = 1;
/**
* Application Context to instantiate the Renderscript
**/
protected Context Context;
/**
* Selected Radius
**/
protected int Radius;
/**
* Creates a new Blur transformation
*
* @param Context Application Context to instantiate the Renderscript
**/
public BlurTransformation(Context context, int radius)
{
this.Context = context;
this.Radius = radius < MinRadius
? MinRadius
: radius > MaxRadius ? MaxRadius : radius;
}
public Bitmap Transform(Bitmap source)
{
// Load a clean bitmap and work from that.
var originalBitmap = source;
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = null;
blurredBitmap = Bitmap.CreateBitmap(originalBitmap);
// Create the Renderscript instance that will do the work.
var rs = RenderScript.Create(Context);
// Allocate memory for Renderscript to work with
var input = Allocation.CreateFromBitmap(rs, originalBitmap, Allocation.MipmapControl.MipmapFull,
AllocationUsage.Script);
var output = Allocation.CreateTyped(rs, input.Type);
// Load up an instance of the specific script that we want to use.
var script = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs));
script.SetInput(input);
// Set the blur radius
script.SetRadius(Radius);
// Start the ScriptIntrinisicBlur
script.ForEach(output);
// Copy the output to the blurred bitmap
output.CopyTo(blurredBitmap);
source.Recycle();
return blurredBitmap;
}
public string Key => "blurred";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment