Android大图片裁剪解决方案

截图原理android

在Android中,Intent触发Camera程序,拍好照片后,将会返回数据,可是考虑到内存问题,Camera不会将全尺寸的图像返回给调用的Activity,通常状况下,有可能返回的是缩略图,好比120*160px。安全

    这是为何呢?这不是一个Bug,而是通过精心设计的,却对开发者不透明。app

    以个人小米手机为例,摄像头800W像素,根据我目前设置拍出来的图片尺寸为3200*2400px。有人说,那就返回呗,大不了耗1-2M的内存,不错,这个尺寸的图片确实只有1.8M左右的大小。可是你想不到的是,这个尺寸对应的Bitmap会耗光你应用程序的全部内存。Android出于安全性考虑,只会给你一个寒碜的缩略图。spa

    在Android2.3中,默认的Bitmap为32位,类型是ARGB_8888,也就意味着一个像素点占用4个字节的内存。咱们来作一个简单的计算题:3200*2400*4 bytes =   30M。.net

    如此惊人的数字!哪怕你愿意为一张生命周期超不过10s的位图愿意耗费这么巨大的内存,Android也不会答应的。翻译

1 Mobile devices typically have constrained system resources.
2 Android devices can have as little as 16MB of memory available to a single application.

    这是Android Doc的原文,虽然不一样手机系统的厂商可能围绕16M这个数字有微微的上调,可是这30M,通常的手机还真挥霍不起。也只有小米这种牛机,内存堪比我的PC,本着土财主般挥金如土的霸气才能作到。设计

    OK,说了这么多,无非是吐吐苦水,爆爆我的经历而已,实际的解决方案在哪里呢?code

    我也是Google到的,话说通常百度不了的问题,那就Google或者直接StackOverFlow,只不过得看英文罢了。orm

    最后翻来覆去,我在国外的一个Android团队的博客中找到了相应的方案,印证了个人猜测同时也给出了实际的代码。blog

    我将这篇文章翻译成了中文,做为本博客的基础,建议详细看看。

【译】如何使用Android MediaStore裁剪大图片

    这篇博客了不得的地方在于解决了Android对返回图片的大小限制,而且详细解释了裁剪图片的Intent附加数据的具体含义。OK,我只是站在巨人的肩膀上,改善方案,适应更普遍需求而已。

    拿图说事儿:

    Intent("com.android.camera.action.CROP")对应的全部可选数据都一目了然。在了解上面个个选项的含义以后,咱们将目光着眼于三个极为重要的选项:

    dataMediaStore.EXTRA_OUTPUT以及return-data

    data和MediaStore.EXTRA_OUTPUT都是可选的传入数据选项,你能够选择设置data为Bitmap,或者将相应的数据与URI关联起来,你也能够选择是否返回数据(return-data: true)。

    为何还有不用返回数据的选项?若是对URI足够了解的话,应该知道URI与File类似,你全部的操做如裁剪将数据都保存在了URI中,你已经持有了相应的URI,也就无需画蛇添足,再返回Bitmap了。

    前面已经说到,能够设置data为Bitmap,可是这种操做的限制在于,你的Bitmap不能太大。所以,咱们前进的思路彷佛明确了:截大图用URI,小图用Bitmap。

    我将这个思路整理成一张图片:

从相册截图

    根据咱们的分析与总结,图片的来源有拍照和相册,而可采起的操做有

  • 使用Bitmap并返回数据

  • 使用Uri不返回数据

    前面咱们了解到,使用Bitmap有可能会致使图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。

    咱们将要使用到URI来保存拍照后的图片:

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

    不难知道,咱们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。

    根据咱们上一篇博客的分析,我准备好了两个实例的Intent。

    1、从相册截大图:

01 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
02 intent.setType("image/*");
03 intent.putExtra("crop""true");
04 intent.putExtra("aspectX"2);
05 intent.putExtra("aspectY"1);
06 intent.putExtra("outputX"600);
07 intent.putExtra("outputY"300);
08 intent.putExtra("scale"true);
09 intent.putExtra("return-data"false);
10 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
11 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
12 intent.putExtra("noFaceDetection"true); // no face detection
13 startActivityForResult(intent, CHOOSE_BIG_PICTURE);

    2、从相册截小图

01 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
02 intent.setType("image/*");
03 intent.putExtra("crop""true");
04 intent.putExtra("aspectX"2);
05 intent.putExtra("aspectY"1);
06 intent.putExtra("outputX"200);
07 intent.putExtra("outputY"100);
08 intent.putExtra("scale"true);
09 intent.putExtra("return-data"true);
10 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
11 intent.putExtra("noFaceDetection"true); // no face detection
12 startActivityForResult(intent, CHOOSE_SMALL_PICTURE);

    3、对应的onActivityResult能够这样处理返回的数据

01 switch (requestCode) {
02 case CHOOSE_BIG_PICTURE:
03     Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null
04     if(imageUri != null){
05         Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap
06         imageView.setImageBitmap(bitmap);
07     }
08     break;
09 case CHOOSE_SMALL_PICTURE:
10     if(data != null){
11         Bitmap bitmap = data.getParcelableExtra("data");
12         imageView.setImageBitmap(bitmap);
13     }else{
14         Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);
15     }
16     break;
17 default:
18     break;
19 }
01 private Bitmap decodeUriAsBitmap(Uri uri){
02     Bitmap bitmap = null;
03     try {
04         bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
05     catch (FileNotFoundException e) {
06         e.printStackTrace();
07         return null;
08     }
09     return bitmap;
10 }

    效果图:

大图 小图


拍照截图

拍照截图有点儿特殊,要知道,如今的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是很是大的。所以,咱们不能像对待相册截图同样使用Bitmap小图,不管大图小图都统一使用Uri进行操做。

    1、首先准备好须要使用到的Uri:

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

    2、使用MediaStore.ACTION_IMAGE_CAPTURE能够轻松调用Camera程序进行拍照:

1 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
2 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
3 startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE

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

01 switch (requestCode) {
02 case TAKE_BIG_PICTURE:
03     Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);//it seems to be null
04     //TODO sent to crop
05     cropImageUri(imageUri, 800400, CROP_BIG_PICTURE);
06
07     break;
08 case TAKE_SMALL_PICTURE:
09     Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data);
10     //TODO sent to crop
11     cropImageUri(imageUri, 300150, CROP_SMALL_PICTURE);
12
13     break;
14 default:
15     break;
16 }

    能够看到,不管是拍大图片仍是小图片,都是使用的Uri,只是尺寸不一样而已。咱们将这个操做封装在一个方法里面。

01 private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){
02     Intent intent = new Intent("com.android.camera.action.CROP");
03     intent.setDataAndType(uri, "image/*");
04     intent.putExtra("crop""true");
05     intent.putExtra("aspectX"2);
06     intent.putExtra("aspectY"1);
07     intent.putExtra("outputX", outputX);
08     intent.putExtra("outputY", outputY);
09     intent.putExtra("scale"true);
10     intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
11     intent.putExtra("return-data"false);
12     intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
13     intent.putExtra("noFaceDetection"true); // no face detection
14     startActivityForResult(intent, requestCode);
15 }

    4、最后一步,咱们已经将数据传入裁剪图片程序,接下来要作的就是处理返回的数据了:

01 switch (requestCode) {
02 case CROP_BIG_PICTURE://from crop_big_picture
03     Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);//it seems to be null
04     if(imageUri != null){
05         Bitmap bitmap = decodeUriAsBitmap(imageUri);
06         imageView.setImageBitmap(bitmap);
07     }
08     break;
09 case CROP_SMALL_PICTURE:
10     if(imageUri != null){
11         Bitmap bitmap = decodeUriAsBitmap(imageUri);
12         imageView.setImageBitmap(bitmap);
13     }else{
14         Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
15     }
16     break;
17 default:
18     break;
19 }

效果图:

相关文章
相关标签/搜索