Skip to content

Instantly share code, notes, and snippets.

@allenatwork
Created December 1, 2016 07:51
Show Gist options
  • Save allenatwork/3012246a39628df134e947dbb259f3e5 to your computer and use it in GitHub Desktop.
Save allenatwork/3012246a39628df134e947dbb259f3e5 to your computer and use it in GitHub Desktop.
Blue Image
package allen.blurimage;
import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
/**
* Created by Allen on 01-Dec-16.
*/
public class BlurBuilder {
private static final String url = "http://i.imgur.com/QIkGoEp.jpg";
private static final float BITMAP_SCALE = 0.5f;
private static final float BLUR_RADIUS = 25f;
public static Bitmap blur(Context context, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = 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;
}
}
android {
defaultConfig {
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment