Getting Started with CameraX

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).

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 to use CameraX are Android Studio 3.3 or later and A device running on Android L or later.

Let’s get hands on app sample.

defaultConfig {
applicationId "com.ankkumar.cameraxsampleapp"
minSdkVersion 21
targetSdkVersion 28
......
}
implementation "androidx.camera:camera-core:1.0.0-alpha01"
implementation "androidx.camera:camera-camera2:1.0.0-alpha01"

In the manifest file

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

In kotlin class for runtime permission handling

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)
  1. create view finder in activity_main.xml

2. Implementation of view finder

Declare lateinit

private lateinit var viewFinder: TextureView

define it inside onCreate(..) method

viewFinder = findViewById(R.id.finder_view)

And implement it as below

Write below code to capture image

Place this code between

// Every time the viewfinder is updated, recompute layout
preview.setOnPreviewOutputUpdateListener {

val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
parent.addView(viewFinder, 0)

viewFinder.surfaceTexture = it.surfaceTexture
updateTransform()
}

and

CameraX.bindToLifecycle(this, preview)

and then update bindToLifecycle with CameraX as below

CameraX.bindToLifecycle(this, preview, imageCapture)

After this modification smartCamera method will become

create a class called ImageAnalyzer as below

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

// 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.

And now test the app.

— — — — — — — — — — — — Congratulations — — — — — — — — —

Clap, share if you like it and follow for the updates.

credit for inspiration goes to documents here.

Polyglot Fullstack Developer