现在的位置: 首页 > 综合 > 正文

用 Renderscript 实现高效率的图片模糊效果

2018年08月27日 ⁄ 综合 ⁄ 共 2142字 ⁄ 字号 评论关闭

转自:http://blog.chengyunfeng.com/?p=596

Android Api 11 中 引入的 Renderscript 可以用 GPU 来提高图片处理的速度,而在 API 17(4.2.1) 中,Renderscript 把一些常用的图片处理功能添加到系统中了,这些功能被称之为 Intrinsics 。包含 图片 模糊、混合、Matrix Convolution (矩阵卷积) 等常用操作。

下面是一个使用 ScriptIntrinsicBlur 来实现图片模糊的示例:

1234567891011121314151617181920212223242526272829303132333435
public Bitmap blurBitmap(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(), 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
blurScript.setRadius(25.f);
//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;
}
view rawblurBitmap.java hosted
with ❤ by GitHub


效果:

Intrinsic 包含的功能列表:

Table 1. RenderScript intrinsics and the operations they provide.

Name Operation
ScriptIntrinsicConvolve3x3,ScriptIntrinsicConvolve5x5 执行 3×3 或者 5×5 convolution(卷积)
ScriptIntrinsicBlur 执行高斯模糊。支持 grayscale (灰度) 和 RGBA 缓冲,系统也使用该功能来绘制图形的阴影。
ScriptIntrinsicYuvToRGB 把 YUV 缓冲 内容 转换为  RGB。通常用来处理相机数据
ScriptIntrinsicColorMatrix 在缓冲中应用一个 4×4 颜色矩阵(color matrix)
ScriptIntrinsicBlend 有多种方式来可以来混个两个图片
ScriptIntrinsicLUT Applies a per-channel lookup table to a buffer.
ScriptIntrinsic3DLUT Applies a color cube with interpolation to a buffer.

参考内容

https://gist.github.com/Mariuxtheone/903c35b4927c0df18cf8

http://android-developers.blogspot.it/2013/08/renderscript-intrinsics.html
http://android-developers.blogspot.it/2011/02/introducing-renderscript.html
http://android-developers.blogspot.it/2011/03/renderscript.html

advance:

http://trickyandroid.com/advanced-blurring-techniques/

https://github.com/PomepuyN/BlurEffectForAndroidDesign


抱歉!评论已关闭.