Skip to content

Optimizing camera initialization with Camera Limiter#

Applies to LUNA ID for Android only.

To improve the performance of your app's camera features, you can optimize the camera initialization process using the Camera Limiter feature in CameraX . During the first invocation of ProcessCameraProvider.getInstance(), CameraX enumerates and queries the characteristics of all available cameras on the device. This process can be time-consuming, especially on low-end devices, as it involves communication with hardware components.

If your app only uses specific cameras (for example, the default front or back camera), you can configure CameraX to ignore unnecessary cameras. By limiting the available cameras, you can significantly reduce startup latency for the cameras your app uses.

Implementation#

To restrict CameraX to a specific camera, use the CameraSelector class with CameraXConfig.Builder.setAvailableCamerasLimiter(). For example, the following code limits the app to only use the device's default back camera:

class MainApplication : Application(), CameraXConfig.Provider {
    override fun getCameraXConfig(): CameraXConfig {
        return CameraXConfig.Builder.fromConfig(Camera2Config.defaultConfig())
            .setAvailableCamerasLimiter(CameraSelector.DEFAULT_BACK_CAMERA)
            .build()
    }
}