您的位置:首页 > 移动开发 > Android开发

android调用系统的相机拍照 、裁剪,然后保存

2016-08-31 16:53 393 查看
上一篇说了用surfaceView来拍照,现在说一下调用系统的拍照、相册和裁剪。

由于调用系统的拍照可能返回的data非常大,很容易导致OOM。所以在项目中都是给它传URI,然后让它把图片保存到URI,我们再跟进URI来读取图片。

首先我们是不是得准备一个File ,让系统把结果存在FIle里。File tempFile = new File(Environment.getExternalStorageDirectory(),"lv.png");
调用系统拍照的Intent:

Intent cameraintent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(tempFile));
cameraintent.putExtra("return-data", false);//不返回数据
cameraintent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
cameraintent.putExtra("noFaceDetection", true);//开启人脸识别
startActivityForResult(cameraintent,
AccountInfoPresenter.PHOTO_REQUEST_TAKEPHOTO);



拍完后就是onActivityResult的处理了,由于系统帮我们保存的到URI的图片可能会被旋转了,所以我们要把它转回来,
/**
* 读取图片属性:旋转的角度
* @param path 图片绝对路径
* @return degree旋转的角度
*/
public static int readPictureDegree(String path) {
int degree  = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}

if (0==degree) {
return degree;
}

} catch (IOException e) {
e.printStackTrace();
}

try {
Bitmap bitmap=BitmapFactory.decodeFile(path);

Matrix m = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
m.setRotate(degree); // 旋转angle度
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);// 从新生成
FileOutputStream out = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
bitmap.recycle();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return degree;
}
图片也旋转完了,我们是不是得调用到系统的裁剪呢,下面是跳转到裁剪:
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));

// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);

// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("return-data", false);
intent.putExtra("noFaceDetection", true);
AccountInformationFragment.this.startActivityForResult(intent, AccountInfoPresenter.PHOTO_REQUEST_CUT);

}
系统裁剪后也是把结果保存到指定的URI中,我们就在onActivityResult中处理它:
if (data != null) {

try {
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile));
imgAccountIcon.setImageBitmap(bitmap);
imgAccountBg.setBackgroundDrawable((new BitmapDrawable(FastBlur.doBlur(bitmap, 10, false))));
isEditAviator = true;
} catch (Exception e) {
e.printStackTrace();
}

}
接下来是调用系统相册的:
Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
getAlbum.setType("image/*");
startActivityForResult(getAlbum, AccountInfoPresenter.PHOTO_REQUEST_GALLERY);
在相册选择图片后,一般会在onActivityResult回调的返回的Intent返回URI,注意,我说的是一般,有些特别的机子是不会返回的,所以我们得判断是否空再跳转到裁剪界面
// 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同
if (data != null) {
System.out.println("11================");
startPhotoZoom(data.getData());
} else {
System.out.println("================");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: