Android Camera + image Cropping

Source: Internet
Author: User

To meet a small requirement, we need to achieve the image cropping function similar to the screenshot picture! The time requirement is very tight, so I read a few demos. Although the principle is not difficult, there are only a few useful examples. Find the least rotten apple in a pile of rotten apples. Then I modified it myself to meet the expected requirements!

 

Departments and bureaus:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <Button 8 android: id = "@ + id/selectImageBtn" 9 android: layout_width = "fill_parent" 10 android: layout_height = "wrap_content" 11 android: text = "select image"/> 12 13 <ImageView14 android: id = "@ + id/imageView" 15 android: layout_width = "wrap_content" 16 android: layout_height = "wrap_content"/> 17 18 </LinearLayout>View Code

  

Activity part:

1 import android. app. activity; 2 import android. app. alertDialog; 3 import android. content. dialogInterface; 4 import android. content. intent; 5 import android. graphics. bitmap; 6 import android. graphics. bitmapFactory; 7 import android.net. uri; 8 import android. OS. bundle; 9 import android. OS. environment; 10 import android. provider. mediaStore; 11 import android. view. view; 12 import android. widget. button; 13 import android. widget. imageView; 14 15 public class DoPhotoActivity extends Activity implements 16 View. onClickListener {17 18/** Called when the activity is first created. */19 20 private Button selectImageBtn; 21 22 private ImageView imageView; 23 24 private File sdcardTempFile; 25 26 private AlertDialog dialog; 27 28 private int crop = 128; 29 30 private static final int PHOTO_PICKED_WI TH_DATA = 3021; 31 32 private static final int PHOTO_CAMERA_PICKED_WITH_DATA = 3022; 33 34 private static final int CAMERA_WITH_DATA = 3023; 35 36 @ Override 37 public void onCreate (Bundle savedInstanceState) {38 39 super. onCreate (savedInstanceState); 40 41 setContentView (R. layout. do_photo); 42 43 selectImageBtn = (Button) findViewById (R. id. selectImageBtn); 44 45 imageView = (ImageView) findViewBy Id (R. id. imageView); 46 47 selectImageBtn. setOnClickListener (this); 48 49 sdcardTempFile = new File (Environment. getExternalStorageDirectory () 50 + "/MYIMAGE/" + String. valueOf (System. currentTimeMillis () 51 + ". jpg "); 52} 53 54 public void ImageScale () {55 56 Intent intent = new Intent (" android. intent. action. PICK "); 57 intent. setDataAndType (MediaStore. images. media. INTERNAL_CONTENT_URI, 58 "image/ * "); 59 intent. putExtra ("output", Uri. fromFile (sdcardTempFile); 60 intent. putExtra ("crop", "true"); 61 intent. putExtra ("aspectX", 1); // The ratio of the cropping frame is 62 intent. putExtra ("aspectY", 1); 63 intent. putExtra ("outputX", crop); // the size of the output image is 64 intent. putExtra ("outputY", crop); 65 startActivityForResult (intent, PHOTO_PICKED_WITH_DATA); 66} 67 68 public void photo () {69 70 Intent intent = new Intent (MediaStore. ACTI Principal); 71 startActivityForResult (intent, CAMERA_WITH_DATA); 72} 73 74 protected void doCropPhoto (Bitmap data) {75 76 Intent intent = getCropImageIntent (data); 77 startActivityForResult (intent, PHOTO_CAMERA_PICKED_WITH_DATA); 78} 79 80 public Intent getCropImageIntent (Bitmap data) {81 Intent intent = new Intent ("com. android. camera. action. CROP "); 82 intent. setType ("image/*"); 83 inte Nt. putExtra ("data", data); 84 intent. putExtra ("crop", "true"); 85 intent. putExtra ("aspectX", 1); 86 intent. putExtra ("aspectY", 1); 87 intent. putExtra ("outputX", crop); 88 intent. putExtra ("outputY", crop); 89 intent. putExtra ("return-data", true); 90 return intent; 91} 92 93 @ Override 94 public void onClick (View v) {95 96 if (v = selectImageBtn) {97 98 if (dialog = null) {99 100 dialog = new LertDialog. builder (this ). setItems (101 new String [] {"camera", "album"}, 102 new DialogInterface. onClickListener () {103 104 @ Override105 public void onClick (DialogInterface dialog, 106 int which) {107 108 109 if (which = 0) {110 photo (); 111 112} else {113 ImageScale (); 114} 115 116} 117 118 }). create (); 119 120} 121 122 if (! Dialog. isShowing () {123 124 dialog. show (); 125 126} 127 128} 129 130} 131 132 @ overridemo-protected void onActivityResult (int requestCode, int resultCode, Intent data) {134 135 if (resultCode = RESULT_ OK) {136 137 switch (requestCode) {138 139 case CAMERA_WITH_DATA: 140 141 Bitmap camera_photo = data. getParcelableExtra ("data"); 142 doCropPhoto (camera_photo); 143 144 145 case PHOTO_PICKED_WITH_DATA: 146 147 Bitmap picked_photo = BitmapFactory. decodeFile (sdcardTempFile. getAbsolutePath (); 148 imageView. setImageBitmap (picked_photo); 149 break; 150 151 case PHOTO_CAMERA_PICKED_WITH_DATA: 152 153 Bitmap camera_picked_photo = data. getParcelableExtra ("data"); 154 imageView. setImageBitmap (camera_picked_photo); 155 break; 156 157} 158 159} 160} 161}View Code

 

 

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.