Search in sources :

Example 6 with ScriptIntrinsicBlur

use of android.support.v8.renderscript.ScriptIntrinsicBlur in project BlurKit-Android by flurgle.

the class BlurKit method blur.

public Bitmap blur(Bitmap src, int radius) {
    final Allocation input = Allocation.createFromBitmap(rs, src);
    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(src);
    return src;
}
Also used : Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Example 7 with ScriptIntrinsicBlur

use of android.support.v8.renderscript.ScriptIntrinsicBlur in project T-MVP by north2016.

the class BlurTransformation method transform.

/**
     * Transforms the given {@link Bitmap} based on the given dimensions and returns the transformed
     * result.
     * <p/>
     * <p>
     * The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically
     * recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations
     * should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning
     * the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing
     * errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may
     * safely be returned to the BitmapPool and/or recycled.
     * </p>
     * <p/>
     * <p>
     * outWidth and outHeight will never be {@link Target#SIZE_ORIGINAL}, this
     * class converts them to be the size of the Bitmap we're going to transform before calling this method.
     * </p>
     *
     * @param pool        A {@link BitmapPool} that can be used to obtain and
     *                    return intermediate {@link Bitmap}s used in this transformation. For every
     *                    {@link Bitmap} obtained from the pool during this transformation, a
     *                    {@link Bitmap} must also be returned.
     * @param toTransform The {@link Bitmap} to transform.
     * @param outWidth    The ideal width of the transformed bitmap (the transformed width does not need to match exactly).
     * @param outHeight   The ideal height of the transformed bitmap (the transformed heightdoes not need to match
     */
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    boolean needScaled = mSampling == DEFAULT_SAMPLING;
    int originWidth = toTransform.getWidth();
    int originHeight = toTransform.getHeight();
    int width, height;
    if (needScaled) {
        width = originWidth;
        height = originHeight;
    } else {
        width = (int) (originWidth / mSampling);
        height = (int) (originHeight / mSampling);
    }
    //find a re-use bitmap
    Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    if (mSampling != DEFAULT_SAMPLING) {
        canvas.scale(1 / mSampling, 1 / mSampling);
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    canvas.drawBitmap(toTransform, 0, 0, paint);
    // TIPS: Glide will take care of returning our original Bitmap to the BitmapPool for us,
    // we needn't to recycle it.
    //        toTransform.recycle();  <--- Just for tips. by Ligboy
    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    blur.setInput(input);
    blur.setRadius(mRadius);
    blur.forEach(output);
    output.copyTo(bitmap);
    rs.destroy();
    if (needScaled) {
        return bitmap;
    } else {
        Bitmap scaled = Bitmap.createScaledBitmap(bitmap, originWidth, originHeight, true);
        bitmap.recycle();
        return scaled;
    }
}
Also used : Bitmap(android.graphics.Bitmap) RenderScript(android.support.v8.renderscript.RenderScript) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur) Canvas(android.graphics.Canvas) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 8 with ScriptIntrinsicBlur

use of android.support.v8.renderscript.ScriptIntrinsicBlur in project ngAndroid by davityle.

the class DefaultBlur method blurBitmap.

private static Bitmap blurBitmap(Bitmap src, float blurRadius, Context context) {
    RenderScript rs = RenderScript.create(context);
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap blurredBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), conf);
    final Allocation input = Allocation.createFromBitmap(rs, src, 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(blurRadius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(blurredBitmap);
    return blurredBitmap;
}
Also used : RenderScript(android.support.v8.renderscript.RenderScript) Bitmap(android.graphics.Bitmap) Allocation(android.support.v8.renderscript.Allocation) ScriptIntrinsicBlur(android.support.v8.renderscript.ScriptIntrinsicBlur)

Aggregations

Allocation (android.support.v8.renderscript.Allocation)8 ScriptIntrinsicBlur (android.support.v8.renderscript.ScriptIntrinsicBlur)8 Bitmap (android.graphics.Bitmap)6 RenderScript (android.support.v8.renderscript.RenderScript)5 Canvas (android.graphics.Canvas)1 Paint (android.graphics.Paint)1 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1 RSRuntimeException (android.support.v8.renderscript.RSRuntimeException)1