Search code examples
androidxandroid-camerax

CameraX ProcessCameraProvider vs CameraController - which one to use when?


Can someone please clarify, when I should use androidx.camera.view.CameraController and when androidx.camera.lifecycle.ProcessCameraProvider? Both seems to be able to start/stop camera using lifecycle events, handle different uses cases (preview, taking pictures, image analysis). Both also work with androidx.camera.lifecycle.PreviewView.


Solution

  • ProcessCameraProvider is part of CameraX's lifecycle artifact. It provides access to some/all the device's cameras, allows attaching the lifecycle of the camera (when it opens/closes) to a lifecycleOwner, and has a scope of the app's process (it's a Singleton). In terms of how it's used, it's sort of a low-level API in CameraX, in the sense that when you use it, you'll need to initialize it, create and configure your use cases (i.e Preview, ImageAnalysis, ImageCapture), bind them to a LifecycleOwner and then interact with the camera if you'd like to, by getting access to the bound camera's CameraInfo and CameraControl instances. And yes, you can use PreviewView in this scenario, though you'll have to manually attach it to the Preview use case.

    CameraController is part of CameraX's view artifact, which builds on top of the core APIs CameraX offers to provide a simple API that can easily be plugged into an app and be ready to use. CameraX's view artifact mainly provides PreviewView and CameraController, the latter handling all the camera setup you'd otherwise need to manually do if you had used APIs like ProcessCameraProvider. Besides, CameraController handles device rotation (to make sure its use cases receive frames from the camera in the correct orientation), and provides a WYSIWYG experience, ensuring that the output of its use cases match what PreviewView displays, and handles touch events (tap-to-focus, pinch-to-zoom) on its attached PreviewView. You can check out this article to see what it provides out of the box.

    Which API you choose to use depends on your usage needs, CameraController provides/handles many of CameraX's core features for you, but is less flexible in terms of use case configuration. So if you need more advanced control over the use cases (e.g setting their resolutions), you'll probably have to set up things manually using ProcessCameraProvider.