Image for post
Image for post

Android’s CameraX Jetpack Library is now in Beta!

Oscar Wahltinez
Mar 16, 2020 · 4 min read

We are very excited to share with everyone that CameraX is now available as Beta! We want to thank our entire developer community who made this possible. With this release you will find a camera API foundation that will remain unchanged and get better with time via bug and device compatibility fixes.

In addition to expanded testing, we have many apps using CameraX in production giving us the confidence to recommend developers to start experimenting and (if suitable) rolling out CameraX in production on the Play Store. We do expect to have some bugs and are committed to ensuring that your apps have the best possible camera experience. Should you need help please reach out to us for support via either the discussion mailing list or opening an issue.

In this blog post, we will provide a brief overview of CameraX Jetpack Library and share what’s new in Beta. Reaching beta status means that a Jetpack library is considered to be ready for production use, but may still contain some non-critical bugs.

For up-to-date resources, check out the documentation, review the official sample, and join our online developer community.

Image for post
Image for post
CameraX logo

CameraX recap

Device compatibility

Lifecycle awareness

CameraX use cases

  • Preview: Used to display a viewfinder of what the camera is pointing at.
  • ImageAnalysis: Used to parse information from the camera stream.
  • ImageCapture: Used to capture a high-quality photo.
Image for post
Image for post
Portrait mode

What has changed in Beta?

Initialization

val cameraProviderFuture: ListenableFuture<ProcessCameraProvider> =
ProcessCameraProvider.getInstance(context)
// The listener will be triggered once the future’s value is ready
cameraProviderFuture.addListener(Runnable {
// Camera provider is now guaranteed to be available
val cameraProvider = cameraProviderFuture.get()

}, executor)

Picking a camera

val cameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()

If you want a simple selector to pick either front or back facing, you can use the static fields CameraSelector.DEFAULT_BACK_CAMERA or CameraSelector.DEFAULT_FRONT_CAMERA.

Camera preview

<androidx.camera.view.PreviewView
android:layout_width=”match_parent”
android:layout_height=”match_parent”/>

Then make sure that your Preview use case is using the surface from PreviewView:

val preview: Preview = …
val viewFinder: PreviewView = … // for example findViewById()
preview.setSurfaceProvider(viewFinder.previewSurfaceProvider)

If you want to use your own surface (from TextureView, for example), then you must implement your own surface provider as well as making sure that you handle sizing and orientation appropriately, which can be tricky.

Camera controls

val camera = cameraProvider.bindToLifecycle(…)
val zoomState: LiveData<ZoomState> = camera.cameraInfo.zoomState
val torchState: LiveData<Int> = camera.cameraInfo.torchState

Conversely, you can control those values with the corresponding CameraControl object:

val camera = cameraProvider.bindToLifecycle(…)
camera.cameraControl.enableTorch(true)
camera.cameraControl.setLinearZoom(0.5f)
camera.cameraControl.startFocusAndMetering(…)

What’s next?

To learn more about CameraX, check out the documentation and the official sample, or join our online developer community. Stay tuned for future blog posts and updates!

Android Developers

The official Android Developers publication on Medium

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store