Android 拍照或從相冊取圖片並裁剪

在Android中,Intent觸發Camera程序,拍好照片後,將會返回數據,可是kao慮到內存問題,Camera不會將全尺寸的圖像返回給調用的Activity,通常狀況下,有可能返回的是縮略圖,好比120*160px。android

    這是爲何呢?這不是一個Bug,而是通過精心設計的,卻對開發者不透明。git

    好比攝像頭800W像素,根據我目前設置拍出來的圖片尺寸爲3200*2400px。有人說,那就返回唄,大不了耗1-2M的內存,不錯,這個尺寸的圖片確實只有1.8M左右的大小。可是你想不到的是,這個尺寸對應的Bitmap會耗光你應用程序的全部內存。Android出於安全性kao慮,只會給你一個寒磣的縮略圖。github

    在Android2.3中,默認的Bitmap爲32位,類型是ARGB_8888,也就意味着一個像素點佔用4個字節的內存。咱們來作一個簡單的計算題:3200*2400*4 bytes =   30M,因此用拍照獲得的圖片須要裁剪處理後使用。安全

Android裁剪圖片的Intent附加數據的具體含義,拿圖說事兒:spa

    Intent("com.android.camera.action.CROP")對應的全部可選數據都一目瞭然。在瞭解上面個個選項的含義以後,咱們將目光着眼於三個極爲重要的選項:.net

    data、MediaStore.EXTRA_OUTPUT以及return-data。設計

    data和MediaStore.EXTRA_OUTPUT都是可選的傳入數據選項,你能夠選擇設置data爲Bitmap,或者將相應的數據與URI關聯起來,你也能夠選擇是否返回數據(return-data: true)。code

    爲何還有不用返回數據的選項?若是對URI足夠了解的話,應該知道URI與File類似,你全部的操做如裁剪將數據都保存在了URI中,你已經持有了相應的URI,也就無需畫蛇添足,再返回Bitmap了。orm

    前面已經說到,能夠設置data爲Bitmap,可是這種操做的限制在於,你的Bitmap不能太大。所以,咱們前進的思路彷佛明確了:截大圖用URI,小圖用Bitmap。圖片

    我將這個思路整理成一張圖片:

 

從相冊截圖

    在上面,我就拍照截圖這一需求進行了詳細的分析,試圖讓你們瞭解Android自己的限制,以及咱們應當採起的實現方案。

    根據咱們的分析與總結,圖片的來源有拍照和相冊,而可採起的操做有

  • 使用Bitmap並返回數據

  • 使用Uri不返回數據

    前面咱們瞭解到,使用Bitmap有可能會致使圖片過大,而不能返回實際大小的圖片,我將採用大圖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

    不難知道,咱們從相冊選取圖片的Action爲Intent.ACTION_GET_CONTENT。

    根據咱們上一篇博客的分析,我準備好了兩個實例的Intent。

    1、從相冊截大圖:

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 detectionstartActivityForResult(intent, CHOOSE_BIG_PICTURE);

    2、從相冊截小圖

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 detectionstartActivityForResult(intent, CHOOSE_SMALL_PICTURE);

    3、對應的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進行操做。

    1、首先準備好須要使用到的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

    2、使用MediaStore.ACTION_IMAGE_CAPTURE能夠輕鬆調用Camera程序進行拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is captureintent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE

    3、接下來就能夠在 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);

    }

    4、最後一步,咱們已經將數據傳入裁剪圖片程序,接下來要作的就是處理返回的數據了:

    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

相關文章
相關標籤/搜索