Android大圖片裁剪終極解決方案 原理分析

來源:互聯網
上載者:User

標籤:des   android   code   http   tar   ext   

約幾個月前,我正為公司的APP在Android手機上實現拍照而煩惱不已。

上網搜尋,確實有不少的例子,大多都是抄來抄去,而且水平多半處於demo的樣子,可以用來講解知識點,但是一碰到實際項目,就漏洞百出。

當時我用福士化的解決方案,暫時性的做了一個拍照的功能,似乎看起來很不錯。問題隨之而來,我用的是小米手機,在別的手機上都運行正常,小米這裡卻總是碰釘子。雖然我是個理性的米粉,但是也暗地裡把小米的工程師問候了個遍。真是慚愧!

翻文檔也找不出個答案來,我一直對com.android.camera.action.CROP持有大大的疑問,它是從哪裡來,它能幹什麼,它接收處理什麼類型的資料?Google對此卻諱莫如深,在官方文檔中只有Intent中有隻言片語言及,卻不甚詳盡。

隨著項目的驅動,我不能抱著不瞭解原理就不往前走的心態,唯一要做的,是解決問題。最後在德問上找到一條解決方案,說是哪怕是大米也沒問題。當時樂呵呵將代碼改了改,確實在所有的手機上跑起來了,一時如釋重負,對這個的疑問也拋諸腦後了。

直到月前,BOSS要求將拍照上傳到伺服器的圖片解析度加倍。OK,加倍簡單,增加outputX以及outputY不就得了?

intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY);

這一增加,嚇了我一跳。BOSS的手機拍到的照片幾乎就是個縮圖,但是被我問候了全體工程師的小米在這個時候就體現出國產神機的範兒了,小米上的尺寸一切正常。這個為什麼呢?我大致瞭解原因,卻不知道如何解決。

在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。

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

這篇主要讓大家瞭解需求的來源,以及如何去思考分析並解決問題。下一篇將介紹具體的操作。

 

在這篇文章中,我將向大家展示如何從相簿。

上一篇文章中,我就拍照這一需求進行了詳細的分析,試圖讓大家瞭解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相簿。在這篇部落格中,我將向大家展示如何拍照。

拍照有點兒特殊,要知道,現在的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; }

:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.