android调用系统拍照程序和从图库选取图片,返回后调用系统裁剪工具

调用系统拍照

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


File myImageDir = new File(TEMP_TAKE_PHOTO_FILE_PATH);

//创建图片保存目录
if (!myImageDir.exists()) 
{
Mylog.d(THIS, "Create the path:" + TEMP_TAKE_PHOTO_FILE_PATH);
myImageDir.mkdirs();
}

//根据时间来命名
imagFile = File.createTempFile(""+System.currentTimeMillis(), ".jpg",myImageDir);

tmpuri = Uri.fromFile(imagFile);

i.putExtra(MediaStore.EXTRA_OUTPUT, tmpuri); 


startActivityForResult(i, TAKE_PHOTO_REQUEST_CODE);


从图库选择图片

Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"


innerIntent.setType("image/*"); // 查看类型


// StringIMAGE_UNSPECIFIED="image/*";详细的类型在com.google.android.mms.ContentType中


Intent wrapperIntent = Intent.createChooser(innerIntent, null);


act.startActivityForResult(wrapperIntent, SELECT_PHOTO_REQUEST_CODE);



返回后接收并调用系统裁剪工具

@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == MediaHelper.TAKE_PHOTO_REQUEST_CODE || requestCode == MediaHelper.SELECT_PHOTO_REQUEST_CODE) {
   
if (resultCode == RESULT_OK ) {
Uri uri = null;
if(requestCode == MediaHelper.SELECT_PHOTO_REQUEST_CODE) {
uri = intent.getData();
}else if(requestCode == MediaHelper.TAKE_PHOTO_REQUEST_CODE) {
uri = MediaHelper.tmpuri;
}


if (uri != null) {

    final Intent intent1 = new Intent("com.android.camera.action.CROP"); 
      intent1.setDataAndType(uri, "image/*");
      intent1.putExtra("crop", "true");
      intent1.putExtra("aspectX", 1);
      intent1.putExtra("aspectY", 1);
      intent1.putExtra("outputX", 132);
      intent1.putExtra("outputY", 132);
      intent1.putExtra("return-data", true);
      startActivityForResult(intent1, MediaHelper.CUT_PHOTO_REQUEST_CODE);
    
}
}

else if(requestCode == MediaHelper.CUT_PHOTO_REQUEST_CODE) {
if (resultCode == RESULT_OK && intent != null) {
bm= intent.getParcelableExtra("data");

}

}



}



在裁剪图片时,遇到有些图片不能按照某一指定的比例进行裁剪,查看了源码后才知道:系统的裁剪图片默认对图片进行人脸识别,当识别到有人脸时,会按aspectX和aspectY为1来处理,如果想设置成自定义的裁剪比例,需要设置noFaceDetection为true。

即iintent.putExtra("noFaceDetection", true);  取消人脸识别功能。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值