美文网首页
高斯模糊

高斯模糊

作者: 最忆是深秋 | 来源:发表于2018-05-04 14:49 被阅读9次

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

    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;     
        }  
    blurScript.setRadius(25.f); 
     设置模糊程度,参数越大,越模糊,参数范围为0-25,否则会报错:
    android.renderscript.RSIllegalArgumentException: Radius out of range (0 < r <= 25).
    

    相关文章

      网友评论

          本文标题:高斯模糊

          本文链接:https://www.haomeiwen.com/subject/whmdrftx.html