如何使用 Camera2 获取前置摄像头的 ID?

How to get id of front facing camera using Camera2?

如何使用新的 Camera2 库获取前置摄像头的 ID?我在文档中的任何地方都找不到它。

我找到了解决方案,我可以通过在 CameraManager 对象上调用 getCameraCharacteristics(cameraId); 来获取相机特性。

String getFrontFacingCameraId(CameraManager cManager){
    for(final String cameraId : cManager.getCameraIdList()){
        CameraCharacteristics characteristics = cManager.getCameraCharacteristics(cameraId);
        int cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
        if(cOrientation == CameraCharacteristics.LENS_FACING_FRONT) return cameraId;
    }
    return null;
}

根据目前Android API,你应该添加这个方法:

 private String getFrontFacingCameraId(CameraManager cManager) {
    try {
        String cameraId;
        int cameraOrientation;
        CameraCharacteristics characteristics;
        for (int i = 0; i < cManager.getCameraIdList().length; i++) {
            cameraId = cManager.getCameraIdList()[i];
            characteristics = cManager.getCameraCharacteristics(cameraId);
            cameraOrientation = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (cameraOrientation == CameraCharacteristics.LENS_FACING_FRONT) {
                return cameraId;
            }

        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
    return null;
}

确保在方法内部调用它:

/**
 * Tries to open a {@link CameraDevice}. The result is listened by `mStateCallback`.
 */
private void openCamera(int width, int height) {
    if (!hasPermissionsGranted(VIDEO_PERMISSIONS)) {
        requestVideoPermissions();
        return;
    }
    final Activity activity = getActivity();
    if (null == activity || activity.isFinishing()) {
        return;
    }
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        Log.d(TAG, "tryAcquire");
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        //String cameraId = manager.getCameraIdList()[0];//It was the original way
        String cameraId = getFrontFacingCameraId(manager);//That's the good one to reach FRONT

        // Choose the sizes for camera preview and video recording
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
        StreamConfigurationMap map = characteristics
                .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
        mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
                width, height, mVideoSize);

        int orientation = getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        } else {
            mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
        }
        configureTransform(width, height);
        mMediaRecorder = new MediaRecorder();
        manager.openCamera(cameraId, mStateCallback, null);
    } catch (CameraAccessException e) {
        Toast.makeText(activity, "Cannot access the camera.", Toast.LENGTH_SHORT).show();
        activity.finish();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error))
                .show(getChildFragmentManager(), FRAGMENT_DIALOG);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.");
    }
}