Instance: RGB2Grey
Project running:
Source code:
[Java]
Public class MainActivity extends Activity {
/* (Non-Javadoc)
* @ See android. app. Activity # onCreate (android. OS. Bundle)
*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Obtain the reference of the component in the interface by Id
Button rgb2greyBtn = (Button) findViewById (R. id. rgb2greybtn );
ImageView imageView1 = (ImageView) findViewById (R. id. imageView1 );
Final ImageView imageView2 = (ImageView) findViewById (R. id. imageView2 );
// Create a bitmap through the bitmap Factory
Final Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. ic_android );
ImageView1.setImageBitmap (bitmap );
// Add a listener event for the "convert to grayscale" button
Rgb2greyBtn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Display the converted grayscale image
ImageView2.setImageBitmap (convertGreyImg (bitmap ));
}
});
}
/**
* Convert a color image to a grayscale image
* @ Param img bitmap
* @ Return returns the converted bitmap.
*/
Public Bitmap convertGreyImg (Bitmap img ){
Int width = img. getWidth (); // obtain the width of the bitmap.
Int height = img. getHeight (); // retrieves the height of a bitmap.
Int [] pixels = new int [width * height]; // creates a pixel array based on the bitmap size.
Img. getPixels (pixels, 0, width, 0, 0, width, height );
Int alpha = 0xFF <24;
For (int I = 0; I For (int j = 0; j <width; j ++ ){
Int gray = pixels [width * I + j];
Int red = (gray & 0x00FF0000)> 16 );
Int green = (gray & 0x0000FF00)> 8 );
Int blue = (gray & 0x000000FF );
Gray = (int) (float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11 );
Gray = alpha | (gray <16) | (gray <8) | gray;
Pixels [width * I + j] = gray;
}
}
Bitmap result = Bitmap. createBitmap (width, height, Config. RGB_565 );
Result. setPixels (pixels, 0, width, 0, 0, width, height );
Return result;
}
}
Public class MainActivity extends Activity {
/* (Non-Javadoc)
* @ See android. app. Activity # onCreate (android. OS. Bundle)
*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Obtain the reference of the component in the interface by Id
Button rgb2greyBtn = (Button) findViewById (R. id. rgb2greybtn );
ImageView imageView1 = (ImageView) findViewById (R. id. imageView1 );
Final ImageView imageView2 = (ImageView) findViewById (R. id. imageView2 );
// Create a bitmap through the bitmap Factory
Final Bitmap bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. ic_android );
ImageView1.setImageBitmap (bitmap );
// Add a listener event for the "convert to grayscale" button
Rgb2greyBtn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Display the converted grayscale image
ImageView2.setImageBitmap (convertGreyImg (bitmap ));
}
});
}
/**
* Convert a color image to a grayscale image
* @ Param img bitmap
* @ Return returns the converted bitmap.
*/
Public Bitmap convertGreyImg (Bitmap img ){
Int width = img. getWidth (); // obtain the width of the bitmap.
Int height = img. getHeight (); // retrieves the height of a bitmap.
Int [] pixels = new int [width * height]; // creates a pixel array based on the bitmap size.
Img. getPixels (pixels, 0, width, 0, 0, width, height );
Int alpha = 0xFF <24;
For (int I = 0; I For (int j = 0; j <width; j ++ ){
Int gray = pixels [width * I + j];
Int red = (gray & 0x00FF0000)> 16 );
Int green = (gray & 0x0000FF00)> 8 );
Int blue = (gray & 0x000000FF );
Gray = (int) (float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11 );
Gray = alpha | (gray <16) | (gray <8) | gray;
Pixels [width * I + j] = gray;
}
}
Bitmap result = Bitmap. createBitmap (width, height, Config. RGB_565 );
Result. setPixels (pixels, 0, width, 0, 0, width, height );
Return result;
}
}
Layout file:
[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
/>
<Button
Android: id = "@ + id/rgb2greybtn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/rgb2greybtn"
Android: layout_gravity = "center_horizontal"/>
<ImageView
Android: id = "@ + id/imageView2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
/>"
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/LinearLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
/>
<Button
Android: id = "@ + id/rgb2greybtn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/rgb2greybtn"
Android: layout_gravity = "center_horizontal"/>
<ImageView
Android: id = "@ + id/imageView2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
/>"
</LinearLayout>