【发布时间】:2017-03-15 17:00:52
【问题描述】:
我正在使用相机 API2 开发相机应用程序。 我有一个相机预览。 如您所见,我可以选择我感兴趣的领域。 我多么想从矩形内的区域拍照。 有谁知道如何实现这一点?如果您已经浏览过任何教程/示例代码,请发布链接。提前致谢
【问题讨论】:
-
你能拍一张完整的照片吗?如果你是,那么只需裁剪它,并在需要时删除原始图像。
我正在使用相机 API2 开发相机应用程序。 我有一个相机预览。 如您所见,我可以选择我感兴趣的领域。 我多么想从矩形内的区域拍照。 有谁知道如何实现这一点?如果您已经浏览过任何教程/示例代码,请发布链接。提前致谢
【问题讨论】:
当您完成捕获图像后,您将获得捕获图像的路径或存储您在onActivityResult() 中获取的位图以进行捕获。
然后使用下面的代码来执行裁剪该图像:
public void performCrop(){
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "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", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(Intent.createChooser(cropIntent,"Cropping"), PIC_CROP);
}
现在再次在onActivityResult() 中匹配您的PIC_CROP 请求代码并获得如下结果图像:
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
【讨论】:
com.android.camera.action.CROP 不是所有设备的强制意图操作,但更可能是可选的