调用系统拍照
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); 取消人脸识别功能。