CameraX — sneak peek into an upcoming Jetpack support library
Exploring the new CameraX library in Android
- Overview
- Integration
- Preview
- Image Analysis
- Image Analysis with ML kit
- Image Capture
- Extensions
- Useful links
Overview
CameraX is a Jetpack support library which is in Alpha at present. CameraX library is build to simplify the usage of camera features in app development. CameraX provides an in-depth analysis of what camera is pointed at through Image Analysis and also provides a way to use built-in device camera features like HDR, portrait mode and so on through extensions.
There is a concept called use-case in CameraX which enhances developer interaction by using specific use-case instead of focusing on managing all the device-specific nuances.
There are three use-cases to get-started, they are
- Preview: get an image on the display
- Image analysis: access a buffer seamlessly for use in your algorithms, such as to pass into MLKit
- Image capture: save high-quality images
CameraX provides consistent behavior with camera interface for developers no matter which OS or device you’re working
That being said CameraX is in Alpha, please note that it is not good to use in your production app yet.
Integration
CameraX library is backward compatibility to Android L, that being said add the following lines to your build.gradle file & you’re good to go
Preview
The preview use-case offers a TextureView
where the camera output is streamed. Let’s see how to create a TextureView
in our layout
Here we use a textureview with 600 * 800 resolution, It also provides additional information for a view to crop, scale, or rotate for proper display. Now let’s see the coding part
Image Analysis
The image analysis use-case provides a virtually accessible image to operate image processing, apply machine learning and many more image analyzing techniques. Let’s see the coding part for image analysis use case
CameraX with ML kit
If you aren’t familiar with ML kit from Firebase, go through the following article
That being said, with ImageAnalysis.Analyzer an ImageProxy is obtained through which we can get the image and use that virtual image in ML kit as shown below
Image Capture
Through image capture, you can save high-resolution images with very minimal code have a look
Here is the use-case configuration to take a photo
as we set the use case now we can see the preview of the camera, it’s time to take a photo
let’s see how it can be done
Extensions
CameraX provides an API for optional effects like HDR, portrait, and others implemented by the manufacturer for specific phones. For a device to support vendor extensions, all of the following must be true:
- The effect has library support from the device OEM.
- The OEM library is installed on the current device.
- The OEM library reports the device as supporting the extension.
- The device has a version of the operating system that the library requires.
To apply vendor extensions to CameraX use cases, create an Extender
object, which allows you to configure the Builder
with the settings for that effect or function. Query the extension for availability, because if an extension is unavailable the enableExtension()
call will not do anything.
To disable vendor extensions we have to create the new image capture use case instance.
Useful links
CameraX is still in Alpha iterations there might be changes in the code in future releases so I suggest you do not use CameraX in your production apps yet.
You can find me on Medium, Twitter and LinkedIn Cheers!
Thank you for reading.