Android-how to convert an RGB color image to a grayscale image

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.