gpt4 book ai didi

android - 如何在 android 中使用 Intent 打开前置摄像头?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:58 25 4
gpt4 key购买 nike

我想制作一个只需要打开前置摄像头的应用程序,我该如何使用 intent 来实现?

private void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

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

最佳答案

Java

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri photoUri = Uri.fromFile(getOutputPhotoFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE);

其他/替代解决方案

private Camera openFrontFacingCameraGingerbread() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.toString());
}
}
}

return cam;
}

AndroidManifest.xml 文件中添加这些权限

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />

only available in Gingerbread(2.3) and Up Android Version.

否则你也可以检查这些例子

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

希望对你有帮助

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

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