Java Code Examples for android.renderscript.ScriptIntrinsicBlur#setInput()

The following examples show how to use android.renderscript.ScriptIntrinsicBlur#setInput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitmapUtil.java    From a with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 高斯模糊
 */
public static Bitmap stackBlur(Bitmap srcBitmap) {
    if (srcBitmap == null) return null;
    RenderScript rs = RenderScript.create(MApplication.getInstance());
    Bitmap blurredBitmap = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);

    //分配用于渲染脚本的内存
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    //加载我们想要使用的特定脚本的实例。
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    //设置模糊半径
    script.setRadius(8);

    //启动 ScriptIntrinsicBlur
    script.forEach(output);

    //将输出复制到模糊的位图
    output.copyTo(blurredBitmap);

    return blurredBitmap;
}
 
Example 2
Source File: VMBlur.java    From VMLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * 使用 RenderScript 模糊图片
 * PS:此方法只能在 SDK API 17 以上使用
 *
 * @param context 上下文对象
 * @param bitmap 需要模糊的bitmap
 * @param scale 模糊的比例因数
 * @param radius 模糊半径
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlurBitmp(Context context, Bitmap bitmap, int scale, float radius) {
    // 创建一个新的压缩过的 Bitmap
    Bitmap overlay = VMBitmap.compressBitmapByScale(bitmap, scale);

    RenderScript rs = null;
    try {
        rs = RenderScript.create(context);
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, overlay, 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(radius);
        blur.forEach(output);
        output.copyTo(overlay);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return overlay;
}
 
Example 3
Source File: CoolBGView.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * 获取模糊的图片
 *
 * @param context 上下文对象
 * @param bitmap  传入的bitmap图片
 */
private Bitmap blurBitmap(Context context, Bitmap bitmap) {
    //用需要创建高斯模糊bitmap创建一个空的bitmap
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    // 初始化Renderscript,该类提供了RenderScript context,创建其他RS类之前必须先创建这个类,其控制RenderScript的初始化,资源管理及释放
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    //设定模糊度(注:radius最大只能设置25f)
    blurScript.setRadius(BLUR_RADIUS);
    // 设置blurScript对象的输入内存
    blurScript.setInput(allIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(allOut);
    // 将数据填充到Allocation中
    allOut.copyTo(outBitmap);
    rs.destroy();
    return outBitmap;
}
 
Example 4
Source File: BlurBuilder.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
private static Bitmap blur_real(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);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 5
Source File: TwitterCoverListView.java    From TwitterCover-Android with MIT License 5 votes vote down vote up
public Bitmap renderScriptBlur(Bitmap bitmap, int radius) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    RenderScript rs = RenderScript.create(mContext);

    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    blurScript.setRadius(radius);
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(outBitmap);
    rs.destroy();
    return outBitmap;
}
 
Example 6
Source File: BlurTransformation.java    From XKnife-Android with Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(
            rs,
            blurredBitmap,
            Allocation.MipmapControl.MIPMAP_FULL,
            Allocation.USAGE_SHARED
    );
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example 7
Source File: BitmapUtils.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
/**
 * android系统的模糊方法
 *
 * @param bitmap 要模糊的图片
 * @param radius 模糊等级 >=0 && <=25
 */
private static Bitmap blurBitmap(Context context, Bitmap bitmap, int radius) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //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
                .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(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;
    } else {
        return bitmap;
    }
}
 
Example 8
Source File: BlurBitmapUtil.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
/**
 * 模糊图片的具体方法
 *
 * @param context 上下文对象
 * @param image   需要模糊的图片
 * @return 模糊处理后的图片
 */
@SuppressLint("NewApi")
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
    // 计算图片缩小后的长宽
    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内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(blurRadius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);

    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 9
Source File: BitmapUtils.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap rsBlur(Context context, Bitmap sentBitmap, int radius) {
    Bitmap overlay = Bitmap.createBitmap(sentBitmap.getWidth(), sentBitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);
    Allocation overlayAlloc = Allocation.createFromBitmap(rs, sentBitmap);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);
    rs.destroy();
    return overlay;
}
 
Example 10
Source File: BlurBitmapUtils.java    From MTransition with Apache License 2.0 5 votes vote down vote up
/**
 * 得到模糊后的bitmap
 * thanks http://wl9739.github.io/2016/07/14/教你一分钟实现模糊效果/
 *
 * @param context
 * @param bitmap
 * @param radius
 * @return
 */
public static Bitmap getBlurBitmap(Context context, Bitmap bitmap, int radius) {
    // 将缩小后的图片做为预渲染的图片。
    Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, SCALED_WIDTH, SCALED_HEIGHT, false);
    // 创建一张渲染后的输出图片。
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

    // 创建RenderScript内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(radius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);

    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 11
Source File: ImageUtil.java    From timecat with Apache License 2.0 5 votes vote down vote up
/**
 * 图片高斯模糊具体实现方法
 *
 * @param context
 * @param image
 * @param radius
 *
 * @return
 */
public static Bitmap blur(Context context, Bitmap image, float radius) {
    // 计算图片缩小后的长宽
    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内核对象
    RenderScript rs = RenderScript.create(context);
    // 创建一个模糊效果的RenderScript的工具对象
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
    // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
    Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
    Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);

    // 设置渲染的模糊程度, 25f是最大模糊度
    blurScript.setRadius(radius);
    // 设置blurScript对象的输入内存
    blurScript.setInput(tmpIn);
    // 将输出数据保存到输出内存中
    blurScript.forEach(tmpOut);

    // 将数据填充到Allocation中
    tmpOut.copyTo(outputBitmap);

    return outputBitmap;
}
 
Example 12
Source File: RenderScriptBlurFilter.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Not-in-place intrinsic Gaussian blur filter using {@link ScriptIntrinsicBlur} and {@link
 * RenderScript}. This require an Android versions >= 4.2.
 *
 * @param dest The {@link Bitmap} where the blurred image is written to.
 * @param src The {@link Bitmap} containing the original image.
 * @param context The {@link Context} necessary to use {@link RenderScript}
 * @param radius The radius of the blur with a supported range 0 < radius <= {@link
 *     #BLUR_MAX_RADIUS}
 */
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void blurBitmap(
    final Bitmap dest, final Bitmap src, final Context context, final int radius) {
  Preconditions.checkNotNull(dest);
  Preconditions.checkNotNull(src);
  Preconditions.checkNotNull(context);
  Preconditions.checkArgument(radius > 0 && radius <= BLUR_MAX_RADIUS);
  RenderScript rs = null;
  try {
    rs = RenderScript.create(context);

    // Create an Intrinsic Blur Script using the Renderscript
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    // Create the input/output allocations with Renderscript and the src/dest bitmaps
    Allocation allIn = Allocation.createFromBitmap(rs, src);
    Allocation allOut = Allocation.createFromBitmap(rs, dest);

    // Set the radius of the blur
    blurScript.setRadius(radius);
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(dest);

    blurScript.destroy();
    allIn.destroy();
    allOut.destroy();
  } finally {
    if (rs != null) {
      rs.destroy();
    }
  }
}
 
Example 13
Source File: BlurKit.java    From blurkit-android with MIT License 5 votes vote down vote up
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;
}
 
Example 14
Source File: ImageUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
@RequiresApi(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(UtilsApp.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;
}
 
Example 15
Source File: BlurBuilder.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap blur(Context ctx, @NonNull 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(ctx);
    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);
    theIntrinsic.forEach(tmpOut);
    tmpOut.copyTo(outputBitmap);
    return outputBitmap;
}
 
Example 16
Source File: BlurTransformation.java    From Android-Tech with Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL,
            Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to user.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // start the ScriptInstrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();


    return blurredBitmap;
}
 
Example 17
Source File: BlurTransformation.java    From Music-Player with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

    Bitmap out = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (out == null) {
        out = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}
 
Example 18
Source File: BlurTransformation.java    From VinylMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

    Bitmap out = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (out == null) {
        out = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}
 
Example 19
Source File: CaptionedImageView.java    From auid2 with Apache License 2.0 4 votes vote down vote up
private void updateBlur() {
    if (!(mDrawable instanceof BitmapDrawable)) {
        return;
    }
    final int textViewHeight = mTextView.getHeight();
    final int imageViewHeight = mImageView.getHeight();
    if (textViewHeight == 0 || imageViewHeight == 0) {
        return;
    }

    // Get the Bitmap
    final BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable;
    final Bitmap originalBitmap = bitmapDrawable.getBitmap();

    // Determine the size of the TextView compared to the height of the ImageView
    final float ratio = (float) textViewHeight / imageViewHeight;

    // Calculate the height as a ratio of the Bitmap
    final int height = (int) (ratio * originalBitmap.getHeight());
    final int width = originalBitmap.getWidth();
    final String blurKey = getBlurKey(width);
    Bitmap newBitmap = BitmapUtils.getBitmap(blurKey);
    if (newBitmap != null) {
        mImageView.setImageBitmap(newBitmap);
        return;
    }

    // The y position is the number of pixels height represents from the bottom of the Bitmap
    final int y = originalBitmap.getHeight() - height;

    TraceCompat.beginSection("BLUR - createBitmaps");
    final Bitmap portionToBlur = Bitmap.createBitmap(originalBitmap, 0, y, originalBitmap.getWidth(), height);
    final Bitmap blurredBitmap = Bitmap.createBitmap(portionToBlur.getWidth(), height, Bitmap.Config.ARGB_8888);
    TraceCompat.endSection();

    // Use RenderScript to blur the pixels
    TraceCompat.beginSection("BLUR - RenderScript");
    RenderScript rs = RenderScript.create(getContext());
    ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    TraceCompat.beginSection("BLUR - RenderScript Allocation");
    Allocation tmpIn = Allocation.createFromBitmap(rs, portionToBlur);
    // Fix internal trace that isn't ended
    TraceCompat.endSection();
    Allocation tmpOut = Allocation.createFromBitmap(rs, blurredBitmap);
    // Fix internal trace that isn't ended
    TraceCompat.endSection();
    TraceCompat.endSection();
    theIntrinsic.setRadius(25f);
    theIntrinsic.setInput(tmpIn);
    TraceCompat.beginSection("BLUR - RenderScript forEach");
    theIntrinsic.forEach(tmpOut);
    TraceCompat.endSection();
    TraceCompat.beginSection("BLUR - RenderScript copyTo");
    tmpOut.copyTo(blurredBitmap);
    TraceCompat.endSection();
    new Canvas(blurredBitmap).drawColor(mScrimColor);
    TraceCompat.endSection();

    // Create the new bitmap using the old plus the blurred portion and display it
    TraceCompat.beginSection("BLUR - Finalize image");
    newBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
    final Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(blurredBitmap, 0, y, new Paint());
    BitmapUtils.cacheBitmap(blurKey, newBitmap);
    mTextView.setBackground(null);
    mImageView.setImageBitmap(newBitmap);
    TraceCompat.endSection();
}
 
Example 20
Source File: BlurTransformation.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    int sampling;
    if (this.sampling == 0) {
        sampling = ImageUtil.calculateInSampleSize(toTransform.getWidth(), toTransform.getHeight(), 100);
    } else {
        sampling = this.sampling;
    }

    int width = toTransform.getWidth();
    int height = toTransform.getHeight();
    int scaledWidth = width / sampling;
    int scaledHeight = height / sampling;

    Bitmap out = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (out == null) {
        out = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(out);
    canvas.scale(1 / (float) sampling, 1 / (float) sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(toTransform, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= 17) {
        try {
            final RenderScript rs = RenderScript.create(context.getApplicationContext());
            final Allocation input = Allocation.createFromBitmap(rs, out, 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(out);

            rs.destroy();

            return out;

        } catch (RSRuntimeException e) {
            // on some devices RenderScript.create() throws: android.support.v8.renderscript.RSRuntimeException: Error loading libRSSupport library
            if (BuildConfig.DEBUG) e.printStackTrace();
        }
    }

    return StackBlur.blur(out, blurRadius);
}