How to Create Custom Camera using CameraX in Android?

Rohit Garg
2 min readJun 12, 2024

--

Let’s start…

Add Dependency:

Add CameraX Dependency First. We need to add the CameraX dependency to our project. To do this, we need to add the following code to our build.gradle file:

def camerax_version = "1.2.1"
// CameraX core library using camera2 implementation
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:$camerax_version"

Set Up Permission:

Set Up Camera Permissions Next. Need to set up camera permissions in our application’s manifest file. We need to add the following code to our manifest file:

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

Check Permission in Activity file:

In our activity class first we will have to check camera permission. Through allpermissionsGranted() function we are requesting for Camera permission and onRequestPermissionsResult() if all permissions granted , then we will start Camera by startCamera() function.

private lateinit var mViewBinding: ActivityMainBinding
private val TAG = "CameraXBasic"
private var imageCapture: ImageCapture? = null
private val REQUEST_CODE_PERMISSIONS = 20
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mViewBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mViewBinding.root)
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
// checks the camera permission
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_PERMISSIONS) {
// If all permissions granted , then start Camera
if (allPermissionsGranted()) {
startCamera()
} else {
// If permissions are not granted,
// present a toast to notify the user that
// the permissions were not granted.
Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT)
.show()
finish()
}
}
}

Let’s take a close look at our startCamera() function.

  • cameraProvider is using to bind the lifecycle of cameras to the lifecycle owner.
  • preview is to set the preview in View layout element of camera preview.
  • Here we are selecting the back camera as a default camera by DEFAULT_BACK_CAMERA.
  • cameraProvider.bindToLifecycle binds use cases to camera.
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
    cameraProviderFuture.addListener(Runnable {        // Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(mViewBinding.viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build() // Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture
)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}

That’s it. Now we are good to work with CameraX preview.

--

--