Crop a Picture after Using Camera to Take It

All your problems with Audio, Video and Images.

Crop a Picture after Using Camera to Take It

Postby Wysie » Tue Dec 29, 2009 6:14 am

Hi all, in the following code, I am able to get the photo editor to come up after I take a picture, and am able to crop it. However, the aspectX and aspectY does not seem to be in effect, as the aspect ratio is not locked at all.

What I is that after taking a picture, the crop/editor comes up, with a fixed aspect ratio of 1:1. Please help, thank you! :)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void doTakePhotoAction() {
  2.  
  3.        
  4.  
  5.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  6.  
  7.         intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
  8.  
  9.         intent.putExtra("crop", "true");
  10.  
  11.         intent.putExtra("aspectX", 1);
  12.  
  13.         intent.putExtra("aspectY", 1);
  14.  
  15.         intent.putExtra("outputX", 96);
  16.  
  17.         intent.putExtra("outputY", 96);
  18.  
  19.        
  20.  
  21.         try {
  22.  
  23.             intent.putExtra("return-data", true);
  24.  
  25.             startActivityForResult(intent, PICK_FROM_CAMERA);
  26.  
  27.         } catch (ActivityNotFoundException e) {
  28.  
  29.             //Do nothing for now
  30.  
  31.         }
  32.  
  33.     }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Wysie
Freshman
Freshman
 
Posts: 4
Joined: Wed Dec 16, 2009 6:26 am

Top

Postby Wysie » Tue Dec 29, 2009 7:31 pm

Hi all,

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void doTakePhotoAction() {
  2.  
  3.         // http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
  4.  
  5.         // http://stackoverflow.com/questions/1050 ... ured-image
  6.  
  7.         // http://www.damonkohler.com/2009/02/android-recipes.html
  8.  
  9.         // http://www.firstclown.us/tag/android/
  10.  
  11.         // The one I used to get everything working: http://groups.google.com/group/android- ... 12ee99ba30
  12.  
  13.        
  14.  
  15.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  16.  
  17.        
  18.  
  19.         //Wysie_Soh: Create path for temp file
  20.  
  21.         mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
  22.  
  23.                             "tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
  24.  
  25.        
  26.  
  27.         intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
  28.  
  29.  
  30.  
  31.         try {
  32.  
  33.             intent.putExtra("return-data", true);
  34.  
  35.             startActivityForResult(intent, PICK_FROM_CAMERA);
  36.  
  37.         } catch (ActivityNotFoundException e) {
  38.  
  39.             //Do nothing for now
  40.  
  41.         }
  42.  
  43.     }
  44.  
  45.  
  46.  
  47.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  48.  
  49.         if (resultCode != RESULT_OK) {
  50.  
  51.             return;
  52.  
  53.         }
  54.  
  55.  
  56.  
  57.         switch (requestCode) {
  58.  
  59.        
  60.  
  61.         case CROP_FROM_CAMERA: {
  62.  
  63.             //Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
  64.  
  65.             //after the image is cropped.
  66.  
  67.        
  68.  
  69.             final Bundle extras = data.getExtras();
  70.  
  71.  
  72.  
  73.             if (extras != null) {
  74.  
  75.                 Bitmap photo = extras.getParcelable("data");
  76.  
  77.  
  78.  
  79.                 mPhoto = photo;
  80.  
  81.                 mPhotoChanged = true;
  82.  
  83.                 mPhotoImageView.setImageBitmap(photo);
  84.  
  85.                 setPhotoPresent(true);
  86.  
  87.             }
  88.  
  89.            
  90.  
  91.             //Wysie_Soh: Delete the temporary file                        
  92.  
  93.             File f = new File(mImageCaptureUri.getPath());            
  94.  
  95.             if (f.exists()) {
  96.  
  97.                 f.delete();
  98.  
  99.             }
  100.  
  101.            
  102.  
  103.             InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  104.  
  105.             mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);
  106.  
  107.            
  108.  
  109.             break;
  110.  
  111.         }
  112.  
  113.        
  114.  
  115.         case PICK_FROM_CAMERA: {
  116.  
  117.             //Wysie_Soh: After an image is taken and saved to the location of mImageCaptureUri, come here
  118.  
  119.             //and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)
  120.  
  121.                    
  122.  
  123.             Intent intent = new Intent("com.android.camera.action.CROP");
  124.  
  125.             intent.setClassName("com.android.camera", "com.android.camera.CropImage");
  126.  
  127.            
  128.  
  129.             intent.setData(mImageCaptureUri);
  130.  
  131.             intent.putExtra("outputX", 96);
  132.  
  133.             intent.putExtra("outputY", 96);
  134.  
  135.             intent.putExtra("aspectX", 1);
  136.  
  137.             intent.putExtra("aspectY", 1);
  138.  
  139.             intent.putExtra("scale", true);
  140.  
  141.             intent.putExtra("return-data", true);            
  142.  
  143.             startActivityForResult(intent, CROP_FROM_CAMERA);
  144.  
  145.            
  146.  
  147.             break;
  148.  
  149.  
  150.  
  151.         }
  152.  
  153.         }
  154.  
  155.     }
  156.  
  157.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

Hope it helps :)
Wysie
Freshman
Freshman
 
Posts: 4
Joined: Wed Dec 16, 2009 6:26 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 8 guests