Ankit Kumar
Ankit Kumar Programmer, Technologist, Technical Writer, YouTuber and Tutor

Getting Started with CameraX API

Getting Started with CameraX API

CameraX, launched at Google IO 19 , is a Jetpack support library, built to help us make camera app development easier. It provides a consistent and easy-to-use API surface that works across most Android devices, with backward-compatibility to Android 5.0 (API level 21).

Why CameraX is beneficial?

As per the documentation, CameraX has following primary benefits :

  1. Easy to use
  2. Consistence across the devices
  3. It provide new camera experience to the user

Requirements

Requirements to use CameraX are :

  • Android Studio 3.3 or later
  • A device running on Android L or later

Let’s get hands on app code

Create Android Project with minimum APL level 21.

1
2
3
4
5
6
    defaultConfig {
        applicationId "com.ankkumar.cameraxsampleapp"
        minSdkVersion 21
        targetSdkVersion 28
        ......
    }

Add dependencies in app

1
2
    implementation "androidx.camera:camera-core:1.0.0-alpha01"
    implementation "androidx.camera:camera-camera2:1.0.0-alpha01"

Request Camera Permissions

In the manifest file

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

Handoling runtime permission in kotlin

1
2
3
4
5
6
7
8
    import android.Manifest
    import android.util.Size
    import android.graphics.Matrix
    import java.util.concurrent.TimeUnit

    private const val REQUEST_CODE_PERMISSIONS = 881

    private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)

actual code of permission handling look like below


Time to create UI

  • Create and Implement view finder
  • Implementation of view finder

    • Declare lateinit

      1
      
          private lateinit var viewFinder: TextureView
      
    • define it inside onCreate(..) method
      1
      
         viewFinder = findViewById(R.id.finder_view)
      
    • and implement it as below

Implementing Image Capture

  • Write below code to capture image

  • Place below code between CameraX.bindToLifecycle(this, preview) and CameraX.bindToLifecycle(this, preview, imageCapture)

Analysing captured image

  • create a class called ImageAnalyzer as below

    1
    2
    3
    4
    5
    
      class ImageAnalyzer : ImageAnalysis.Analyzer {
          override fun analyze(image: ImageProxy?, rotationDegrees: Int) {
              TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
          }
      }
    
  • And implement above class for analyzing image
  • Now add image analysis code inside startCamera() method as below
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
       // Setup image analysis pipeline that computes average pixel luminance
      val analyzerConfig = ImageAnalysisConfig.Builder().apply {
          // Use a worker thread for image analysis to prevent glitches
          val analyzerThread = HandlerThread(
                "LuminosityAnalysis"
          ).apply { start() }
          setCallbackHandler(Handler(analyzerThread.looper))
          // In our analysis, we care more about the latest image than
          // analyzing *every* image
          setImageReaderMode(
              ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE
          )
      }.build()
    
  • After adding this snippet modify CameraX.bindToLifecycle() method with this argument
  • Now our complete startCamera() will look like this

Testing app

And it works perfect !!! Congratulations !!!

Rating: