国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現拍照截圖功能

2020-04-11 10:58:42
字體:
來源:轉載
供稿:網友

本文將向大家展示如何拍照截圖。

先看看效果圖:

拍照截圖有點兒特殊,要知道,現在的Android智能手機的攝像頭都是幾百萬的像素,拍出來的圖片都是非常大的。因此,我們不能像對待相冊截圖一樣使用Bitmap小圖,無論大圖小圖都統一使用Uri進行操作。

一、首先準備好需要使用到的Uri:

privatestatic final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

二、使用MediaStore.ACTION_IMAGE_CAPTURE可以輕松調用Camera程序進行拍照:

Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);//action is captureintent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE

三、接下來就可以在 onActivityResult中拿到返回的數據(Uri),并將Uri傳遞給截圖的程序。

switch(requestCode) {caseTAKE_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;caseTAKE_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,只是尺寸不同而已。我們將這個操作封裝在一個方法里面

privatevoid cropImageUri(Uri uri, intoutputX, intoutputY, intrequestCode){  Intent intent = newIntent("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) {caseCROP_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;caseCROP_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實現拍照截圖功能的方法,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广德县| 长垣县| 兴业县| 凤山市| 庐江县| 汝城县| 莱芜市| 高要市| 永安市| 塘沽区| 禄丰县| 江山市| 巴中市| 贵南县| 武邑县| 招远市| 长顺县| 东乌| 将乐县| 咸丰县| 独山县| 九寨沟县| 华宁县| 普安县| 深水埗区| 牙克石市| 镇江市| 延吉市| 兰考县| 白城市| 大关县| 闵行区| 西安市| 阿尔山市| 盐津县| 威信县| 泌阳县| 沾化县| 伊春市| 扎鲁特旗| 梁平县|