Search code examples
androidandroid-camera2android-camerax

android camera 2 must use only camera devices supporting backward compatibility


I am building an android application that lists down all camera devices, I want to list down all the camera devices and allow the user to use them all and not only that, I want also to allow the user to change the play with the resolution as they want, so I follow this link: https://developer.android.com/training/camera2/camera-enumeration

It recommends only using the camera devices with the flag:

// check if the selected camera device supports basic features
// ensures backward compatibility with the original Camera API
val isBackwardCompatible = cameraCapabilities?.contains(
    CameraMetadata.REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) ?: false

this skips lots of lenses, for example, google pixel 6 which I am sure uses two physical cameras on the rear size it can only use one lens if I apply this flag, the issue if I skip using this is that the app crashes when I try to use the method

cameraConfiguration.getOutputSizes(MediaCodec::class.java)

and it gives me this error once I call it on camera devices with no "REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE"

Any android device I get my hands on and try my app on, I find it at least missing one lens(if not more) and those lenses I am sure can be used for recording video(I turned on the camera app and covered them one by one while changing the modes)

Any help would be appreciated.

To explain how to get this NullPointerException

val characteristics = cameraManager.getCameraCharacteristics(id)
val cameraConfig =
            characteristics.get(SCALER_STREAM_CONFIGURATION_MAP)!!

//outputSizes is null if the cameraDevice is not backward compatible
val outputSizes = cameraConfig.getOutputSizes(ImageFormat.JPEG)

Solution

  • BACKWARD_COMPATIBLE devices are ones that support YUV and JPEG output and a bunch of basic camera behavior.

    In general, only very few camera types won't list BACKWARD_COMPATIBLE; one such example is a pure depth camera, which won't produce JPEGs. For such devices, you have to manually check what output formats are actually supported via 'getOutputFormats', since it's likely something like JPEG won't be listed, or it may only support monochrome output and not color, which may make it impossible to use it with a video recorder.

    If you're seeing a lot of devices get filtered out by excluding BACKWARD_COMPATIBLE, it'd be interesting to know, since in my experience they're very rare.