Search in sources :

Example 11 with RenderScript

use of android.support.v8.renderscript.RenderScript in project BlurView by Dimezis.

the class RenderScriptBlur method blur.

/**
     * @param bitmap     bitmap to blur
     * @param blurRadius blur radius (1..25)
     * @return blurred bitmap
     */
@Override
public final Bitmap blur(Bitmap bitmap, float blurRadius) {
    //Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag
    Allocation inAllocation = Allocation.createFromBitmap(renderScript, bitmap);
    Bitmap outputBitmap;
    if (canModifyBitmap) {
        outputBitmap = bitmap;
    } else {
        outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    }
    if (!canReuseAllocation(bitmap)) {
        if (outAllocation != null) {
            outAllocation.destroy();
        }
        outAllocation = Allocation.createTyped(renderScript, inAllocation.getType());
        lastBitmapWidth = bitmap.getWidth();
        lastBitmapHeight = bitmap.getHeight();
    }
    blurScript.setRadius(blurRadius);
    blurScript.setInput(inAllocation);
    //do not use inAllocation in forEach. it will cause visual artifacts on blurred Bitmap
    blurScript.forEach(outAllocation);
    outAllocation.copyTo(outputBitmap);
    inAllocation.destroy();
    return outputBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) Allocation(android.support.v8.renderscript.Allocation)

Example 12 with RenderScript

use of android.support.v8.renderscript.RenderScript in project ngAndroid by davityle.

the class DefaultBlur method blurBitmap.

private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context) {
    RenderScript rs = RenderScript.create(context);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);
    final Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(blurRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
Also used : RenderScript(android.support.v8.renderscript.RenderScript) Bitmap(android.graphics.Bitmap) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Aggregations

Bitmap (android.graphics.Bitmap)11 RenderScript (android.support.v8.renderscript.RenderScript)9 Allocation (android.support.v8.renderscript.Allocation)8 ScriptIntrinsicBlur (android.support.v8.renderscript.ScriptIntrinsicBlur)7 BitmapFactory (android.graphics.BitmapFactory)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Canvas (android.graphics.Canvas)2 Paint (android.graphics.Paint)2 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1 RSRuntimeException (android.support.v8.renderscript.RSRuntimeException)1 CornerView (carbon.widget.CornerView)1