Get ID of currently open camera

只谈情不闲聊 提交于 2021-02-18 08:52:54

问题


How can I get the ID of the currently open android camera from an android camera instance? I can't see it in the parameters and getCameraInfo requires the id as a parameter.


回答1:


There isn't a way to get the id of the currently open android camera. I ended up storing the id when I opened it.




回答2:


It is just a number of the camera, so you loop through looking for the camera you want.

Here is a snippet to find the front-facing camera:

int cameraId = -1;
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(i, info);
  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
    Log.d(DEBUG_TAG, "Camera found");
    cameraId = i;
    break;
  }
}



回答3:


  private int findFrontFacingCameraID() {
        int cameraId = -1;
        // Search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
          CameraInfo info = new CameraInfo();
          Camera.getCameraInfo(i, info);
          if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            Log.d(TAG, "Camera found");
            cameraId = i;
            break;
          }
        }
        return cameraId;
     }



回答4:


For Kotlin with newer camera API:

fun getCameraId(context: Context, facing: Int): String {
    val manager = context.getSystemService(CAMERA_SERVICE) as CameraManager

    return manager.cameraIdList.first {
        manager
            .getCameraCharacteristics(it)
            .get(CameraCharacteristics.LENS_FACING) == facing
    }
}

Valid values for facing are:

CameraCharacteristics.LENS_FACING_FRONT
CameraCharacteristics.LENS_FACING_BACK
CameraCharacteristics.LENS_FACING_EXTERNAL


来源:https://stackoverflow.com/questions/21559699/get-id-of-currently-open-camera

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!