上面包含了所有可选的操作,其中有一些非常重要的参数。
intent.putExtra(“return-data”, true):表示裁剪后返回的数据为Bitmap,是存在内存中的缩略图,效果模糊。获取的方式为,在Activity中的onActivityResult方法中:
Bundle bundle = data.getExtras();Bitmap bitmap = bundle.getParcelable("data");为了获取到裁切后的原图,我们选择将剪切的图片保存在本地,然后调用本地的图片,并不直接返回Bitmap.
intent.putExtra("return-data", false);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);下面代码实现拍照/剪切并进行显示的
public static int TAKE_PHOTO_REQUEST_CODE = 1; //拍照 public static int PHOTO_REQUEST_CUT = 2; //裁切 public static int PHOTO_REQUEST_GALLERY = 3; //相册 public Uri imageUri; /*** 打开相机拍照*/ private void takePhotos() {imageUri = Uri.fromFile(getImageStoragePath(this));Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//指定照片存储路径intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent,TAKE_PHOTO_REQUEST_CODE); }/*** 打开相册选择图片*/ private void choicePicFromAlbum() {Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(intent, PHOTO_REQUEST_GALLERY); }/*** 设置图片保存路径* @return*/ private File getImageStoragePath(Context context){if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES),"temp.jpg"); return file;}return null; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == TAKE_PHOTO_REQUEST_CODE){ if (imageUri != null){startPhotoZoom(imageUri); }}else if (requestCode == PHOTO_REQUEST_CUT){ if (imageUri != null) {Bitmap bitmap = decodeUriBitmap(imageUri);imageView.setImageBitmap(bitmap); }}else if (requestCode == PHOTO_REQUEST_GALLERY){ if (data != null) {imageUri = data.getData();Bitmap bitmap = decodeUriBitmap(imageUri);imageView.setImageBitmap(bitmap); }} } private Bitmap decodeUriBitmap(Uri uri) {Bitmap bitmap = null;try { bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));} catch (FileNotFoundException e) { e.printStackTrace(); return null;}return bitmap; } /*** 调用系统裁剪** @param uri*/ 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("scale", true);// aspectX aspectY 是宽高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX,outputY 是剪裁图片的宽高intent.putExtra("outputX", 800);intent.putExtra("outputY", 800);//设置了true的话直接返回bitmap,可能会很占内存intent.putExtra("return-data", false);//设置输出的格式intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//设置输出的地址intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//不启用人脸识别intent.putExtra("noFaceDetection", true);startActivityForResult(intent, PHOTO_REQUEST_CUT); }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。