Start a Project

Custom Camera Using CameraX

In the Android phone, the best feature is a camera for capturing the picture and videos.  And in the apps, we can take pictures and videos just by using default mobile camera app but sometime in the app, we need to add a camera functionality inside our app.

In Google IO 2019, Google added another powerful tool for camera development in Android development called CameraX as part of Jetpack.

CameraX 

CameraX is an addition to Jetpack that makes it easier to leverage the capabilities of Camera2 APIs. This topic covers the architecture of CameraX, including its structure, how to work with the API, how to work with Lifecycles, and how to combine use cases.

Steps to implement cameraX 

1. Add the dependency in build.gradle file

2. We are using TextureView tag in order to display camera in a view. Please review the xml file here:-

3. Now, we have to add Camera permission in AndroidManifest file

And we also need to take runtime permission for all android version 6 and above.

4. Now we have to add the following code in the CameraActivity.kt file.

  1. CameraX.bindToLifecycle() binds to the lifecycle of the view.
  2. PreviewConfig.Builder() is the builder class for PreviewConfig(which helps to see the Preview of Image) where you can set different type of configuration like lens facing sidetarget resolutionAspect ratio with setLensFacing(value) where value = CameraX.LensFacing.BACK/FRONT etc.
  3. ImageCaptureConfiguration.Builder() is the configuration for the image when the image is captured.
  4. rotationDegrees Using this, we can rotate of the Preview and finally, texture sets the transformation using texture.setTransform(matrix)and matrix rotates the preview based on rotationDegress.
  5. when a button, button_take_picture is clicked, we have to click and image and store it in out Local storage.
  6. in the Click listeners, we take images using takePicture and it implements two methods onError() and onImageSaved().

 

Benefits:-

  1. You can also create Video Recorder App using CameraX
  2. You can also implement Image Analysis using CameraX
  3. Add multiple extensions like Portrait Mode, HDR, etc.
  4. We can also use Image Analysis to perform Computer Vision, ML. So it implements Analyzer method to run on each and every frame.

You can also review the more details about cameraX and its feature and enjoy in making more interesting functionality like dubsmash, edit picture in your android app. Please refer the following the link for more details:-

https://codelabs.developers.google.com/codelabs/camerax-getting-started/#0

https://developer.android.com/training/camerax/architecture

 

 

 

Exit mobile version