刚从相机拍摄并通过从图库中挑选显示的Android裁剪图像
Posted
技术标签:
【中文标题】刚从相机拍摄并通过从图库中挑选显示的Android裁剪图像【英文标题】:Android crop image just after taking from camera and display by picking from gallery 【发布时间】:2018-01-11 07:02:19 【问题描述】:在我的应用程序中,有一个活动从相机拍摄照片,将其保存并随后显示在 ImageView 上。我使用 FilePath 类来指向将其存储在路径上的位置,并使用 CameraUtils 类来获取 file:URI 并将图像转换为 位图。现在我的任务是添加裁剪功能以摆脱我们不感兴趣的区域。裁剪功能应在拍摄照片后立即在屏幕上激活,然后需要保存裁剪的照片以显示。在此之前,请查看添加裁剪前的工作代码:
我有这个方法来捕捉图像:
/* Capture Image Method */
private void captureImage()
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Start intent with Action_Image_Capture
fileUri = CameraUtils.getOutputMediaFileUri(this);//get fileUri from CameraUtils
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);//Send fileUri with intent
//intent.putExtra("return-data",true); // Should be for crop
startActivityForResult(intent, CAMERA_REQUEST_CODE);//start activity for result with CAMERA_REQUEST_CODE
此方法显示捕获的图像
/* Show Captured over ImageView */
private void showCapturedImage()
if (!getImageUrl.equals("") && getImageUrl != null)
imageView.setImageBitmap(CameraUtils.convertImagePathToBitmap(getImageUrl, false));
else
Toast.makeText(this, R.string.capture_image_failed, Toast.LENGTH_SHORT).show();
裁剪图片的方法:
private void CropImage()
try
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 4);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, 1003);
catch (ActivityNotFoundException ex)
//
onActivityResult() 方法:
/**
* public static final int PERMISSION_REQUEST_CODE = 1001;//request code for Camera and External Storage permission
* private static final int CAMERA_REQUEST_CODE = 1002;//request code for capture image
* private static final int CROP_REQUEST_CODE = 1003;//request code for crop
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
case CAMERA_REQUEST_CODE:
try
//When image is captured successfully
if (resultCode == RESULT_OK)
//Check if device SDK is greater than 22 then we get the actual image path via below method
if (Build.VERSION.SDK_INT > 22)
getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
else
//else we will get path directly
getImageUrl = fileUri.getPath();
//After image capture show captured image over image view
showCapturedImage();
else
Toast.makeText(this, R.string.cancel_message, Toast.LENGTH_SHORT).show();
catch (Exception e)
e.printStackTrace();
break;
到目前为止,一切都很好,但是在插入裁剪功能时,我正在努力将带有所需 requestCodes 的 if else/语句添加到 switch/case 语句中,因为它会引发未知错误。你能帮我用多个requstCodes创建正确的逻辑并让它工作吗?
【问题讨论】:
请发布您得到的确切错误(logcat) 你好。我的意思是它会在屏幕上发送一条吐司消息,说明“未知错误”。该应用程序不会崩溃。我假设它是未处理的空异常语句的 bcs 【参考方案1】:进行以下更改后,我可以使其正常工作。
首先,CropIntent 是通过密钥对值传递值,而密钥对值应该通过 MediaStore.EXTRA_OUTPUT 传递,并且 uri 没有使用正确的名称定义。
private void CropImage()
try
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(fileUri, "image/*");
fileUri = CameraUtils.getOutputMediaFileUri(this);
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 4);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(CropIntent, CROP_REQUEST_CODE);
catch (ActivityNotFoundException ex)
//
其次,我在onActivityResult()中用if/else替换了switch/case语句
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
if (resultCode == RESULT_OK)
CropImage();
else if (requestCode == CROP_REQUEST_CODE)
//Check if device SDK is greater than 22 then we get the actual image path via below method
if (Build.VERSION.SDK_INT > 22)
getImageUrl = FilePath.getPath(MainActivity.this, fileUri);
else
//else we will get path directly
getImageUrl = fileUri.getPath();
//After image capture show captured image over image view
showCapturedImage();
【讨论】:
以上是关于刚从相机拍摄并通过从图库中挑选显示的Android裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章