1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android大图片裁剪之手机拍照和从相册中选择注意点

Android大图片裁剪之手机拍照和从相册中选择注意点

时间:2020-08-11 03:23:16

相关推荐

Android大图片裁剪之手机拍照和从相册中选择注意点

Intent("com.android.camera.action.CROP");中data、MediaStore.EXTRA_OUTPUT以及return-data

data:Parcelable类型 传入的数据类型

MediaStore.EXTRA_OUTPUT:Uri类型自定义裁切输出的图片存储位置

return-data:boolean类型 是否有返回数据(有返回值,返回Bitmap,没有返回值是因为使用Uri与File相似,所有的操作如裁剪将数据都保存在了Uri中,已经持有了相应的URI也就无需多此一举再返回Bitmap。)

截大图用Uri,小图用Bitmap保存数据。

使用Bitmap并返回数据使用Uri不返回数据

图片的来源有相册和拍照:

相册截图采用大图Uri,小图Bitmap的数据存储方式。

//准备好需要使用到的Uri

private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

一、从相册截大图:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 600); intent.putExtra("outputY", 300); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("outputFormat", pressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, CHOOSE_BIG_PICTURE);

二、从相册截小图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1);intent.putExtra("outputX", 200); intent.putExtra("outputY", 100); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", pressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, CHOOSE_SMALL_PICTURE);

三、对应的onActivityResult可以这样处理返回的数据

switch (requestCode) {case CHOOSE_BIG_PICTURE:Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);if (imageUri != null) {Bitmap bitmap = decodeUriAsBitmap(imageUri);imageView.setImageBitmap(bitmap);}break; case CHOOSE_SMALL_PICTURE: if (data != null) { Bitmap bitmap = data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); } else { Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data); } break; default: break; }

private Bitmap decodeUriAsBitmap(Uri uri) {Bitmap bitmap = null;try {bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));} catch (FileNotFoundException e) {e.printStackTrace();return null; } return bitmap; }

效果图:

大图

小图

拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。

一、首先准备好需要使用到的Uri:

private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg"; Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);

二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE

三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。

switch (requestCode) { case TAKE_BIG_PICTURE:Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);//it seems to be nullcropImageUri(imageUri, 800, 400, CROP_BIG_PICTURE);break;case TAKE_SMALL_PICTURE:Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data); //TODO sent to crop cropImageUri(imageUri, 300, 150, CROP_SMALL_PICTURE); break; default: break; }

可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面。

private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");intent.putExtra("aspectX", 2);intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", pressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }

四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据。

switch (requestCode) {case CROP_BIG_PICTURE:// from crop_big_pictureLog.d(TAG, "CROP_BIG_PICTURE: data = " + data);if (imageUri != null) {Bitmap bitmap = decodeUriAsBitmap(imageUri);imageView.setImageBitmap(bitmap);}break;case CROP_SMALL_PICTURE: if (imageUri != null) { Bitmap bitmap = decodeUriAsBitmap(imageUri); imageView.setImageBitmap(bitmap); } else { Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data); } break; default: break; }

效果图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。