Today, I am bored with some photography and album cropping functions. I hope this will help you!
I will not talk about it much more. The code is actually a little bit:
First, XML:
Then paste the main activity:
/*** @ Title: PicCutDemoActivity. java * @ Package com. xiaoma. piccut. demo * @ Description: Image cropping function test * @ author XiaoMa */public class PicCutDemoActivity extends Activity implements OnClickListener {private ImageButton ib = null; private ImageView iv = null; private Button btn = null; private String tp = null;/** Called when the activity is first created. * // @ Overridepublic void onCreate (Bundle savedInstance State) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // initialize init ();}/*** Implementation of the initialization method */private void init () {ib = (ImageButton) findViewById (R. id. imageButton1); iv = (ImageView) findViewById (R. id. imageView1); btn = (Button) findViewById (R. id. button1); ib. setOnClickListener (this); iv. setOnClickListener (this); btn. setOnClickListener (this);}/*** Click Event Implementation of the control ** because a friend asked how to crop the background image of different controls, * I used it here. Three controls are provided to record and learn for yourself. * if you think it is useless, you can skip this step. */@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. imageButton1: ShowPickDialog (); break; case R. id. imageView1: ShowPickDialog (); break; case R. id. button1: ShowPickDialog (); break; default: break;}/*** select prompt dialog box */private void ShowPickDialog () {new AlertDialog. builder (this ). setTitle ("set Avatar... "). setNegativeButton ("album", new DialogInterface. onClickListener () {pu Blic void onClick (DialogInterface dialog, int which) {dialog. dismiss ();/*** at the beginning, I did not know what ACTION_PICK was doing. Later I looked at the Intent Source Code directly. * I can find many things in it, intent is a very powerful thing. You must carefully read */Intent intent = new Intent (Intent. ACTION_PICK, null);/*** the following statement achieves the same effect as other write methods. If: * intent. setData (MediaStore. images. media. EXTERNAL_CONTENT_URI); * intent. setType ("image/*"); set the data type * if you want to restrict the image types uploaded to the server, you can directly write the data as follows: "image/jpeg, image/png, and Other types "* */Intent. setDataAndType (MediaStore. images. media. EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult (intent, 1 );}}). setPositiveButton ("photo", new DialogInterface. onClickListener () {public void onClick (DialogInterface dialog, int whichButton) {dialog. dismiss ();/*** the following sentence is still the old one. You can refer to the following official * Document to call the quick photo function, you_sdk_path/docs/guide/topics/media/camera.html **/Intent intent = new Intent (MediaSt Ore. ACTION_IMAGE_CAPTURE); // specifies the path of the image storage after the camera is taken. putExtra (MediaStore. EXTRA_OUTPUT, Uri. fromFile (new File (Environment. getExternalStorageDirectory (), "xiaoma.jpg"); startActivityForResult (intent, 2 );}}). show () ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {switch (requestCode) {// you can directly obtain case 1: startPhotoZoom (data. getData (); break; // if it is the case when the camera is called 2: File temp = new File (Environment. getExternalStorageDirectory () + "/xiaoma.jpg"); startPhotoZoom (Uri. fromFile (temp); break; // obtain the cropped image case 3:/*** if it is not null, you must verify the image. If it is not verified, * if you are not satisfied with the cropping, You need to crop it again and discard it */if (data! = Null) {setPicToView (data);} break; default: break;} super. onActivityResult (requestCode, resultCode, data);}/*** image cropping method implementation * @ param uri */public void startPhotoZoom (Uri uri) {/** how do I know the Intent ACTION below, you can see the following webpage * yourself_sdk_path/docs/reference/android/content/Intent.html */Intent intent = new Intent ("com. android. camera. action. CROP "); intent. setDataAndType (uri, "image/*"); // The crop = true below is the setting You can set the displayed VIEW in the enabled Intent to crop the intent. putExtra ("crop", "true"); // aspectX aspectY is a width-high ratio intent. putExtra ("aspectX", 1); intent. putExtra ("aspectY", 1); // outputX outputY is the width and height intent of the cropped image. putExtra ("outputX", 150); intent. putExtra ("outputY", 150); intent. putExtra ("return-data", true); startActivityForResult (intent, 3 );} /*** Save the cropped image data * @ param picdata */private void setPicToView (Intent picdata) {Bundle extras = pi Cdata. getExtras (); if (extras! = Null) {Bitmap photo = extras. getParcelable ("data"); Drawable drawable = new BitmapDrawable (photo);/*** the following annotation method uploads the cropped image to the server using Base64Coder characters, the method used to upload the QQ avatar is similar to this * // * ByteArrayOutputStream stream = new ByteArrayOutputStream (); photo. compress (Bitmap. compressFormat. JPEG, 60, stream); byte [] B = stream. toByteArray (); // store the image stream as a String. tp = new String (Base64Coder. encodeLines (B); in this case, you can write down the implementation of uploading images to the server, and directly upload the tp directly. The server handles the problem on the server side, if the data downloaded to the server is still in the Base64Coder format, you can convert the data to the image type that we can use in the following way... roar Bitmap dBitmap = BitmapFactory. decodeFile (tp); Drawable drawable = new BitmapDrawable (dBitmap); */ib. setBackgroundDrawable (drawable); iv. setBackgroundDrawable (drawable );}}
The main code has been pasted!
Try it.