Cdacians

Cdacians
Cdacians

Tuesday, 7 August 2012

Convert bitmap from color to black and white


Convert bitmap from color to black and white

In this example, the bitmap is converted from color to black and white by adding all three color of red, green and blue and dividing by 3. (The next article demonstrate how to Convert bitmap from color to GrayScale using 0.3R + 0.59G + 0.11B.)

Convert bitmap from color to black and white


?
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
package com.AndroidBitmapProcessing;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
 
public class AndroidBitmapProcessingActivity extends Activity {
  
 ImageView imageView_Source, imageView_Dest;
 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_Dest = (ImageView)findViewById(R.id.dest);
         
        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        imageView_Dest.setImageBitmap(processingBitmap(bitmap_Source));
    }
     
    private Bitmap processingBitmap(Bitmap src){
      
     Bitmap dest = Bitmap.createBitmap(
       src.getWidth(), src.getHeight(), src.getConfig());
      
     for(int x = 0; x < src.getWidth(); x++){
      for(int y = 0; y < src.getHeight(); y++){
       int pixelColor = src.getPixel(x, y);
       int pixelAlpha = Color.alpha(pixelColor);
       int pixelRed = Color.red(pixelColor);
       int pixelGreen = Color.green(pixelColor);
       int pixelBlue = Color.blue(pixelColor);
        
       int pixelBW = (pixelRed + pixelGreen + pixelBlue)/3;
       int newPixel = Color.argb(
         pixelAlpha, pixelBW, pixelBW, pixelBW);
        
       dest.setPixel(x, y, newPixel);
      }
     }
      
     return dest;
    }
}


akm
www.cdacians.com

No comments:

Post a Comment