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>