2009-12-29 76 views
22

我嘗試服用後裁剪圖像後,裁剪圖像,我的代碼如下:安卓:把它帶攝像頭與一個固定長寬比

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 
     } 
    } 

與上面的代碼,我能夠進入裁剪模式,裁剪照片。但是,1:1寬高比不是強制執行的,outputX和outputY都不是。我相信這是因爲其意圖是爲了拍攝照片,而不是種植。我也寫的另一種方法,從意向的getData(),之後使用以下命令:

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

然而,當我這樣做,我得到以下運行時錯誤:

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

謝謝您的幫助! :)

回答

2

你試過這個Intent(但保留作物/方面/輸出/返回數據額外的,你已經有)?

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("image/*"); 

這是基本Android contacts application做什麼,所以也許會不太符合你的使用情況(即拍照立即,而不是從庫中選擇一個的選擇拍攝新照片)。

無論如何值得一試! :)

+0

對我來說不起作用:android.content.ActivityNotFoundException:沒有找到處理Intent的活動{act = android.intent.action.GET_CONTENT(has extras)} – stoefln 2014-10-14 16:16:38

22

經過一番閱讀,我意識到它不能這麼簡單。我的聯繫人來源爲http://github.com/Wysie,如果您有興趣,可以查看一下。此外,這裏就是我做得到它的工作:

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; 

    } 
    } 
} 

希望它能幫助:)

+0

真棒!謝謝! – 2010-12-20 14:59:41

+16

謝謝Wysie,但它不適用於Android 2.x.這是例外: 04-29 18:35:02.857:錯誤/ AndroidRuntime(3486):引起:android.content.ActivityNotFoundException:無法找到顯式活動類{com.android.camera/com.android.camera .CropImage};你有沒有在你的AndroidManifest.xml中聲明這個活動? – 2011-04-29 12:03:20

+0

如果outputX超過500,所以沒有任何想法? – somish 2013-11-01 11:53:56

3

退房這個職位。我在我的android 1.5(Htc Magic)上測試過它,並且完美運行。

Android Works

-3

雖然這可能是一個很老的線程,我可以用下面的代碼編程裁剪圖片:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 

     photo = (Bitmap) data.getExtras().get("data"); 

     performcrop(); 
    } 

} 

private void performcrop() { 
    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics(); 
    int width = displayMetrics.widthPixels; 
    int height = displayMetrics.heightPixels; 

    Bitmap croppedBmp = Bitmap.createBitmap(photo, 0, 0, width/2, 
      photo.getHeight()); 

    imageTaken.setImageBitmap(croppedBmp); 
} 

 btnTakePicture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

      startActivityForResult(cameraIntent, CAMERA_REQUEST); 
     } 
    }); 

然後我用裁剪它

imageTaken是我看來的ImageView組件。你可以看到我的源Here