Search in sources :

Example 16 with RenderScript

use of android.renderscript.RenderScript in project DevRing by LJYcoder.

the class ImageUtil method rsBlur.

/**
 * 使用RenderScript实现的高斯模糊效果(性能较高,模糊半径0-25,越大越模糊)
 * 需在module下的build.gradle中加入以下配置才可使用
 * renderscriptTargetApi 19
 * renderscriptSupportModeEnabled true
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlur(Context context, Bitmap bitmap, int radius) throws RSRuntimeException {
    RenderScript rs = null;
    try {
        rs = RenderScript.create(context);
        Allocation input = Allocation.createFromBitmap(rs, bitmap);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blur.setRadius(radius);
        blur.setInput(input);
        blur.forEach(output);
        output.copyTo(bitmap);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return bitmap;
}
Also used : RenderScript(android.renderscript.RenderScript) Allocation(android.renderscript.Allocation) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur) TargetApi(android.annotation.TargetApi)

Example 17 with RenderScript

use of android.renderscript.RenderScript in project MVP by yuchengren.

the class BlurUtil method rsBlur.

public static Bitmap rsBlur(Context context, Bitmap bitmap, float radius) {
    // 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(), bitmap.getConfig());
    // Instantiate a new Renderscript
    RenderScript rs = RenderScript.create(context.getApplicationContext());
    // 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: 0 < radius <= 25
    blurScript.setRadius(radius);
    // 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;
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.renderscript.RenderScript) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur) Allocation(android.renderscript.Allocation)

Example 18 with RenderScript

use of android.renderscript.RenderScript in project EnableHands by LeviWGG.

the class ImageUtils method renderScriptBlur.

/**
 * Return the blur bitmap using render script.
 *
 * @param src     The source of bitmap.
 * @param radius  The radius(0...25).
 * @param recycle True to recycle the source of bitmap, false otherwise.
 * @return the blur bitmap
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) final float radius, final boolean recycle) {
    RenderScript rs = null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    try {
        rs = RenderScript.create(BaseUtils.getApp());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, ret, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(ret);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return ret;
}
Also used : RenderScript(android.renderscript.RenderScript) Bitmap(android.graphics.Bitmap) Allocation(android.renderscript.Allocation) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur) TargetApi(android.annotation.TargetApi)

Example 19 with RenderScript

use of android.renderscript.RenderScript in project EnableHands by LeviWGG.

the class ImageUtils method renderScriptBlur.

/**
 * Return the blur bitmap using render script.
 *
 * @param src     The source of bitmap.
 * @param radius  The radius(0...25).
 * @param recycle True to recycle the source of bitmap, false otherwise.
 * @return the blur bitmap
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(final Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) final float radius, final boolean recycle) {
    RenderScript rs = null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    try {
        rs = RenderScript.create(MyApplication.getMyContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, ret, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(ret);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return ret;
}
Also used : RenderScript(android.renderscript.RenderScript) Bitmap(android.graphics.Bitmap) Allocation(android.renderscript.Allocation) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur) TargetApi(android.annotation.TargetApi)

Example 20 with RenderScript

use of android.renderscript.RenderScript in project android_frameworks_base by DirtyUnicorns.

the class ColorUtils method blurBitmap.

public static Bitmap blurBitmap(Context context, Bitmap bmp, int radius) {
    Bitmap out = Bitmap.createBitmap(bmp);
    RenderScript rs = RenderScript.create(context);
    Allocation input = Allocation.createFromBitmap(rs, bmp, MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);
    script.setRadius(radius);
    script.forEach(output);
    output.copyTo(out);
    rs.destroy();
    return out;
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.renderscript.RenderScript) Allocation(android.renderscript.Allocation) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur)

Aggregations

RenderScript (android.renderscript.RenderScript)51 Allocation (android.renderscript.Allocation)44 ScriptIntrinsicBlur (android.renderscript.ScriptIntrinsicBlur)41 Bitmap (android.graphics.Bitmap)37 SuppressLint (android.annotation.SuppressLint)12 TargetApi (android.annotation.TargetApi)10 BitmapDrawable (android.graphics.drawable.BitmapDrawable)4 Type (android.renderscript.Type)4 BitmapFactory (android.graphics.BitmapFactory)3 Paint (android.graphics.Paint)3 RequiresApi (android.support.annotation.RequiresApi)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Canvas (android.graphics.Canvas)2 ScriptIntrinsicYuvToRGB (android.renderscript.ScriptIntrinsicYuvToRGB)2 Point (android.graphics.Point)1 RSRuntimeException (android.renderscript.RSRuntimeException)1 Script (android.renderscript.Script)1 NonNull (androidx.annotation.NonNull)1 RequiresApi (androidx.annotation.RequiresApi)1