android-How to convert an RGB color graph to a grayscale graph method _android

Source: Internet
Author: User
Tags stub

Example: Rgb2grey

Project Operation Effect Chart:

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);
Get references to components 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 from a 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
Displays the converted grayscale image
Imageview2.setimagebitmap (convertgreyimg (bitmap));
}
});

}

/**
* Convert a color graph to a grayscale image
* @param img Bitmap
* @return returns the converted Bitmap
*/
Public Bitmap convertgreyimg (Bitmap img) {
int width = img.getwidth (); Get the width of a bitmap
int height = img.getheight (); Gets the height of the bitmap

int []pixels = new int[width * height]; Create a set of pixel points from the size of a bitmap

Img.getpixels (pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; J < width; j + +) {
int grey = Pixels[width * i + j];

int red = ((Grey & 0x00ff0000) >> 16);
int green = ((Grey & 0X0000FF00) >> 8);
int blue = (grey & 0X000000FF);

Grey = (int) ((float) Red * 0.3 + (float) Green * 0.59 + (float) blue * 0.11);
Grey = Alpha | (Grey << 16) | (Grey << 8) | Grey
Pixels[width * i + j] = grey;
}
}
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);
Get references to components 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 from a 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
Displays the converted grayscale image
Imageview2.setimagebitmap (convertgreyimg (bitmap));
}
});

}

/**
* Convert a color graph to a grayscale image
* @param img Bitmap
* @return returns the converted Bitmap
*/
Public Bitmap convertgreyimg (Bitmap img) {
int width = img.getwidth (); Get the width of a bitmap
int height = img.getheight (); Gets the height of the bitmap

int []pixels = new int[width * height]; Create a set of pixel points from the size of a bitmap

Img.getpixels (pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24;
for (int i = 0; i < height; i++) {
for (int j = 0; J < width; j + +) {
int grey = Pixels[width * i + j];

int red = ((Grey & 0x00ff0000) >> 16);
int green = ((Grey & 0X0000FF00) >> 8);
int blue = (grey & 0X000000FF);

Grey = (int) ((float) Red * 0.3 + (float) Green * 0.59 + (float) blue * 0.11);
Grey = Alpha | (Grey << 16) | (Grey << 8) | Grey
Pixels[width * i + j] = grey;
}
}
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>

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.