Android Camera2 front camera

前端 未结 4 1565
刺人心
刺人心 2021-02-19 02:46

I recently noticed that the Camera API is deprecated and I found the new API called Camera2.

I have read the documentation but I don\'t really understand it.

So

4条回答
  •  太阳男子
    2021-02-19 03:28

    First of all, find out the id of your front camera (if it has one of course)

        CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
                try {
                    return manager.getCameraIdList();
                } catch (CameraAccessException e) {
                    return null;
                }
    

    Then find the faceCamera like this:

    CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
    
            if (cameraCharacteristics == null)
                throw new NullPointerException("No camera with id " + cameraId);
    
            return cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
    

    Lastly, you have to set the camera with that id:

    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
       try {
           characteristics = manager.getCameraCharacteristics(mCameraId);
       }  catch (CameraAccessException e) {
           e.printStackTrace();
       } 
    

    Note, these are just tips on how to do what you wanna do. For details on how to start a preview and more, refer to: http://developer.android.com/samples/Camera2Basic/index.html

提交回复
热议问题