Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Crop an Image after Taking it With Camera with a Fixed Aspect Ratio

Tags:

I'm trying to crop an image after taking it, and my code is as follows:

   private void doTakePhotoAction() {          Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);         intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());         intent.putExtra("crop", "true");         intent.putExtra("aspectX", 1);         intent.putExtra("aspectY", 1);         intent.putExtra("outputX", 96);         intent.putExtra("outputY", 96);          try {             intent.putExtra("return-data", true);             startActivityForResult(intent, PICK_FROM_CAMERA);         } catch (ActivityNotFoundException e) {             //Do nothing for now         }     } 

With the above code, I'm able to go to crop mode, and crop the picture. However, the 1:1 aspect ratio is not enforced, and neither is the outputX and outputY. I believe this is because the intent was for taking a picture, not for cropping. I've also written another method to getData() from the Intent, and after that use the following:

Intent intent = new Intent("com.android.camera.action.CROP"); intent.setClassName("com.android.camera", "com.android.camera.CropImage"); 

However, when I do that, I get the following runtime error:

E/AndroidRuntime(14648): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.camera/com.android.camera.CropImage}: java.lang.NullPointerException 

Thanks for the help! :)

like image 822
Wysie Avatar asked Dec 29 '09 07:12

Wysie


People also ask

How do I crop a picture without changing the aspect ratio?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

How do you crop while maintaining aspect ratio?

To maintain the aspect ratio of the current crop region, either hold down the SHIFT key while dragging any handle, or specify an aspect ratio in the Aspect ratio box. To maintain the center point of the crop region, hold down the CTRL key while dragging any handle.


2 Answers

After doing some reading, I realized it can't be done so simply. My modded Contacts source is at http://github.com/Wysie, you can take a look if you're interested. Also, here's what I did to get it working:

private void doTakePhotoAction() {     // http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo     // http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image     // http://www.damonkohler.com/2009/02/android-recipes.html     // http://www.firstclown.us/tag/android/     // The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);      //Wysie_Soh: Create path for temp file     mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),                         "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);      try {         intent.putExtra("return-data", true);         startActivityForResult(intent, PICK_FROM_CAMERA);     } catch (ActivityNotFoundException e) {         //Do nothing for now     } }  protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if (resultCode != RESULT_OK) {         return;     }      switch (requestCode) {      case CROP_FROM_CAMERA: {         //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here         //after the image is cropped.          final Bundle extras = data.getExtras();          if (extras != null) {             Bitmap photo = extras.getParcelable("data");              mPhoto = photo;             mPhotoChanged = true;             mPhotoImageView.setImageBitmap(photo);             setPhotoPresent(true);         }          //Wysie_Soh: Delete the temporary file                                 File f = new File(mImageCaptureUri.getPath());                     if (f.exists()) {             f.delete();         }          InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);         mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);          break;     }      case PICK_FROM_CAMERA: {         //Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here         //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)          Intent intent = new Intent("com.android.camera.action.CROP");         intent.setClassName("com.android.camera", "com.android.camera.CropImage");          intent.setData(mImageCaptureUri);         intent.putExtra("outputX", 96);         intent.putExtra("outputY", 96);         intent.putExtra("aspectX", 1);         intent.putExtra("aspectY", 1);         intent.putExtra("scale", true);         intent.putExtra("return-data", true);                     startActivityForResult(intent, CROP_FROM_CAMERA);          break;      }     } } 

Hope it helps :)

like image 166
Wysie Avatar answered Oct 15 '22 03:10

Wysie


Check out this post. I tested it on my android 1.5 (Htc Magic) and worked perfectly.

Android Works

like image 32
dharmin007 Avatar answered Oct 15 '22 04:10

dharmin007