Java Code Examples for android.renderscript.RenderScript#setMessageHandler()

The following examples show how to use android.renderscript.RenderScript#setMessageHandler() . 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: ImageUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * renderScript模糊图片
 * <p>API大于17</p>
 *
 * @param src     源图片
 * @param radius  模糊半径(0...25)
 * @return 模糊后的图片
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur(Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(Utils.getContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, src, 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(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
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: 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 4
Source File: BlurTransformation.java    From AcgClub with MIT License 5 votes vote down vote up
private Bitmap doBlur(Context context, Bitmap bitmap, int radius)
    throws RSRuntimeException {
  RenderScript rs = null;
  Allocation input = null;
  Allocation output = null;
  ScriptIntrinsicBlur blur = null;
  try {
    rs = RenderScript.create(context);
    rs.setMessageHandler(new RenderScript.RSMessageHandler());
    input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT);
    output = Allocation.createTyped(rs, input.getType());
    blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(radius);
    blur.forEach(output);
    output.copyTo(bitmap);
  } finally {
    if (rs != null) {
      rs.destroy();
    }
    if (input != null) {
      input.destroy();
    }
    if (output != null) {
      output.destroy();
    }
    if (blur != null) {
      blur.destroy();
    }
  }

  return bitmap;
}
 
Example 5
Source File: RxImageTool.java    From RxTools-master with Apache License 2.0 5 votes vote down vote up
/**
 * renderScript模糊图片
 * <p>API大于17</p>
 *
 * @param src     源图片
 * @param radius  模糊度(0...25)
 * @return 模糊后的图片
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap renderScriptBlur( Bitmap src, float radius) {
    if (isEmptyBitmap(src)) return null;
    RenderScript rs = null;
    try {
        rs = RenderScript.create(RxTool.getContext());
        rs.setMessageHandler(new RenderScript.RSMessageHandler());
        Allocation input = Allocation.createFromBitmap(rs, src, Allocation.MipmapControl.MIPMAP_NONE, Allocation
                .USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        if (radius > 25) {
            radius = 25.0f;
        } else if (radius <= 0) {
            radius = 1.0f;
        }
        blurScript.setInput(input);
        blurScript.setRadius(radius);
        blurScript.forEach(output);
        output.copyTo(src);
    } finally {
        if (rs != null) {
            rs.destroy();
        }
    }
    return src;
}
 
Example 6
Source File: BitmapUtils.java    From FastAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * renderScript 模糊图片
 * <p>API 大于 17</p>
 *
 * @param src     源图片
 * @param radius  模糊半径(0...25)
 * @param recycle 是否回收
 * @return 模糊后的图片
 */
public static Bitmap renderScriptBlur(@NonNull Context context,
                                      @NonNull Bitmap src,
                                      @FloatRange(from = 0, to = 25, fromInclusive = false) float radius,
                                      boolean recycle) {

    RenderScript rs = null;
    Bitmap ret = recycle ? src : src.copy(src.getConfig(), true);
    try {
        rs = RenderScript.create(context);
        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 7
Source File: ImageUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
@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(Utils.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;
}