Example usage for android.renderscript Allocation createFromBitmap

List of usage examples for android.renderscript Allocation createFromBitmap

Introduction

In this page you can find the example usage for android.renderscript Allocation createFromBitmap.

Prototype

static public Allocation createFromBitmap(RenderScript rs, Bitmap b) 

Source Link

Document

Creates an Allocation from a android.graphics.Bitmap .

Usage

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);/*from w  w w  .  ja v a  2  s.  c o  m*/
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}

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);//w ww  .  ja va  2 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);//www  .ja v a2  s. c o  m

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

    allOut.copyTo(outBitmap);

    bitmap.recycle();
    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);/*from ww  w. j  av  a2 s .c o  m*/
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}

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);//  ww w  . j av  a  2  s  . c om

    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

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);// ww w  .  ja v  a  2 s  . c o m

    //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 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   w w  w  .  j  a v a 2  s. c  om

    //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

@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);//ww w.  ja v a  2s .  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 .j ava  2s  . 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;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blur(Context context, Bitmap bitmap) {

    //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 w w  .j  a  v a2 s  .  co  m

    //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;
}