Android CameraX Java Example

In the world of Android, I have felt Camera/Camera2 API is one of the complicated implementation to make it work at our convenience.

First it was Camera API, then it was deprecated as Camera2 API came into picture. Whereas there is no much less in difficulty working with the same.

Finally for us, Android Jetpack brought us CameraX API with which the implementation and working with Android Camera can be done easily and very less possible lines of code. CameraX is built on top of the Camera2 API. In this article, we have a simple tutorial of the implementation of CameraX API with JAVA.

As I have browsed through many blogs and tutorials, there are no tutorial of implementing CameraX with Java mainly according to the latest documentation (1.0.0-beta03). That’s the main reason for this article.

Why late, Let’s jump into the code:

Implementing CameraX is a simple 5-step process:
1. Adding Permission in Manifest
2. Adding required Dependencies
3. Adding Camera View and Capture Button in XML
4. Adding Camera M-permission (Realtime permission)
5. Setting up CameraX in Activity

Step 1: Adding Permission in Manifest

First thing to be done is adding the Camera permission to our application manifest

<uses-permission android:name=”android.permission.CAMERA” />

Add Storage permission along with the camera permission as we are saving the captured picture in this example

Step 2: Adding required dependencies in build.gradle (App level)

Right now CameraX is in beta version — 1.0.0-beta03

Step 3: Adding Camera View (PreviewView) and Capture Button in XML

PreviewView ( has been introduced by Android team in CameraX. As previously in Camera/Camera2 we used TextureView or customized TextureView (AutoFitTextureView).

As per documentation, PreviewView have some limitations:
Using PreviewView has some limitations.

When using PreviewView, you can't do any of the following things:

  • Create a SurfaceTexture to set on TextureView and PreviewSurfaceProvider.
  • Retrieve the SurfaceTexture from TextureView and set it on PreviewSurfaceProvider.
  • Get the Surface from SurfaceView and set it on PreviewSurfaceProvider.

If any of these happen, then the Preview will stop streaming frames to the PreviewView.

Step 4: Adding Camera M-permission (Realtime permission)

Below is a method to request Camera and Storage permission, which will be used in onCreate method of the Activity.

Step 5: Setting up CameraX in Activity

As per documentation:
Developers use CameraX to interface with a device’s camera through an abstraction called a use case. The following use cases are currently available:

  • Preview: accepts a surface for displaying a preview, such as a PreviewView.
  • Image analysis: provides CPU-accessible buffers for analysis, such as for machine learning.
  • Image capture: captures and saves a photo.

Use cases can be combined and active concurrently. For example, an app can let the user view the image that the camera sees using a preview use case, have an image analysis use case that determines whether the people in the photo are smiling, and include an image capture use case to take a picture once they are.

In simple words, CameraX currently have three Classes/Use Cases namely Preview, Image Analysis and Image Capture. In these, Preview and Image Capture are mandatory things to be associated whereas Image Analysis is optional.

Creating and Associating the object of these classed/use cases are shown in below code snippet.

So the CameraX Code is as follows.

The deep explanation on each statement used is available in Official Documentation.

Finally, add this code in your onCreate along with the PreviewView and Capture ImageView declaration and initialization:

Final Code:

Image Credit: Google Images

That’s all about the simple tutorial on implementation of CameraX.
If you have any thoughts or questions that you’d like to share, then please do reach out!

Check out this post in my blog:

Here is the Github repository link of the functional app. Check it out: