閱讀本文以前關於將RGB顏色值轉換爲灰階值的計算方法可參見:html
三種算法轉換彩色灰階 http://aiilive.blog.51cto.com/1925756/1718960java
灰階顯示圖片的典型應用就是用戶頭像,如用戶在線頭像顯示彩色(原圖),不在線顯示灰色(黑白圖)。總結一點就是更加一張原始圖片來經過顏色的過濾處理計算獲得不一樣顯示效果的圖片。這方法的API主要位於:android.android
使用上文中提到的「三種算法轉換彩色灰階」一文中提到的灰階計算方法產生的黑白圖片顯示效果以下圖:算法
說明:經過Use Matrix是使用Android的ColorMatrix和ColorFilter實現,其中設置ColorMatrix的setSaturation(float sat)飽和度設置爲0時顏色過濾以後顯示灰階,android.graphics.ColorMatrix的內部實現和具體RGB顏色權重值近似等於圖中BT709中的權重。canvas
代碼示例(依賴此文中附加的灰階計算方法封裝類)ide
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grayscale); ButterKnife.bind(this); BitmapDrawable bd = (BitmapDrawable) Original_ImageView.getDrawable(); Bitmap bitmap = bd.getBitmap(); Log.d(TAG, " w=" + bitmap.getWidth() + ", h=" + bitmap.getHeight() + ", c=" + bitmap.getConfig().toString()); //0 BT709 Bitmap matrix = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(matrix); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); //傳入一個大於1的數字將增長飽和度,而傳入一個0~1之間的數字會減小飽和度。0值將產生一幅灰度圖像 //Android ColorMatrix 默認的灰階計算採用下面的BT709標準 colorMatrix.setSaturation(0f); ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(colorMatrixColorFilter); canvas.drawBitmap(bitmap, 0f, 0f, paint); Matrix_ImageView.setImageBitmap(matrix); //原始圖片 Bitmap sunflower = XUtils.BitmapUtil.decodeMutableBitmapFromResourceId(this, R.drawable.sunflower); //1 Bitmap lightness = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Lightness); Lightness_ImageView.setImageBitmap(lightness); //2 Bitmap average = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Average); Average_ImageView.setImageBitmap(average); //3 Bitmap luminosity = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Luminosity); Luminosity_ImageView.setImageBitmap(luminosity); //4 Bitmap bt709 = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.BT709); BT709_ImageView.setImageBitmap(bt709); //5 Bitmap rmy = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.RMY); RMY_ImageView.setImageBitmap(rmy); //6 Bitmap y = grayScale(sunflower, XUtils.GrayScaleUtil.GrayScale.Y); Y_ImageView.setImageBitmap(y); } public Bitmap grayScale(final Bitmap bitmap, XUtils.GrayScaleUtil.GrayScale grayScale) { if (null == bitmap || null == grayScale) { return null; } Bitmap rs = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(rs); Paint paint = new Paint(); for (int x = 0, w = bitmap.getWidth(); x < w; x++) { for (int y = 0, h = bitmap.getHeight(); y < h; y++) { int c = bitmap.getPixel(x, y); int a = Color.alpha(c); int r = Color.red(c); int g = Color.red(c); int b = Color.blue(c); int gc = grayScale.grayScale(r, g, b); paint.setColor(Color.argb(a, gc, gc, gc)); canvas.drawPoint(x, y, paint); } } return rs; }
關於ColorMatrix的介紹參見Android document : 學習
本地:${SDK}/docs/reference/android/graphics/ColorMatrix.html this
在線:http://developer.android.com/reference/android/graphics/ColorMatrix.html spa
參考信息:.net
Android學習筆記之圖像顏色處理(ColorMatrix)
Understanding the Use of ColorMatrix and ColorMatrixColorFilter to Modify a Drawable's Hue