Android to determine whether there is a front facing camera and a rear facing camera

  • 2020-06-12 10:35:57
  • OfStack

In general, we conduct camera operations, such as scanning 2-dimensional codes, and need to determine whether there is a rear camera (Rear camera). For example, Nexus 7 1 generation does not have a rear camera. In this way, when trying to use it, we need to make a judgment and carry out some prompts or processing.

The following code is a series 1 method to determine if there is a front camera (Front Camera) and a rear camera.


private static boolean checkCameraFacing(final int facing) {
    if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
        return false;
    }
    final int cameraCount = Camera.getNumberOfCameras();
    CameraInfo info = new CameraInfo();
    for (int i = 0; i < cameraCount; i++) {
        Camera.getCameraInfo(i, info);
        if (facing == info.facing) {
            return true;
        }
    }
    return false;
} public static boolean hasBackFacingCamera() {
    final int CAMERA_FACING_BACK = 0;
    return checkCameraFacing(CAMERA_FACING_BACK);
} public static boolean hasFrontFacingCamera() {
    final int CAMERA_FACING_BACK = 1;
    return checkCameraFacing(CAMERA_FACING_BACK);
} public static int getSdkVersion() {
    return android.os.Build.VERSION.SDK_INT;
}

Note: Since getNumberOfCameras and getCameraInfo are both introduced as API 9, the method is only applicable to 2.3 and above.

Read on: http: / / developer android. com/reference/android/hardware/Camera html http: / / developer android. com/reference/android/hardware/Camera CameraInfo. html


Related articles: