RenderScript对图片模糊处理

本文介绍了如何利用Android API 11以上引入的RenderScript进行高效的图片模糊处理。通过创建Renderscript实例,设置模糊半径,以及使用ScriptIntrinsicBlur脚本,将原始图片转换为模糊效果。示例代码展示了从输入位图到输出位图的完整过程,并回收了原始位图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RenderScript是Android在API 11之后加入的,用于高效的图片处理,包括模糊、混合、矩阵卷积计算等,原图:

App中效果图:

代码示例如下

[java]  view plain  copy
 print ?
  1. public Bitmap blurBitmap(Bitmap bitmap){  
  2.           
  3.         //Let's create an empty bitmap with the same size of the bitmap we want to blur  
  4.         Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
  5.           
  6.         //Instantiate a new Renderscript  
  7.         RenderScript rs = RenderScript.create(getApplicationContext());  
  8.           
  9.         //Create an Intrinsic Blur Script using the Renderscript  
  10.         ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));  
  11.           
  12.         //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps  
  13.         Allocation allIn = Allocation.createFromBitmap(rs, bitmap);  
  14.         Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);  
  15.           
  16.         //Set the radius of the blur  
  17.         blurScript.setRadius(25.f);  
  18.           
  19.         //Perform the Renderscript  
  20.         blurScript.setInput(allIn);  
  21.         blurScript.forEach(allOut);  
  22.           
  23.         //Copy the final bitmap created by the out Allocation to the outBitmap  
  24.         allOut.copyTo(outBitmap);  
  25.           
  26.         //recycle the original bitmap  
  27.         bitmap.recycle();  
  28.           
  29.         //After finishing everything, we destroy the Renderscript.  
  30.         rs.destroy();  
  31.           
  32.         return outBitmap;  
  33.           
  34.           

  1.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值