來源:CSDN 作者:floodingfire
約幾個月前,我正為公司的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;
}
效果圖:
源碼下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/11月/11日/Android大圖片裁剪終極解決方案