Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Convert bitmap from color to GrayScale using ColorMatrixColorFilter


Convert bitmap from color to GrayScale using ColorMatrixColorFilter


Last article "Convert bitmap from color to GrayScale" by calculation. It's another approach using ColorMatrixColorFilter.

ColorMatrix is a 5x4 matrix for transforming the color+alpha components of a Bitmap. The method setSaturation() set the matrix to affect the saturation of colors. A value of 0 maps the color to gray-scale.

ColorMatrixColorFilter create a colorfilter that transforms colors through a 4x5 color matrix.

Convert bitmap from color to GrayScale using ColorMatrixColorFilter


In the sample code below, getGrayscale_ColorMatrixColorFilter() method use ColorMatrixColorFilter with ColorMatrix of Saturation 0, to convert color bitmap to gray scale.

We can also create our own matrix for ColorMatrixColorFilter to convert transform bitmap color+alpha, as shown in getGrayscale_custom_matrix() method.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.AndroidBitmapProcessing;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
 
public class AndroidBitmapProcessingActivity extends Activity {
  
 ImageView imageView_Source, imageView_Gray, imageView_Gray2;
 Bitmap bitmap_Source, bitmap_Dest;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView_Source = (ImageView)findViewById(R.id.source);
        imageView_Gray = (ImageView)findViewById(R.id.imageGray);
        imageView_Gray2 = (ImageView)findViewById(R.id.imageGray2);
         
        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.testpicture);
        imageView_Gray.setImageBitmap(getGrayscale_ColorMatrixColorFilter(bitmap_Source));
        imageView_Gray2.setImageBitmap(getGrayscale_custom_matrix(bitmap_Source));
    }
     
    private Bitmap getGrayscale_ColorMatrixColorFilter(Bitmap src){
     int width = src.getWidth();
     int height = src.getHeight();
 
     Bitmap dest = Bitmap.createBitmap(width, height,
       Bitmap.Config.RGB_565);
      
     Canvas canvas = new Canvas(dest);
     Paint paint = new Paint();
     ColorMatrix colorMatrix = new ColorMatrix();
     colorMatrix.setSaturation(0); //value of 0 maps the color to gray-scale
     ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
     paint.setColorFilter(filter);
     canvas.drawBitmap(src, 0, 0, paint);
      
     return dest;
    }
     
    private Bitmap getGrayscale_custom_matrix(Bitmap src){
     int width = src.getWidth();
     int height = src.getHeight();
      
     //Custom color matrix
     float[] matrix = new float[]{
       0.3f, 0.59f, 0.11f, 0, 0,
                0.3f, 0.59f, 0.11f, 0, 0,
                0.3f, 0.59f, 0.11f, 0, 0,
                0, 0, 0, 1, 0,};
 
     Bitmap dest = Bitmap.createBitmap(width, height,
       Bitmap.Config.RGB_565);
      
     Canvas canvas = new Canvas(dest);
     Paint paint = new Paint();
     ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
     paint.setColorFilter(filter);
     canvas.drawBitmap(src, 0, 0, paint);
           
     return dest;
    }
 
}

akm
www.cdacians.com

No comments:

Post a Comment