Android-converts an RGB color image to a grayscale image,
Package com. example. yanlei. wifi; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends AppCompatActivity {/* (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 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 final bitmap Bitmap = BitmapFactory through the bitmap factory. decodeResource (getResources (), R. drawable. c); imageView1.setImageBitmap (bitmap); // Add the listener event rgb2greyBtn to the "convert to grayscale" button. 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 (); // obtain the high int of the bitmap [] pixels = new int [width * height]; // create an img array of pixels based on the size of the bitmap. getPixels (pixels, 0, width, 0, 0, width, height); int alpha = 0xFF <24; for (int I = 0; I
<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>