用ColorMatrix將Bitmap轉成灰度图

时间:2024-12-05 20:38:02
在Android中,若想將整張圖片轉成灰階效果其實有更簡便的方式,只要透過ColorMatrix類別的setSaturation函式將飽和度設為0即可。(您也可以試試從0~1之間的值,看看不同飽和度的效果)
詳細方法如下:
//colorBitmap為原始Bitmap,grayBitmap則用來存放處理過後的灰階Bitmap
Canvas canvas = new Canvas(grayBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(colorBitmap, 0, 0, paint);
參考資料:
http://developer.android.com/reference/android/graphics/ColorMatrix.html

via:http://droidparadise.blogspot.com/2011/11/colormatrixbitmap.html