Android converts an image into a black/white image

Source: Internet
Author: User

Android converts an image into a black/white image

First, call the android system tool to obtain the image to generate a bitmap file, and then use the binarization Technology in android to convert the image into a black-and-white image. The main interface code is as follows:

Package com. example. blackwhite; import java. io. IOException; import android. media. thumbnailUtils; import android.net. uri; import android. OS. bundle; import android. provider. mediaStore; import android. app. activity; import android. content. contentResolver; import android. content. intent; import android. database. cursor; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. util. log; imp Ort android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. widget. textView; public class MainActivity extends Activity {private final String IMAGE_TYPE = "image/*"; private final int IMAGE_CODE = 0; // here, IMAGE_CODE is the custom private Button addPic = null; private ImageView imgShow = null; private Text View imgPath = null; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); init ();} private void init () {// TODO Auto-generated method stubaddPic = (Button) findViewById (R. id. btn_add); imgPath = (TextView) findViewById (R. id. img_path); imgShow = (ImageView) findViewById (R. id. imgShow); addPic. setOnClickListener (listener);} priva Te OnClickListener listener = new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubButton btn = (Button) v; switch (btn. getId () {case R. id. btn_add: setImage (); break;} private void setImage () {// TODO Auto-generated method stub // use intent to call the album function provided by the system, startActivityForResult is used to obtain the address Intent getAlbum = new Intent (Intent. ACTION_GET_CONTENT); getAlbum. setTyp E (IMAGE_TYPE); startActivityForResult (getAlbum, IMAGE_CODE) ;}}; protected void onActivityResult (int requestCode, int resultCode, Intent data) {// RESULT_ OK is the if (resultCode! = RESULT_ OK) {Log. e ("onActivityResult", "returned resultCode error"); return;} Bitmap bm = null; // external programs can access the data provided by ContentProvider through the ContentResolver interface ContentResolver resolver = getContentResolver (); // determine whether the received Activity selects the image if (requestCode = IMAGE_CODE) {try {// obtain the image URL UriUri originalUri = data. getData (); // generate bitmapbm = MediaStore Based on the image URi. images. media. getBitmap (resolver, originalUri); // convert an image into a black/white image bm = convertToBla CkWhite (bm); // displays the bitmap image imgShow. setImageBitmap (bm); // creates a new string array to store the image address data. String [] proj = {MediaStore. images. media. DATA}; // interface provided by the android system, used to obtain DATA Cursor cursor = managedQuery (originalUri, proj, null) based on the uri ); // obtain the index value int column_index = cursor of the selected image. getColumnIndexOrThrow (MediaStore. images. media. DATA); // move the cursor to the beginning to prevent queue overflow cursor. moveToFirst (); // obtain the image path String path = cursor Based on the index value. getString (column_index); imgPath. setText (path);} catch (IOException e) {Log. e ("getImg", e. toString ());}}} /*** convert a color image to a pure black/white two color ** @ param Bitmap * @ return returns the converted Bitmap */public static Bitmap convertToBlackWhite (Bitmap bmp) {int width = bmp. getWidth (); // get the width of the bitmap int height = bmp. getHeight (); // obtain the bitmap's high int [] pixels = new int [width * height]; // create the pixel array bmp by the bitmap size. getPixels (pixels, 0, width, 0, 0, width, height); int alpha = 0xFF <24; for (int I = 0; I 

Sample Code address: http://download.csdn.net/detail/stop_pig/8211315

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.