Android大图片裁剪终极解决方案

http://www.linuxidc.com/Linux/2012-11/73940.htm


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

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

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

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

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

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

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

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

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

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

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

【译】如何使用Android MediaStore裁剪大图片 http://www.linuxidc.com/Linux/2012-11/73939.htm

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

拿图说事儿:


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

data、MediaStore.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来保存拍照后的图片:

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

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

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

一、从相册截大图:

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", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
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", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CHOOSE_SMALL_PICTURE);

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

switch (requestCode) {
case CHOOSE_BIG_PICTURE:
 Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null
 if(imageUri != null){
  Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap
  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";//temp file
Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

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

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is 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 null
 //TODO sent to crop
 cropImageUri(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", Bitmap.CompressFormat.JPEG.toString());
 intent.putExtra("noFaceDetection", true); // no face detection
 startActivityForResult(intent, requestCode);
}

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

switch (requestCode) {
case CROP_BIG_PICTURE://from crop_big_picture
 Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);//it seems to be null
 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;
}

效果图:

源码下载:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/11月/11日/Android大图片裁剪终极解决方案








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中进行图片裁剪,可以按照以下步骤进行操作: 1. 首先,在打开相册或者拍照的点击事件中,添加启动图片裁剪的逻辑。你可以使用系统自带的裁剪功能,也可以使用第三方库进行裁剪。如果使用系统自带的裁剪功能,可以在Intent中添加以下代码: ```java intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); ``` 2. 接下来,在`onActivityResult`方法中处理裁剪后的结果。可以通过`data.getExtras().getParcelable("data")`获取裁剪后的图片。如果你使用的是第三方库进行裁剪,那么根据第三方库的文档来获取裁剪后的图片。 3. 最后,你可以将裁剪后的图片保存到指定的位置,可以使用以下代码来保存图片: ```java Bitmap bitmap = (Bitmap) data.getExtras().getParcelable("data"); File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "crop_image.png"); try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } ``` 以上就是在Android Studio中进行图片裁剪的步骤。你可以根据自己的需求选择合适的方法和库来进行图片裁剪。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值