许多应用程序(例如Facebook)都具有此功能,当用户从其设备的图库中选择图像后,该用户将被带到预览图,在其中可以选择图像的裁剪版本。看完SO(例如How to crop image on camera preview "Goggles style" - Android)后,我找到了this link作为示例,但该拉链似乎正在出售(CameraPreview.zip
)。由于该帖子有些陈旧,我想知道是否有人知道那里的另一个示例,或者他们知道如何做,请分享您的知识。谢谢。
最佳答案
尝试以下代码,根据您的要求更改参数:
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(fileUri, "image/*");
//set crop properties
//cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 350);
cropIntent.putExtra("outputY", 350);
//retrieve data on return
//cropIntent.putExtra("return-data", true);
// some code to retriev an valid Uri
cropUri = Uri.fromFile(getOutputMediaFile(MEDIA_TYPE_IMAGE));
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_REQ_CODE);
你可以在OnActivityResult()中获得输出
Bundle extras = data.getExtras();
//get the cropped bitmap
clickedPhoto = extras.getParcelable("data");