Android實現拍照截圖功能
本文將向大家展示如何拍照截圖。
先看看效果圖:

拍照截圖有點兒特殊,要知道,現在的Android智能手機的攝像頭都是幾百萬的像素,拍出來的圖片都是非常大的。因此,我們不能像對待相冊截圖一樣使用Bitmap小圖,無論大圖小圖都統(tǒng)一使用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;
}
以上就是Android實現拍照截圖功能的方法,希望對大家的學習有所幫助。
相關文章
Android Studio 3.1.X中導入項目的正確方法分享
這篇文章主要給大家介紹了關于Android Studio 3.1.X中導入項目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07
Android自定義processor實現bindView功能的實例
下面小編就為大家分享一篇Android自定義processor實現bindView功能的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12

