Jetpack-CameraX-Android

Jetpack-CameraX-Android is the simplest way to click and pick an image. CameraX is a Jetpack support library that was designed to assist you in making the development of camera apps more simple. It offers a uniform and user-friendly API interface that can be used on all Android devices.

It follows a simple approach, using a use case-based model that is lifecycle aware. These features can reduce the number of lines you have to write to add camera features to your app.

How to use JetPack-CameraX-Android?

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

Conclusion

Jetpack-CameraX-Android 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 & read more on DesignRush.

Happy Coding ????