Android 10.0 Camera2 拍照功能默认选前摄像头

1.概述


 在10.0的系统产品开发中,对于app调用系统api来打开摄像头拍照的功能也是常有的功能,而拍照一般是默认打开后置摄像头拍照的,由于
客户的产品特殊要求,需要打开前置摄像头拍照功能,所以需要了解拍照功能的流程,然后修改默认前置摄像头打开拍照功能就可以了

app调用拍照功能如下:

private void photograph(String outputimagepath){
try//判断图片是否存在,存在则删除在创建,不存在则直接创建

{

if (!outputimagepath.getParentFile().exists()) {

outputimagepath.getParentFile().mkdirs();

}

if (outputimagepath.exists()) {

outputimagepath.delete();

}

outputimagepath.createNewFile();
Uri imagUri = null;
if (Build.VERSION.SDK_INT >= 24) {

imageUri = FileProvider.getUriForFile(this,

"com.wj.phone.fileprovider", outputimagepath);

} else {

imageUri = Uri.fromFile(outputimagepath);

}

//使用隐示的Intent,系统会找到与它对应的活动,即调用摄像头,并把它存储

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(in
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
这段代码是一个使用 EasyPermissions 库来请求相机权限,并启动相机拍照的方法。如果您需要在拍照后将照片保存到文件中,可以在 `onActivityResult()` 方法中处理照片数据并保存到指定位置。 完整示例如下: ```java private static final int REQUEST_CAMERA_PERMISSION = 1; private static final int REQUEST_IMAGE_CAPTURE = 2; private String currentPhotoPath; @AfterPermissionGranted(REQUEST_CAMERA_PERMISSION) private void takePicture() { String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}; if (EasyPermissions.hasPermissions(this, perms)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { // Create the File where the photo should go File photoFile = null; try { photoFile = createImageFile(); } catch (IOException ex) { // Error occurred while creating the File ex.printStackTrace(); } // Continue only if the File was successfully created if (photoFile != null) { Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } } else { EasyPermissions.requestPermissions(this, "需要相机权限来拍照和存储照片", REQUEST_CAMERA_PERMISSION, perms); } } private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents currentPhotoPath = image.getAbsolutePath(); return image; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { // Get the dimensions of the View int targetW = imageView.getWidth(); int targetH = imageView.getHeight(); // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeFile(currentPhotoPath, bmOptions); int photoW = bmOptions.outWidth; int photoH = bmOptions.outHeight; // Determine how much to scale down the image int scaleFactor = Math.min(photoW/targetW, photoH/targetH); // Decode the image file into a Bitmap sized to fill the View bmOptions.inJustDecodeBounds = false; bmOptions.inSampleSize = scaleFactor; bmOptions.inPurgeable = true; Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions); imageView.setImageBitmap(bitmap); // Save the bitmap to a file try { FileOutputStream out = new FileOutputStream(currentPhotoPath); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个示例中,我们在 `takePicture()` 方法中请求了相机和存储权限,然后创建了一个用于保存照片的文件,并启动相机拍照。在 `onActivityResult()` 方法中,我们获取了拍摄的照片数据,并将其显示在一个 ImageView 中。然后,我们将照片保存到之创建的文件中。请注意,在 `createImageFile()` 方法中,我们使用了 FileProvider 来获取照片文件的 Uri,以便在 Android N 及以上版本中能够正常使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安卓兼职framework应用工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值