Search in sources :

Example 11 with Allocation

use of android.renderscript.Allocation in project Shuttle by timusus.

the class RSBlur method blur.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public Bitmap blur(Bitmap bitmap, int radius) throws RSRuntimeException {
    Allocation input = null;
    Allocation output = null;
    ScriptIntrinsicBlur blur = null;
    try {
        input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        output = Allocation.createTyped(renderScript, input.getType());
        blur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        blur.setInput(input);
        blur.setRadius(radius);
        blur.forEach(output);
        output.copyTo(bitmap);
    } finally {
        if (input != null) {
            input.destroy();
        }
        if (output != null) {
            output.destroy();
        }
        if (blur != null) {
            blur.destroy();
        }
    }
    return bitmap;
}
Also used : Allocation(android.renderscript.Allocation) ScriptIntrinsicBlur(android.renderscript.ScriptIntrinsicBlur) TargetApi(android.annotation.TargetApi)

Example 12 with Allocation

use of android.renderscript.Allocation in project platform_frameworks_base by android.

the class Camera method createPreviewAllocation.

/**
     * <p>Create a {@link android.renderscript RenderScript}
     * {@link android.renderscript.Allocation Allocation} to use as a
     * destination of preview callback frames. Use
     * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use
     * the created Allocation as a destination for camera preview frames.</p>
     *
     * <p>The Allocation will be created with a YUV type, and its contents must
     * be accessed within Renderscript with the {@code rsGetElementAtYuv_*}
     * accessor methods. Its size will be based on the current
     * {@link Parameters#getPreviewSize preview size} configured for this
     * camera.</p>
     *
     * @param rs the RenderScript context for this Allocation.
     * @param usage additional usage flags to set for the Allocation. The usage
     *   flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always
     *   be set on the created Allocation, but additional flags may be provided
     *   here.
     * @return a new YUV-type Allocation with dimensions equal to the current
     *   preview size.
     * @throws RSIllegalArgumentException if the usage flags are not compatible
     *   with an YUV Allocation.
     * @see #setPreviewCallbackAllocation
     * @hide
     */
public final Allocation createPreviewAllocation(RenderScript rs, int usage) throws RSIllegalArgumentException {
    Parameters p = getParameters();
    Size previewSize = p.getPreviewSize();
    Type.Builder yuvBuilder = new Type.Builder(rs, Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
    // Use YV12 for wide compatibility. Changing this requires also
    // adjusting camera service's format selection.
    yuvBuilder.setYuvFormat(ImageFormat.YV12);
    yuvBuilder.setX(previewSize.width);
    yuvBuilder.setY(previewSize.height);
    Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), usage | Allocation.USAGE_IO_INPUT);
    return a;
}
Also used : Type(android.renderscript.Type) SdkConstantType(android.annotation.SdkConstant.SdkConstantType) Allocation(android.renderscript.Allocation)

Example 13 with Allocation

use of android.renderscript.Allocation 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 14 with Allocation

use of android.renderscript.Allocation in project android_frameworks_base by crdroidandroid.

the class BlurUtils method renderScriptBlur.

public Bitmap renderScriptBlur(Bitmap bitmap, int radius) {
    if (mRenderScript == null)
        return null;
    Allocation input = Allocation.createFromBitmap(mRenderScript, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(mRenderScript, input.getType());
    mScriptIntrinsicBlur.setRadius(radius);
    mScriptIntrinsicBlur.setInput(input);
    mScriptIntrinsicBlur.forEach(output);
    output.copyTo(bitmap);
    return bitmap;
}
Also used : Allocation(android.renderscript.Allocation)

Example 15 with Allocation

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

the class Camera method createPreviewAllocation.

/**
     * <p>Create a {@link android.renderscript RenderScript}
     * {@link android.renderscript.Allocation Allocation} to use as a
     * destination of preview callback frames. Use
     * {@link #setPreviewCallbackAllocation setPreviewCallbackAllocation} to use
     * the created Allocation as a destination for camera preview frames.</p>
     *
     * <p>The Allocation will be created with a YUV type, and its contents must
     * be accessed within Renderscript with the {@code rsGetElementAtYuv_*}
     * accessor methods. Its size will be based on the current
     * {@link Parameters#getPreviewSize preview size} configured for this
     * camera.</p>
     *
     * @param rs the RenderScript context for this Allocation.
     * @param usage additional usage flags to set for the Allocation. The usage
     *   flag {@link android.renderscript.Allocation#USAGE_IO_INPUT} will always
     *   be set on the created Allocation, but additional flags may be provided
     *   here.
     * @return a new YUV-type Allocation with dimensions equal to the current
     *   preview size.
     * @throws RSIllegalArgumentException if the usage flags are not compatible
     *   with an YUV Allocation.
     * @see #setPreviewCallbackAllocation
     * @hide
     */
public final Allocation createPreviewAllocation(RenderScript rs, int usage) throws RSIllegalArgumentException {
    Parameters p = getParameters();
    Size previewSize = p.getPreviewSize();
    Type.Builder yuvBuilder = new Type.Builder(rs, Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
    // Use YV12 for wide compatibility. Changing this requires also
    // adjusting camera service's format selection.
    yuvBuilder.setYuvFormat(ImageFormat.YV12);
    yuvBuilder.setX(previewSize.width);
    yuvBuilder.setY(previewSize.height);
    Allocation a = Allocation.createTyped(rs, yuvBuilder.create(), usage | Allocation.USAGE_IO_INPUT);
    return a;
}
Also used : Type(android.renderscript.Type) SdkConstantType(android.annotation.SdkConstant.SdkConstantType) Allocation(android.renderscript.Allocation)

Aggregations

Allocation (android.renderscript.Allocation)71 Bitmap (android.graphics.Bitmap)42 RenderScript (android.renderscript.RenderScript)42 ScriptIntrinsicBlur (android.renderscript.ScriptIntrinsicBlur)41 SuppressLint (android.annotation.SuppressLint)14 RequiresApi (android.support.annotation.RequiresApi)14 TargetApi (android.annotation.TargetApi)10 Type (android.renderscript.Type)6 SdkConstantType (android.annotation.SdkConstant.SdkConstantType)5 Paint (android.graphics.Paint)4 BitmapDrawable (android.graphics.drawable.BitmapDrawable)4 BitmapFactory (android.graphics.BitmapFactory)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Canvas (android.graphics.Canvas)2 Point (android.graphics.Point)2 Element (android.renderscript.Element)2 Script (android.renderscript.Script)2 RequiresApi (androidx.annotation.RequiresApi)2 ArrayList (java.util.ArrayList)2