【问题标题】:How to crop the image data before saving the in gallery?如何在保存图库之前裁剪图像数据?
【发布时间】:2017-03-15 17:00:52
【问题描述】:

我正在使用相机 API2 开发相机应用程序。 我有一个相机预览。 如您所见,我可以选择我感兴趣的领域。 我多么想从矩形内的区域拍照。 有谁知道如何实现这一点?如果您已经浏览过任何教程/示例代码,请发布链接。提前致谢

【问题讨论】:

  • 你能拍一张完整的照片吗?如果你是,那么只需裁剪它,并在需要时删除原始图像。

标签: android camera crop


【解决方案1】:

当您完成捕获图像后,您将获得捕获图像的路径或存储您在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 不是所有设备的强制意图操作,但更可能是可选的
  • 是的,设备可能没有任何可以 CROP 的应用程序。所以要解决这个问题,您需要创建自己的裁剪活动来为您完成。
  • 另外,您的回答与问题不太相符。
  • 检查此链接:gist.github.com/pookie13/1ad1623bec8d7ee76b66 可能会有所帮助。
猜你喜欢
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多