Cropping photos for Android Development

Source: Internet
Author: User

Cropping photos for Android Development

 

 

1. Select a photo from the album for Cropping

Select a photo from the album and crop it:

 

/*** Select a photo from the album for cropping */private void cropFromGallery () {// TODO Auto-generated method stub Intent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the data intent. setType (image/*); // select intent from all images. putExtra (crop, true); // It is set to crop intent. putExtra (aspectX, 1); // intent for width ratio of cropping. putExtra (aspectY, 1); // specifies a high proportion of intent cropping. putExtra (outputX, 600); // specifies the width of the cropped intent. putExtra (outputY, 600); // intent crop height. putExtra (scale, true); // supports intent scaling. putExtra (return-data, false); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // output the cropping result to the specified Uri intent. putExtra (outputFormat, Bitmap. compressFormat. JPEG. toString (); // The intent format of the cropped image. putExtra (noFaceDetection, true); // no face detection startActivityForResult (intent, SELECT_PIC );}

 

 

Display the cropped photo on ImagaView:

 

Case SELECT_PIC: if (resultCode = RESULT_ OK) {try {Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageUri); // loads the image of the imageUri object to the memory imgShow. setImageBitmap (bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} break;

 

Program running:

 

 

2. Take a photo from the camera for Cropping

Control the camera to take a photo and save the photo to the specified position:

 

/*** Crop a photo taken from the camera */private void cropFromTake () {// TODO Auto-generated method stub Intent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // sets the Action as the photo intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // Save the photo to the specified URI startActivityForResult (intent, TAKE_PIC );}

 

Crop the prepared photo and display it on the ImageView:

 

case TAKE_PIC:    if (resultCode==RESULT_OK) {        cropImageUri(imageUri, 600, 600, CROP_PIC);    }    break;

 

 

/*** Crop the photo corresponding to the specified uri * @ param imageUri: the photo corresponding to the uri * @ param outputX: cropping width * @ param outputY: cropping height * @ param requestCode: request Code */private void cropImageUri (Uri imageUri, int outputX, int outputY, int requestCode) {Intent intent = new Intent (com. android. camera. action. CROP); intent. setDataAndType (imageUri, image/*); intent. putExtra (crop, true); intent. putExtra (aspectX, 1); intent. putExtra (aspectY, 1); intent. putExtra (outputX, outputX); intent. putExtra (outputY, outputY); intent. putExtra (scale, true); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); intent. putExtra (return-data, false); intent. putExtra (outputFormat, Bitmap. compressFormat. JPEG. toString (); intent. putExtra (noFaceDetection, true); // no face detection startActivityForResult (intent, requestCode );}

 

Program running:

 

 

3. Complete project code:
Package com. jph. cp; import java. io. file; import java. io. fileNotFoundException; import android. support. v7.app. actionBarActivity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android.net. uri; import android. OS. bundle; import android. OS. environment; import android. provider. mediaStore; import android. view. view; import android. widget. imageView;/*** select a photo from the album for cropping and take a photo from the camera for cropping * @ author JPH * Date: 2014.10.09 */public class MainActivity extends ActionBarActivity {private final static int SELECT_PIC = 0x123; private final static int TAKE_PIC = 0x124; private final static int CROP_PIC = 0x125; private Uri imageUri; private ImageView imgShow; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize imageUriimageUri = Uri. fromFile (new File (Environment. getExternalStorageDirectory (), test.jpg); imgShow = (ImageView) findViewById (R. id. imgShow) ;}@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (requestCode) {case SELECT_PIC: if (resultCode = RESULT_ OK) {try {Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageUri); // loads the image of the imageUri object to the memory imgShow. setImageBitmap (bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break; case TAKE_PIC: if (resultCode = RESULT_ OK) {cropImageUri (imageUri, 600,600, CROP_PIC);} break; case CROP_PIC: if (resultCode = RESULT_ OK) {try {Bitmap bitmap = BitmapFactory. decodeStream (getContentResolver (). openInputStream (imageUri); // loads the image of the imageUri object to the memory imgShow. setImageBitmap (bitmap);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} break; default: break;} super. onActivityResult (requestCode, resultCode, data);}/*** crop the photo corresponding to the specified uri * @ param imageUri: the photo corresponding to the uri * @ param outputX: crop width * @ param outputY: crop height * @ param requestCode: Request Code */private void cropImageUri (Uri imageUri, int outputX, int outputY, int requestCode) {Intent intent = new Intent (com. android. camera. action. CROP); intent. setDataAndType (imageUri, image/*); intent. putExtra (crop, true); intent. putExtra (aspectX, 1); intent. putExtra (aspectY, 1); intent. putExtra (outputX, outputX); intent. putExtra (outputY, outputY); intent. putExtra (scale, true); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); intent. putExtra (return-data, false); intent. putExtra (outputFormat, Bitmap. compressFormat. JPEG. toString (); intent. putExtra (noFaceDetection, true); // no face detection startActivityForResult (intent, requestCode);} public void cropPic (View view) {switch (view. getId () {case R. id. btnCropFromGallery: // select a photo from the album to crop cropFromGallery (); break; case R. id. btnCropFromTake: // crop cropFromTake (); break; default: break ;}} /*** crop a photo taken from the camera */private void cropFromTake () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (MediaStore. ACTION_IMAGE_CAPTURE); // sets the Action as the photo intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // Save the photo to the specified URIstartActivityForResult (intent, TAKE_PIC);}/*** select a photo from the album for cropping */private void cropFromGallery () {// TODO Auto-generated method stubIntent intent = new Intent (); intent. setAction (Intent. ACTION_PICK); // Pick an item from the dataintent. setType (image/*); // select intent from all images. putExtra (crop, true); // It is set to crop intent. putExtra (aspectX, 1); // intent for width ratio of cropping. putExtra (aspectY, 1); // specifies a high proportion of intent cropping. putExtra (outputX, 600); // specifies the width of the cropped intent. putExtra (outputY, 600); // intent crop height. putExtra (scale, true); // supports intent scaling. putExtra (return-data, false); intent. putExtra (MediaStore. EXTRA_OUTPUT, imageUri); // output the cropping result to the specified Uriintent. putExtra (outputFormat, Bitmap. compressFormat. JPEG. toString (); // The intent format of the cropped image. putExtra (noFaceDetection, true); // no face detectionstartActivityForResult (intent, SELECT_PIC );}}


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.