Android-converts an RGB color image to a grayscale image,

Source: Internet
Author: User

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>

 

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.