How to detect if there is front camera and if there is how to reach and use front camera?

后端 未结 4 1982
眼角桃花
眼角桃花 2021-01-06 04:03

How to detect if there is a front camera and if there is how to reach and use front camera ?

4条回答
  •  暖寄归人
    2021-01-06 04:58

    If you are using API level 9 (Android 2.3) or above, you can do the following to calculate the index of the (first) front-facing camera:

    int getFrontCameraId() {
        CameraInfo ci = new CameraInfo();
        for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
            Camera.getCameraInfo(i, ci);
            if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
        }
        return -1; // No front-facing camera found
    }
    

    you can then use the index for the Camera.open method, e.g.:

    int index = getFrontCameraId();
    if (index == -1) error();
    Camera c = Camera.open(index);
    

    To get the relevant camera.

提交回复
热议问题