CameraX: A Simpler Approach to Camera Application Development

26 / Dec / 2021 by Anupam Bhardwaj 0 comments

CameraX is another addition to the Jetpack suite of libraries by Google to help developers incorporate camera capabilities into their applications. Although Camera2 API, the predecessor of CameraX, is known to be a powerful API, it is a little problematic to take full advantage of it, especially with a large number of manufacturers having a diverse set of camera capabilities for their devices. Camera2 is also complex and inconsistent.

CameraX Library resolves these issues by having a much simpler lifecycle-aware approach that are primarily based on the use cases while taking maximum advantage of the features provided by the Camera2 API. CameraX also addresses the device compatibility issues, which reduces the pain of writing device specific code to your source code. It also facilitates the developers to provide a similar experience and features that the pre-installed camera apps provide to the users with minimal lines of code.

In order to make it easier for the developers to add camera compatibility to their apps, the CameraX focuses on the following essential attributes:

  • More features such as tap-to-focus, pinch to zoom and device rotation are now enabled easily with the addition of new capabilities and API in CameraX
  • Support for additional camera features like, night mode or HDR by expanding the range for devices that provide extension functions
  • Testability, with a lab of 52 various devices that primarily focuses on the stability and consistency of the API

Key Benefits

The major focus of CameraX is to enhance the development experience, and it does that by addressing the most familiar camera scenarios with the 3 use cases:

  1. Preview allows you to include a PreviewView making it much simpler to view live camera feed in your app when the camera becomes active
  2. Image Analysis allows you to acquire camera frame data to pass it onto the image processing, computer vision or other machine learning algorithms
  3. Image Capture allows you to easily capture photos along with simple manual camera controls

CameraX also provides consistency across a large number of devices. This is done by accounting a number of behaviors such as aspect ratio, orientation, rotation, preview size, and high-resolution image size.

Google has also invested in an automated CameraX testing lab with a large number of different devices from various manufacturers and all the Android OS flavors since API level 21 (Android 5.0). This automated lab tests a variety of camera behaviors continuously on all these devices in order to find and fix any issue with the CameraX library thus reducing the test burden from the developers.

CameraX also comes with optional add-ons for supported devices, allowing you to enable similar features and capabilities as that of a native camera app on the newer devices that come with the enhanced camera lenses with just a minimal line of code. These add-ons are called Extensions, and include Portrait mode (Bokeh), HDR, Night Mode and Face retouch.

Another key benefit of using CameraX is the reduced size of the APKs with highly optimized performance and less maintenance of camera code.

Implementing CameraX

Adding CameraX functionality in our app is fairly simple, and just requires three basic steps: configuration, binding and action, to implement any of the 3 above mentioned use cases.

The following code snippet shows how you can implement the image capture use case in your app with ease:


Second step is to bind the use case to a lifecycle, You can bind the use case to different lifecycles, whether it is an activities lifecycle, camera lifecycle or capture session. Once a use case is binded to a lifecycle CameraX automatically handles the camera opening and releasing based on the lifecycle of the lifecycle owner it’s bound to.The first step is to configure the use case, i.e. to create an image capture use case for the above example. You can also specify various parameters such as image resolution, aspect ratio, flash mode etc. while defining the use case. If you have specified a particular parameter such as resolution and the device running your app does not support the given resolution then CameraX will automatically switch to the nearest resolution, hence supporting every configuration.

The last step is to act on the use case. In the above example, the takePicture() method call will take a picture. And here you have an image capture use case ready with just 4 lines of code.

CameraX Extensions

One of the most useful features in the CameraX library are the CameraX Vendor Extensions. These extensions are optional add-ons provided in the library along with the basic functionality and camera controls. In the last few years, the mobile camera hardware has moved from basic single lens camera systems to highly advanced professional grade multi lens camera systems with numerous features like portrait mode(Bokeh), Night mode, Beauty mode, HDR and much more. But with all this high end hardware and multiple lenses these features were only accessible through the device’s built-in native camera applications thus creating a huge gap between the hardware and software in order to make all these features accessible in third party applications by the developers and taking advantage of the camera hardware to the fullest.

CameraX reduces this gap with the help of its extensions by making 5 of the manufacturer specific features available to the developers which include:

  • High Dynamic Range(HDR) developers can now use the multi-frame processing capability to capture an image with both highlights and lowlights in the scene
  • Night developers can use the low light multi-frame processing capabilities to provide more appealing pictures in low-light conditions
  • Auto automatically determines and provides either an HDR or a Night image based on current ambient lighting conditions
  • Portrait(Bokeh) provide a portrait mode image with blurred background.
  • Beauty enables face beauty effects within the image

Although the extensions are available on the devices that support these features natively, it does not mean that you have to write device specific code blocks. You just have to code it once and these extensions will work on all supported devices. You can configure these extensions for both the preview and image capture use cases in your app which will improve the image quality and the user experience.

Conclusion

Leveraging the advantages of Camera2 API, CameraX provides a fast, simple and lifecycle aware approach towards the implementation of camera capabilities in our application. With Camera2 and its predecessors the developers have to start with device and OS specific inputs to receive desired output which in turn results in a large if-else blocks making it very difficult to scale the code base with the entry of new devices in the market. CameraX over turns the entire camera framework programming model and takes a use case based approach. With this new use case based approach in CameraX, the developers have to specify the use case they want to implement and then CameraX will automatically determine the required device and OS specific configuration. CameraX also enables the developers to take advantage of the new professional grade camera hardware in their applications with the help of Extensions.

CameraX also helps the developers by solving the difficulties they have in scaling the testing of their applications to a large number of devices and various camera configurations. The dedicated CameraX Test Lab ensures the test coverage for multiple devices from different manufacturers, different OS levels, price tier and chip sets. All these devices running tests for various configurations continuously represent ~500M Android devices, which helps in continuously improving the CameraX library.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *