Search in sources :

Example 1 with RSRuntimeException

use of android.support.v8.renderscript.RSRuntimeException in project BlurDialogFragment by tvbarthel.

the class RenderScriptBlurHelper method doBlur.

/**
     * blur a given bitmap
     *
     * @param sentBitmap       bitmap to blur
     * @param radius           blur radius
     * @param canReuseInBitmap true if bitmap must be reused without blur
     * @param context          used by RenderScript, can be null if RenderScript disabled
     * @return blurred bitmap
     */
public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap, Context context) {
    Bitmap bitmap;
    if (canReuseInBitmap) {
        bitmap = sentBitmap;
    } else {
        bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
    }
    if (bitmap.getConfig() == Bitmap.Config.RGB_565) {
        // RenderScript hates RGB_565 so we convert it to ARGB_8888
        // (see http://stackoverflow.com/questions/21563299/
        // defect-of-image-with-scriptintrinsicblur-from-support-library)
        bitmap = convertRGB565toARGB888(bitmap);
    }
    try {
        final RenderScript rs = RenderScript.create(context);
        final Allocation input = Allocation.createFromBitmap(rs, bitmap, 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);
        script.setInput(input);
        script.forEach(output);
        output.copyTo(bitmap);
        return bitmap;
    } catch (RSRuntimeException e) {
        Log.e(TAG, "RenderScript known error : https://code.google.com/p/android/issues/detail?id=71347 " + "continue with the FastBlur approach.");
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.support.v8.renderscript.RenderScript) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur) RSRuntimeException(android.support.v8.renderscript.RSRuntimeException)

Aggregations

Bitmap (android.graphics.Bitmap)1 Allocation (android.support.v8.renderscript.Allocation)1 RSRuntimeException (android.support.v8.renderscript.RSRuntimeException)1 RenderScript (android.support.v8.renderscript.RenderScript)1 ScriptIntrinsicBlur (android.support.v8.renderscript.ScriptIntrinsicBlur)1