gpt4 book ai didi

android - 如何通过 Intent 在android中打开前后摄像头?

转载 作者:行者123 更新时间:2023-11-29 15:39:17 27 4
gpt4 key购买 nike

我们实现相机的目的是它在 micromax480p 5.1 版中工作正常但是当我们在 Nexus7 6.1 版中使用时相机已打开但我想打开一些时间正面和一些时间背面是否可以根据我们打开需要。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//intent.addCategory(Intent.CATEGORY_OPENABLE);
//intent.setAction(Intent.ACTION_GET_CONTENT);
if(camera == 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);

} else {
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
}
}


// start the image capture Intent
startActivityForResult(intent, requestCode);

我是这样使用的,但它默认打开我们以前使用的相机(正面和背面)。谢谢。对不起,我的英语交流能力很弱。

最佳答案

要打开摄像头:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

要打开前置摄像头:-

val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.O -> {
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 24 Android version 7.0(Samsung S6)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", CameraCharacteristics.LENS_FACING_FRONT) // Tested on API 27 Android version 8.0(Nexus 6P)
cameraIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true)
}
else -> cameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1) // Tested API 21 Android version 5.0.1(Samsung S4)
}
startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA)

我无法让它适用于 API 28 及更高版本。此外,某些设备无法直接打开前置摄像头(取决于制造商)。

关于android - 如何通过 Intent 在android中打开前后摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com