Example usage for android.renderscript ScriptIntrinsicBlur create

List of usage examples for android.renderscript ScriptIntrinsicBlur create

Introduction

In this page you can find the example usage for android.renderscript ScriptIntrinsicBlur create.

Prototype

public static ScriptIntrinsicBlur create(RenderScript rs, Element e) 

Source Link

Document

Create an intrinsic for applying a blur to an allocation.

Usage

From source file:Main.java

public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    blurScript.setRadius(8f);//from w w  w  .j ava2 s .  c  om
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(outBitmap);
    bitmap.recycle();
    rs.destroy();
    return outBitmap;
}

From source file:Main.java

public static Bitmap blurBitmap(Context context, Bitmap bitmap, float radius) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, bitmap);

    blurScript.setRadius(radius);//from   w  ww  . j  a  v  a 2  s  . com

    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    allOut.copyTo(outBitmap);

    bitmap.recycle();
    rs.destroy();

    return outBitmap;
}

From source file:Main.java

public static Bitmap blurBitmap(Bitmap bitmap, Context context) {

    //Let's create an empty bitmap with the same size of the bitmap we want to blur  
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    //Instantiate a new Renderscript  
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript  
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps  
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur  
    blurScript.setRadius(25.f);/*w ww . ja  va 2 s  .c  om*/

    //Perform the Renderscript  
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap  
    allOut.copyTo(outBitmap);

    //recycle the original bitmap  
    bitmap.recycle();

    //After finishing everything, we destroy the Renderscript.  
    rs.destroy();

    return outBitmap;

}

From source file:Main.java

public static Bitmap blurLight(Context context, Bitmap image) {
    int width = Math.round(image.getWidth() * BITMAP_SCALE_LIGHT);
    int height = Math.round(image.getHeight() * BITMAP_SCALE_LIGHT);

    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_LIGHT);
    theIntrinsic.setInput(tmpIn);//ww w. j ava2 s.  c  om
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

From source file:Main.java

public static Bitmap blur(final Context context, final Bitmap image) {
    if (null == image)
        return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(context);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

    //Intrinsic Gausian blur filter
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    theIntrinsic.setRadius(BLUR_RADIUS);
    theIntrinsic.setInput(tmpIn);// w w w  .j  av  a 2 s. co m
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}

From source file:Main.java

public static Bitmap blurBitmap(Context context, Bitmap bitmap, float blur) {
    Bitmap bitmapCopy = bitmap.copy(bitmap.getConfig(), false);
    //Let's create an empty bitmap with the same size of the bitmap we want to blur
    Bitmap outBitmap = (Bitmap.createBitmap(bitmapCopy.getWidth(), bitmapCopy.getHeight(),
            Bitmap.Config.ARGB_8888));/*from   www . ja  v  a  2 s .  co  m*/

    //Instantiate a new Renderscript
    RenderScript rs = RenderScript.create(context);

    //Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    //Create the in/out Allocations with the Renderscript and the in/out bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, bitmapCopy);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

    //Set the radius of the blur
    blurScript.setRadius(blur);

    //Perform the Renderscript
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);

    //Copy the final bitmap created by the out Allocation to the outBitmap
    allOut.copyTo(outBitmap);

    bitmapCopy.recycle();
    //After finishing everything, we destroy the Renderscript.
    rs.destroy();
    return outBitmap;
}

From source file:Main.java

public static Bitmap makeBlur(Context context, Bitmap sentBitmap, int radius) {

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, sentBitmap,
                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(radius);/*from  w  ww. j a  v a2s.  c  o  m*/
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        return bitmap;
    }
    return null;
}

From source file:Main.java

public static void blur(Context context, Bitmap bkg, View view, int width, int height) {
    float radius = 20;
    Log.i("hulixia", bkg.getWidth() + "w,h" + bkg.getDensity());
    Bitmap overlay = Bitmap.createBitmap(bkg.getWidth(), bkg.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft(), -view.getTop());
    canvas.drawBitmap(bkg, 0, 0, null);//from ww  w  .  ja v a2s  . c o  m

    RenderScript rs = RenderScript.create(context);

    Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);
    view.setBackground(new BitmapDrawable(context.getResources(), overlay));
    rs.destroy();
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
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);//from   w  w  w  .ja  va  2 s  .c o  m
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

From source file:Main.java

/**
 * https://futurestud.io/blog/how-to-blur-images-efficiently-with-androids-renderscript
 * https://developer.xamarin.com/recipes/android/other_ux/drawing/blur_an_image_with_renderscript/
 *
 * @param context//from   w w  w  . ja v  a2 s  .c  o  m
 * @param image      original bitmap
 * @param width      output bitmap width
 * @param height     output bitmap height
 * @param blurRadius blur radius ;  blurRadius in section (0,25]
 * @return
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(Context context, Bitmap image, int width, int height, int blurRadius) {

    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(blurRadius);
    theIntrinsic.setInput(tmpIn);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    rs.destroy();
    return outputBitmap;
}