Android 拍照或从相册取图片并裁剪

  在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,所以用拍照得到的图片需要裁剪处理后使用。

Android裁剪图片的Intent附加数据的具体含义,拿图说事儿:

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。

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

从相册截图

在上面,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。

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

  • 使用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;

    }

摘选自:https://github.com/ryanhoo/PhotoCropper

时间: 2024-08-30 02:22:05

Android 拍照或从相册取图片并裁剪的相关文章

Android 拍照、从相册获取及裁剪的相关实现

首先这些功能都是通过Intent去启动系统的服务去实现的,所以自然就有相应的Action.相关Actiong如下: 拍照——MediaStore.ACTION_IMAGE_CAPTURE ("android.media.action.IMAGE_CAPTURE") 相册——Intent.ACTION_GET_CONTENT("android.intent.action.GET_CONTENT" 同时要设置,intent.setType("image/*&q

android 拍照和从相册选择组件

android 拍照及从相册选择组件 单独封装到一个 activity 中便于更好的复用 拍照或从相册选择成功后使用 EventBus 发出广播回传图片路径,和调用者充分解耦合 根据传入参数支持裁剪和不裁剪两种模式 /** * <pre> * 拍照及从相册选择弹出 activity * 成功后会发送 TakePhotoOutputEvent 事件,返回图片路径 * </pre> */ public class TakePhotoPopupActivity extends Activ

Android拍照后更新相册

方法一: Uri updateUri = Uri.fromFile(file); Intent updateIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, updateUri); sendBroadcast(updateIntent); 方法二: ContentValues localContentValues = new ContentValues(); localContentValues.put("_data"

Android 拍照或者从相册获取图片的实现

我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调用系统方法的好用工具! 首先,需要设计下我们想怎么调用系统的拍照或者选取图片的方法 我们可以点击头像或者一个按钮然后弹出一个对话框,让用户自己 选择是拍照还是选择图片(如下图) . 那这个对话框怎么写呢.通过AlertDialog来实现(我们就给这个方法起名叫dialog): //对头像操作 pri

Android拍照调用系统相册仿微信封装总结,治疗各种崩溃,图片横竖问题压缩等问题。

项目下载地址:https://github.com/Aiushtha/android-PictureSelector 最早使用android调用系统拍照然后遇到很多空指针等问题 以及各种android 不同版本Intent取data有时候会空指针之类的api兼容问题 像使用红米note在开了很多应用后,再启动拍照系统,会发生拍照崩溃图片丢失等问题 用微信控件有时拍照有极小概率拍照无效等等奇怪的问题 其原因是因为Activity被回收了,变量变成null, 参考下面一篇博客 http://blog

Android选取相机、相册图片进行裁剪,并更新UI

demo源码:http://download.csdn.net/detail/u010778159/8648701 效果图: 界面非常的简单,只有一个imageView,通过点击该ImageView,从相册中选取照片,或拍照,将得到的照片按要求进行裁剪,然后将裁剪后的照片更新到ImageView中. 现在,来看一下工程的xml,和.java文件: 有两个xml文件,main.xml是进入app时的主页面,有一个ImageView select_pic_layout.xml是点击ImageView

Android拍照、相册选取、裁剪图片

来自:http://blog.csdn.net/ryantang03/article/details/8656278 package com.example.listactivity; import java.io.ByteArrayOutputStream; import java.io.File; import com.example.model.ImageTools; import android.app.Activity; import android.app.AlertDialog;

Android拍照,相册选择图片以及Android6.0权限管理

概述 在android开发过程中,拍照或者从相册中选择图片是很常见的功能.下面要说得这个案例比较简单,用户点击按钮选择拍照或者打开相册选择图片,然后将选中的图片显示在手机上.android6.0后,推出了动态权限管理.以往我们将涉及到的权限全部写在清单文件中,只要用户安装了该程序,程序在运行过程中都会获得相应权限.android6.0后,对于一些特别敏感的权限,开发者必须在程序中进行声明.拍照和从相册选择图片都是涉及到用户隐私的敏感权限,必须在程序中进行声明. 大概的流程 创建布局文件,这里不多

Android拍照或从图库选择图片并裁剪

今天看<第一行代码>上面关于拍照和从相册选取图片那一部分,发现始终出不来效果,所以搜索其他资料学习一下相关知识,写一个简单的Demo. 一. 拍照选择图片 1.使用隐式Intent启动相机 //构建隐式Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用系统相机 startActivityForResult(intent, 1); 2.处理相机拍照返回的结果 //用户点击了取消 if(data == n