Android大圖裁剪解決辦法

 某些功能需要拍照或者從相冊選擇照片後經過裁剪再上傳的時候,

cropimage

cropimage

可以調用手機自帶的com.android.camera.action.CROP這個Intent進行裁剪
通過設置輸出大小可以得到圖片的大小:
intent.putExtra(“outputX”, outputX);
intent.putExtra(“outputY”, outputY);
但是當outputX或者outputY 大小設置爲320以上的時候,會發現完全沒有效果。
通過搜索才發現了這個問題原來是這樣的:
Mobile devices typically have constrained system resources.
Android devices can have as little as 16MB of memory available to a single application.
在Android2.3中,默認的Bitmap爲32位,類型是ARGB_8888,
也就意味着一個像素點佔用4個字節的內存。3200*2400*4 bytes = 30M。
消耗這樣大的內存當然不可能實現。

看看com.android.camera.action.CROP這個Intent可以設置的參數:

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

使用return Bitmap的話有限制不能太大,那麼如果要裁剪大圖的話只能使用URI這個參數了。
public Intent getCropImageIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType(“image/*”);
intent.putExtra(“crop”, “true”);
intent.putExtra(“aspectX”, 1);
intent.putExtra(“aspectY”, 1);
intent.putExtra(“outputX”, 600);
intent.putExtra(“outputY”, 600);
intent.putExtra(“noFaceDetection”, true);
intent.putExtra(“scale”, true);
intent.putExtra(“return-data”, false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intent.putExtra(“outputFormat”, Bitmap.CompressFormat.JPEG.toString());
return intent;
}

源碼下載地址:http://06peng.com/archives/192

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章