CameraX: Jetpack Support Library

Get to grips with the newly released feature.
David Franquet
David Franquet
June 27, 2019

At the Google I/O 2019 keynote in May, Google announced several brand new features for Android developers to experiment with, from Jetpack Compose to CameraX. Let’s take a look at CameraX in more detail!

  • What is it?
  • What do we need to start using it?
  • How can it improve the Android developer experience?

How many times have you taken a photo with your native camera app, as opposed to the in-app camera functions in apps like Instagram, Twitter and Facebook, before uploading it to your platform of choice?

Until now, in-app camera functions have yet to match the quality afforded by the native camera and the development process has been always been complex. The introduction of CameraX, however, seeks to solve those issues!

What is Camera X?

CameraX is a Google Jetpack library that makes the camera API development process so much easier and more consistent! How? By providing developers with an easy-to-use API surface that works on most Android devices, with support on Android 5.0 (API Level 21) and above.

Interestingly, CameraX also has an add-on called Extensions that gives you access to the same properties and functionalities as your native camera app - such as HDR, Portrait Mode and Night Mode - and it only requires two extra lines of code to implement into your supported applications.

What do we need to start using it?

We need some dependencies so that we can start experimenting with Google’s libraries. In this case, we will need to add Google’s Maven repository and two AndroidX dependencies into the mix.

To get started, all you need to do is open your build.gradle file and add the google() repository...

<p> CODE: "https://gist.github.com/DavidFD005/3503dac1abd46d6e798662be9f17522c.js"</p>

...and the following dependencies:

<p> CODE: "https://gist.github.com/DavidFD005/5a8f30654a3e73688614d9c2342ab762.js"</p>

Simple!

How can CameraX improve the developer experience?

CameraX is a use case based API and it has abstracted 3 main handles which you can use to interact with the camera: Preview, Image Analysis and Image Capture.

The case you use depends on what you’re using the camera for.

Preview: Allows you to use the camera preview stream.

Image analysis: Allows you to process camera data.

Image capture: Allows you to capture and save a photo.

Preview

The preview use case produces a SurfaceTexture which then streams the camera input. It also provides additional information for a View to crop, scale, or rotate for accurate display.

The image preview is streamed to the SurfaceTexture when the camera becomes active. The SurfaceTexture can be connected to a TextureView or a GLSurfaceView.

We can implement the PreviewOutput with the following piece of code:

<p> CODE: "https://gist.github.com/DavidFD005/0394b8ec4d4a14b6e16767df0dae820c.js"</p>

Image analysis

The image analysis use case provides your app with a CPU-accessible image to perform image processing, computer vision, or machine learning inference on. The application implements an Analyzer method that is run on each frame.

The following example shows how to do this.

<p> CODE: "https://gist.github.com/DavidFD005/64d7855725130467bc999d469f6c62cb.js"</p>

image credit: developer.android.com

Image capture

The image capture is designed for capturing high-resolution, high-quality photos and provides auto-white-balance, auto-exposure, and auto-focus functionality, in addition to simple manual camera controls.

The caller is responsible for deciding how to use the captured picture, including the following options:

  • takePicture(OnImageCapturedListener): This method provides an in-memory buffer of the captured image.
  • takePicture(File, OnImageSavedListener): This method saves the captured image to the provided file location.
  • takePicture(File, OnImageSavedListener, Metadata): This method enables you to specify the metadata to embed in the saved file's Exif.

The following code demonstrates how you can then configure your app to take a photo:

<p> CODE: "https://gist.github.com/DavidFD005/5219af123dc2c46456a1bf76bc1270a1.js"</p>

<p> CODE: "https://gist.github.com/DavidFD005/cef5a109c91fd407f67f696b8258e955.js"</p>

In conclusion…

CameraX seeks to align the demand that’s put on smartphones to take world-class photographs with the very applications/social media platforms that the user will inevitably upload the photos onto.

If said social media platforms, such as Facebook and Instagram, implement this powerful tool within their apps, the user experience should greatly improve and thus avoid the risk of users heading to their native camera apps to capture snapshots.

Users aren’t the only people who will benefit from CameraX, of course, as its ease-of-use means that developers can now focus their energy on building around the function as opposed to building within it.

Check out the Google I/O keynote on CameraX for more information!

---

What are your thoughts on CameraX? Tweet us and we’ll be sure to retweet the responses!

We Are Mobile First is a digital product agency based in Barcelona helping to transform businesses in a mobile-first world. Follow us on Twitter, LinkedIn and Medium to be notified of our future posts and stay up-to-date with our company news.

We share weekly content on everything from Swift and the type safety feature to animated headers for Android with MotionLayout.

(Hero image credit: Tech Planet)