Jetpack-CameraX-Android

Share on facebook
Share on google
Share on twitter
Share on linkedin

Jetpack-CameraX is simplest way to click and pick image.

What this blog provide….

I’ll show you how to use JetPack-CameraX library.

Let’s start

JetPack-CameraX helps you add reliable camera to your app quickly. It use cameraX library inside.Simple implementation.

Implement camera in app is so common today, i.e. especially provide use to click and upload his profile image. However writing your code to handle all this is a tedious job. So JetPack-CameraX helps you with all the hard job. You just need first add the library to your app.gradle file.

implementation 'com.github.SaeculumSolutions:Jetpack-CameraX-Android:1.0.0'

And also make sure you in your project.gradle file you add below dependency

allprojects {
 repositories {
  ...
  maven { url 'https://jitpack.io' }
 }
}

After that, in your code(perhaps MainActivity class), just initialise intent

companion object {
		private const val TAG = "MainActivity"
		const val LAUNCH_CAMERA_X_ACTIVITY = 131  
}


override fun onCreate(savedInstanceState: Bundle?) {
...
		val intent = JetPackCameraX(this).build()
		/*
     		*  if you want open front camera as default use this
	     	*  val intent = JetPackCameraX(this).setCameraLensFacingFront(true).build()
     		*/
		startActivityForResult(intent, LAUNCH_CAMERA_X_ACTIVITY)

}

Then override onActivityResult function to get JetPackCameraX result.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if (resultCode == Activity.RESULT_OK && data != null) {
                when (requestCode) {
                    LAUNCH_CAMERA_X_ACTIVITY -> {
                    if (data != null) {
                        val path: String = data.extras?.get(CameraXImage.GET_CLICKED_IMAGE_URI).toString()
                        Log.d(TAG, "onActivityResult: $path" )
                        // Do needful with your selectedMedia
                       }
                    }
                }
            }
        }

Library used for make this library,

  • Glide
  • androidx.camera:camera-core:1.0.0-rc01
  • androidx.camera:camera-camera2:1.0.0-rc01
  • androidx.camera:camera-lifecycle:1.0.0-rc01
  • androidx.camera:camera-view:1.0.0-alpha20

Why I use CameraX library:
According Android CameraX official document, CameraX is Jetpack support library, 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). While CameraX leverages the capabilities of camera2, it uses a simpler approach that is lifecycle-aware and is based on use cases. It also resolves device compatibility issues for you so that you don’t have to include device-specific code in your code base.

Lastly, CameraX enables developers to leverage the same camera experiences and features that preinstalled camera apps provide, with as little as two lines of code. CameraX Extensions are optional add-ons that enable you to add effects on supported devices. These effects include Portrait, HDR, Night, and Beauty.

for More details visit Github Link.

Happy Coding 🙂

Jignesh Jagad

Jignesh Jagad

Scroll to Top