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

Posted

技术标签:

【中文标题】如何使用 Camera2 获取前置摄像头的 ID?【英文标题】:How to get id of front facing camera using Camera2? 【发布时间】:2015-04-23 20:40:26 【问题描述】:

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

【问题讨论】:

【参考方案1】:

我找到了解决方案,我可以通过在 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;

【讨论】:

【参考方案2】:

根据目前的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.");
    

【讨论】:

我在整个互联网上搜索了答案,只有你的答案有意义,谢谢

以上是关于如何使用 Camera2 获取前置摄像头的 ID?的主要内容,如果未能解决你的问题,请参考以下文章

使用后置摄像头的人脸检测无法使用 camera2 API 正常工作

Android 10(api 29)camera2 api回归与广角相机

Front Camera2 API GLSurfaceview

如何查看前置摄像头 ID?捕获的图像也是来自前置摄像头的镜像。

如何获取前置摄像头捕获的图像路径[重复]

安卓camera2 API获取YUV420_888格式详解