Android實(shí)現(xiàn)拍照截圖功能
本文將向大家展示如何拍照截圖。
先看看效果圖:
拍照截圖有點(diǎn)兒特殊,要知道,現(xiàn)在的Android智能手機(jī)的攝像頭都是幾百萬的像素,拍出來的圖片都是非常大的。因此,我們不能像對待相冊截圖一樣使用Bitmap小圖,無論大圖小圖都統(tǒng)一使用Uri進(jìn)行操作。
一、首先準(zhǔn)備好需要使用到的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可以輕松調(diào)用Camera程序進(jìn)行拍照:
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中拿到返回的數(shù)據(jù)(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); }
四、最后一步,我們已經(jīng)將數(shù)據(jù)傳入裁剪圖片程序,接下來要做的就是處理返回的數(shù)據(jù)了:
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; }
以上就是Android實(shí)現(xiàn)拍照截圖功能的方法,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android實(shí)現(xiàn)登錄功能demo示例
這篇文章主要介紹了Android實(shí)現(xiàn)登錄功能demo示例,涉及登錄信息操作、界面布局、登錄邏輯判斷等相關(guān)操作技巧,需要的朋友可以參考下2016-07-07Android Studio 3.1.X中導(dǎo)入項目的正確方法分享
這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Android自定義控件實(shí)現(xiàn)隨手指移動的小球
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)隨手指移動的小球,隨著手指觸摸移動而移動的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解
這篇文章主要介紹了Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-05-05Android自定義processor實(shí)現(xiàn)bindView功能的實(shí)例
下面小編就為大家分享一篇Android自定義processor實(shí)現(xiàn)bindView功能的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Android獲取SD卡路徑及SDCard內(nèi)存的方法
這篇文章主要介紹了Android獲取SD卡路徑及SDCard內(nèi)存的方法,較為詳細(xì)的分析了Android針對SD卡操作所涉及的類及其具體函數(shù)功能,非常具有實(shí)用價值,需要的朋友可以參考下2015-02-02