ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

CameraX — sneak peek into an upcoming Jetpack support library

Exploring the new CameraX library in Android

Siva Ganesh Kantamani
ProAndroidDev
Published in
3 min readOct 15, 2019

  1. Overview
  2. Integration
  3. Preview
  4. Image Analysis
  5. Image Analysis with ML kit
  6. Image Capture
  7. Extensions
  8. 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

dependencies in gradle file

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

textureview in 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

Camera preview code in kotlin file

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 image analysis

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

ML kit with CameraX

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

CameraX image capture use case

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

Saving captured image through CameraX

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.

Camearx extensions

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

The latest posts from Android Professionals and Google Developer Experts.

No responses yet

Write a response