Android:使用固定长宽比的相机拍摄图像

我试图裁剪后的图像,我的代码如下:

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 

谢谢您的帮助! 🙂

经过一番阅读,我意识到不能这么简单。 我的联系人来源位于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; } } } 

希望它有助于:)

看看这个post。 我testing了我的Android 1.5(HTC魔术),并完美的工作。

Android Works

你有没有试过这个Intent (但保留作物/方面/输出/返回数据额外的,你已经有了)?

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

这基本上是Android联系人应用程序的function,所以它可能不太适合您的使用情况(即立即拍摄照片,而不是从画廊中select照片拍摄新照片)。

无论如何值得一试! 🙂

虽然这可能是一个非常古老的线程,我可以通过以下代码以编程方式裁剪图片:

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

然后我把它剪断:

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

在我看来,imageTaken是一个ImageView组件。 你可以在这里看到我的来源