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

如何使用新的Camera2库获取前置摄像头的ID? 我无法在文档中的任何地方find它。

Solutions Collecting From Web of "如何使用Camera2获取前置摄像头的ID?"

我已经find了解决scheme,我可以通过调用getCameraCharacteristics(cameraId);来获得相机的特性getCameraCharacteristics(cameraId);CameraManager对象上。

 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."); } }