在進行模糊的時候,可以先對原始圖片進行壓縮,然后選擇一個合適的方式進行模糊。
效果如下:
模糊
1、處理圖片
縮放、旋轉圖片
private Bitmap getBitmap(Bitmap source) {
//scaleFactor:壓縮比
Bitmap tempBitmap= Bitmap.createBitmap((int) (source.getWidth() / scaleFactor), (int) blurHeight, Bitmap.Config.ARGB_8888);
Canvas canvas=new Canvas(tempBitmap);
canvas.scale(1/scaleFactor,-1*blurHeight/source.getHeight());
canvas.translate(0, -source.getHeight());
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
// ColorMatrix colorMatrix=new ColorMatrix();
// colorMatrix.setScale(0.8f,0.8f,0.8f,1);
// paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(source, 0, 0, paint);
return tempBitmap;
}
2、模糊圖片
1、fastblur
stackblur的java實現,這里
- 使用
Blur.fastblur(context,tempBitmap,25);
2、rs-stackblur
stackblur的Renderscript實現,這里
- 添加依賴
compile 'com.commit451:NativeStackBlur:1.0.2'
- 使用
NativeStackBlur.process(tempBitmap, 60);
其中,模糊半徑越大,處理后的圖片越模糊
**3、RenderScript ** ScriptIntrinsicBlur
RenderScript,一個強大的圖片處理框架,可以使用ScriptIntrinsicBlur 實現高斯模糊的效果(API 17)。
需要進行如下配置:
defaultConfig {
......
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
}
代碼如下:
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: 0 < radius <= 25
blurScript.setRadius(25.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;
}