Android调用系统相机和图片获取照片并裁剪

前言:当下许多程序都有图片上传、发送等功能,这就需要调用摄像头拍照或者获取系统图库的照片,此功能较为常用,在结合网上一些教程及实践,总结出的一份代码,在此记录一下。

首先定义一些程序需要用到的常量

1
2
3
4
5
6
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1; //调用照相机
private static final int PHOTO_ALBUM_REQUEST_CODE = 2; //调用图库
private static final int CROP_PICTURE = 3; //裁剪
private static final int PICTURE_WIDTH = 200; //图片输出大小
private static final int PICTURE_HEIGHT = 200; //图片输出大小
private Uri imageUri; //图片地址

使用Intent调用系统图库,Action为Intent.ACTION_GET_CONTENT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 调用系统图库
*/
private void photoAlbum() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
File outputImage = new File(Environment.getExternalStorageDirectory(), "portrait.png");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
intent.setType("image/*");
intent.putExtra("crop", "true");
//裁剪框比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//图片输出大小
intent.putExtra("outputX", PICTURE_WIDTH);
intent.putExtra("outputY", PICTURE_HEIGHT);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//不启用人脸识别
intent.putExtra("noFaceDetection", false);
startActivityForResult(intent, PHOTO_ALBUM_REQUEST_CODE);
}

使用Intent调用系统相机,Action为android.media.action.IMAGE_CAPTURE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 调用系统照相机
*/
private void captureImage() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File outputImage = new File(Environment.getExternalStorageDirectory(), "portrait.png");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent,CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

使用Intent调用系统裁剪,Action为com.android.camera.action.CROP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 裁剪
*/
private void cropPicture() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("crop", "true");//可裁剪
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", PICTURE_WIDTH);
intent.putExtra("outputY", PICTURE_HEIGHT);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("return-data", true);//若为false则表示不返回数据
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, CROP_PICTURE);
}

系统调用相册、相机、裁剪操作后回调,在onActivityResult里根据requestCode进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 拍照和系统图库选择图片返回事件处理
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE: //拍照结果回调,进行裁剪
cropPicture();
break;
case PHOTO_ALBUM_REQUEST_CODE: //系统图库回调
try {
Bitmap bitmap = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(imageUri));
if (bitmap == null) return;
Log.d(TAG, "photo album" + bitmap.getRowBytes() * bitmap.getHeight());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
case CROP_PICTURE: //拍照后裁剪回调
try {
Bitmap bitmap = BitmapFactory.decodeStream(getApplicationContext().getContentResolver().openInputStream(imageUri));
if (bitmap == null) return;
Log.d(TAG, "take photo" + bitmap.getRowBytes() * bitmap.getHeight());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
break;
}
}