public static Bitmap blurMax(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内核对象 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(25f); // 设置blurScript对象的输入内存 blurScript.setInput(tmpIn); // 将输出数据保存到输出内存中 blurScript.forEach(tmpOut); // 将数据填充到Allocation中 tmpOut.copyTo(outputBitmap); return outputBitmap; }
private static Bitmap blur(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; }
/** * renderScript模糊图片 * <p>API大于17</p> * * @param src 源图片 * @param radius 模糊半径(0...25) * @param recycle 是否回收 * @return 模糊后的图片 */ @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) { if (isEmptyBitmap(src)) return null; 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; }
public static Bitmap blur(Context context, Bitmap sentBitmap, int radius) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); final RenderScript rs = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 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); //0.0f ~ 25.0f script.setInput(input); script.forEach(output); output.copyTo(bitmap); return bitmap; } else { return sentBitmap; } }
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blur(UQI uqi, 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(uqi.getContext()); 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; }
/** * 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; }
public static Bitmap 模糊(Bitmap $图片,int $半径) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Bitmap bitmap = $图片.copy($图片.getConfig(), true); final RenderScript rs = RenderScript.create(上下文工具.取全局上下文()); final Allocation input = Allocation.createFromBitmap(rs, $图片, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); final Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius($半径); script.setInput(input); script.forEach(output); output.copyTo(bitmap); // clean up renderscript resources rs.destroy(); input.destroy(); output.destroy(); script.destroy(); return bitmap; } return null; }
public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) { RenderScript rs = RenderScript.create(context); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte); Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options); final Allocation input = Allocation.createFromBitmap(rs, blurTemplate); final Allocation output = Allocation.createTyped(rs, input.getType()); final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(8f); script.setInput(input); script.forEach(output); output.copyTo(blurTemplate); return new BitmapDrawable(context.getResources(), blurTemplate); }
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Bitmap blurredBitmap = toTransform.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(15); script.forEach(output); output.copyTo(blurredBitmap); toTransform.recycle(); return blurredBitmap; }
/** * renderScript模糊图片 * <p>API大于17</p> * * @param src 源图片 * @param radius 模糊半径(0...25) * @param recycle 是否回收 * @return 模糊后的图片 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap renderScriptBlur(final Bitmap src, final float radius, final boolean recycle) { if (isEmptyBitmap(src)) { return null; } 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; }
public Bitmap blur(Bitmap image) { if (null == image) return null; Bitmap outputBitmap = Bitmap.createBitmap(image); final RenderScript renderScript = RenderScript.create(this); Allocation tmpIn = Allocation.createFromBitmap(renderScript, image); Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap); //Intrinsic Gausian blur filter ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); theIntrinsic.setRadius(BLUR_RADIUS); theIntrinsic.setInput(tmpIn); theIntrinsic.forEach(tmpOut); tmpOut.copyTo(outputBitmap); return outputBitmap; }
/** * 图片高斯模糊 * * @param context * @param bitmap * @return */ public static 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); // 创建高斯模糊对象 ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); // 创建Allocations,此类是将数据传递给RenderScript内核的主要方 法,并制定一个后备类型存储给定类型 Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //设定模糊度(注:Radius最大只能设置25.f) blurScript.setRadius(15f); // Perform the Renderscript blurScript.setInput(allIn); blurScript.forEach(allOut); allOut.copyTo(outBitmap); rs.destroy(); return outBitmap; }
/** * renderScript模糊图片 * <p>API大于17</p> * * @param context 上下文 * @param src 源图片 * @param radius 模糊半径(0...25) * @return 模糊后的图片 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap renderScriptBlur(Context context, Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) float radius) { if (isEmptyBitmap(src)) { return null; } RenderScript rs = null; try { rs = RenderScript.create(context); 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; }
private void generateShadow() { if (isJellyBeanAndAbove()) { if (mShadowBlurRadius == 0f) return; if (mShadow == null) { mShadow = Bitmap.createBitmap(getWidth(), getHeight(), ALPHA_8); } else { mShadow.eraseColor(TRANSPARENT); } Canvas c = new Canvas(mShadow); c.drawPath(mPath, mShadowPaint); if (mShowBorder) { c.drawPath(mPath, mShadowPaint); } RenderScript rs = RenderScript.create(getContext()); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8(rs)); Allocation input = Allocation.createFromBitmap(rs, mShadow); Allocation output = Allocation.createTyped(rs, input.getType()); blur.setRadius(mShadowBlurRadius); blur.setInput(input); blur.forEach(output); output.copyTo(mShadow); input.destroy(); output.destroy(); } }
public static Bitmap rsBlur(Context context, Bitmap source, int radius) { // 计算图片缩小后的长宽 int width = Math.round(source.getWidth() * BITMAP_SCALE); int height = Math.round(source.getHeight() * BITMAP_SCALE); // 将缩小后的图片做为预渲染的图片 Bitmap inputBitmap = Bitmap.createScaledBitmap(source, width, height, false); RenderScript renderScript = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap(renderScript, inputBitmap); final Allocation outPut = Allocation.createTyped(renderScript, input.getType()); ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); scriptIntrinsicBlur.setInput(input); scriptIntrinsicBlur.setRadius(radius); scriptIntrinsicBlur.forEach(outPut); outPut.copyTo(inputBitmap); renderScript.destroy(); return inputBitmap; }
public static Bitmap blurBitmap(Context context, Bitmap bmp, float radius) { Bitmap out = Bitmap.createBitmap(bmp); RenderScript rs = RenderScript.create(context); radius = Math.min(Math.max(radius, 0), 25); Allocation input = Allocation.createFromBitmap( rs, bmp, MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); Allocation output = Allocation.createTyped(rs, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setInput(input); script.setRadius(radius); script.forEach(output); output.copyTo(out); rs.destroy(); return out; }
/** * renderScript模糊图片 * <p>API大于17</p> * * @param src 源图片 * @param radius 模糊半径(0...25) * @return 模糊后的图片 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap renderScriptBlur(final Bitmap src, @FloatRange(from = 0, to = 25, fromInclusive = false) final 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; }
public static Bitmap blur(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 = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { 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); }else{ outputBitmap= BlurHelper.doBlur(image,(int)BLUR_RADIUS,false); } return outputBitmap; }
public static Bitmap blur(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; }
public Bitmap blur(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; }
public static Bitmap blurBitmap(Context context, Bitmap bitmap) { if (context == null || bitmap == null) return null; Bitmap blurredBitmap; try { blurredBitmap = Bitmap.createBitmap(bitmap); } catch (OutOfMemoryError e) { return null; } RenderScript renderScript = RenderScript.create(context); Allocation input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT); Allocation output = Allocation.createTyped(renderScript, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); script.setInput(input); script.setRadius(20); script.forEach(output); output.copyTo(blurredBitmap); return blurredBitmap; }
@Nullable public static Bitmap blurBitmap(Context context, Bitmap bitmap) { if (context == null || bitmap == null) return null; Bitmap blurredBitmap; try { blurredBitmap = Bitmap.createBitmap(bitmap); } catch (OutOfMemoryError e) { return null; } RenderScript renderScript = RenderScript.create(context); Allocation input = Allocation.createFromBitmap(renderScript, bitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SCRIPT); Allocation output = Allocation.createTyped(renderScript, input.getType()); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); script.setInput(input); script.setRadius(20); script.forEach(output); output.copyTo(blurredBitmap); return blurredBitmap; }
public Bitmap blur(Context context, Bitmap image,float radius) { int width = Math.round(image.getWidth() * 1.0f); int height = Math.round(image.getHeight() * 1.0f); 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(radius); theIntrinsic.setInput(tmpIn); theIntrinsic.forEach(tmpOut); tmpOut.copyTo(outputBitmap); return outputBitmap; }
@Override public Bitmap transform(Bitmap bitmap) { // Create another bitmap that will hold the results of the filter. Bitmap blurredBitmap = Bitmap.createBitmap(bitmap); // Allocate memory for Renderscript to work with Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(25); // Start the ScriptIntrinisicBlur script.forEach(output); // Copy the output to the blurred bitmap output.copyTo(blurredBitmap); return blurredBitmap; }
private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) { int width = Math.round(smallBitmap.getWidth() * defaultBitmapScale); int height = Math.round(smallBitmap.getHeight() * defaultBitmapScale); Bitmap inputBitmap = Bitmap.createScaledBitmap(smallBitmap, width, height, false); Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); RenderScript renderScript = RenderScript.create(getContext()); ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript)); Allocation tmpIn = Allocation.createFromBitmap(renderScript, inputBitmap); Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap); theIntrinsic.setRadius(radius); theIntrinsic.setInput(tmpIn); theIntrinsic.forEach(tmpOut); tmpOut.copyTo(outputBitmap); return outputBitmap; }
@TargetApi(17) public static Bitmap blur(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; }
/** * 将bitmap高斯模糊化 * @param bitmap * @return */ public Bitmap blurBitmap(Bitmap bitmap){ Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); RenderScript rs = RenderScript.create(getActivity().getApplicationContext()); ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); blurScript.setRadius(25.f); blurScript.setInput(allIn); blurScript.forEach(allOut); allOut.copyTo(outBitmap); bitmap.recycle(); rs.destroy(); return outBitmap; }
/** * renderScript模糊图片 * <p>API大于17</p> * * @param src 源图片 * @param radius 模糊半径(0...25) * @param recycle 是否回收 * @return 模糊后的图片 */ @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) { if (isEmptyBitmap(src)) return null; RenderScript rs = null; Bitmap ret = recycle ? src : src.copy(src.getConfig(), true); try { rs = RenderScript.create(SDAndroidLib.getContext()); 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; }
public static Bitmap blur(Context ctx, 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; }
/** * 使用RenderScript实现高斯模糊的算法 * @param bitmap * @return */ public Bitmap blur(Bitmap bitmap){ //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(getApplicationContext()); //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: 0 < radius <= 25 blurScript.setRadius(20.0f); //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; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blur(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; }
/** * 模糊函数 * * @param context * @param sentBitmap * @param radius * @return */ public static Bitmap doBlur(Context context, Bitmap sentBitmap, float radius) { if (sentBitmap == null) return null; if (radius <= 0 || radius > 25) radius = 25f;//范围在1-25之间 if (radius <= 6 && Build.VERSION.SDK_INT > 16) {//经测试,radius大于6后,fastBlur效率更高,并且RenderScript在api17以上使用 //Bitmap bitmap = Bitmap.createScaledBitmap(sentBitmap, sentBitmap.getWidth() / scale, sentBitmap.getHeight() / scale, false);//先缩放图片,增加模糊速度 final RenderScript rs = RenderScript.create(context); final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 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(sentBitmap); rs.destroy(); return sentBitmap; } else {//快速模糊 return fastBlur(sentBitmap, radius); } }
/** * 创建一个虚化效果的Bitmap对象,使用了RenderScript,但是要求最低SDK版本号为17. **/ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap createBlurBmpWithRenderScript(Context context, Bitmap srcBmp, float radius) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) throw new IllegalStateException("要求最低SDK版本号为17"); if (radius < 1) return null; Bitmap bitmap = Bitmap.createBitmap(srcBmp.getWidth(), srcBmp.getHeight(), srcBmp.getConfig()); //srcBmp.copy(srcBmp.getConfig(), true); RenderScript rs = RenderScript.create(context); Allocation alloc = Allocation.createFromBitmap(rs, srcBmp); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, alloc.getElement()); blur.setInput(alloc); blur.setRadius(radius); blur.forEach(alloc); alloc.copyTo(bitmap); rs.destroy(); return bitmap; }
/** * 有些手机不支持....so 不能用 * * @param sentBitmap * @param radius */ private void RenderScriptblur(Bitmap sentBitmap, int radius) { long start = System.currentTimeMillis(); Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); final RenderScript rs = RenderScript.create(FastBlurActivity.this); final Allocation input = Allocation.createFromBitmap(rs, sentBitmap, 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 /* e.g. 3.f */); script.setInput(input); script.forEach(output); output.copyTo(bitmap); mIvBlur.setImageBitmap(bitmap); long end = System.currentTimeMillis(); Log.v(TAG, "fastblur time:" + (end - start)); }
public final Drawable zza(Context paramContext, Bitmap paramBitmap, boolean paramBoolean, float paramFloat) { if ((!paramBoolean) || (paramFloat <= 0.0F) || (paramFloat > 25.0F)) { return new BitmapDrawable(paramContext.getResources(), paramBitmap); } try { Bitmap localBitmap1 = Bitmap.createScaledBitmap(paramBitmap, paramBitmap.getWidth(), paramBitmap.getHeight(), false); Bitmap localBitmap2 = Bitmap.createBitmap(localBitmap1); RenderScript localRenderScript = RenderScript.create(paramContext); ScriptIntrinsicBlur localScriptIntrinsicBlur = ScriptIntrinsicBlur.create(localRenderScript, Element.U8_4(localRenderScript)); Allocation localAllocation1 = Allocation.createFromBitmap(localRenderScript, localBitmap1); Allocation localAllocation2 = Allocation.createFromBitmap(localRenderScript, localBitmap2); localScriptIntrinsicBlur.setRadius(paramFloat); localScriptIntrinsicBlur.setInput(localAllocation1); localScriptIntrinsicBlur.forEach(localAllocation2); localAllocation2.copyTo(localBitmap2); BitmapDrawable localBitmapDrawable = new BitmapDrawable(paramContext.getResources(), localBitmap2); return localBitmapDrawable; } catch (RuntimeException localRuntimeException) {} return new BitmapDrawable(paramContext.getResources(), paramBitmap); }
@Override public Bitmap transform(Bitmap bitmap) { // Create another bitmap that will hold the results of the filter. Bitmap blurredBitmap = Bitmap.createBitmap(bitmap); // Allocate memory for Renderscript to work with Allocation input = Allocation.createFromBitmap(rs, bitmap, 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(20); // Start the ScriptIntrinisicBlur script.forEach(output); // Copy the output to the blurred bitmap output.copyTo(blurredBitmap); bitmap.recycle(); return blurredBitmap; }