CameraX in Android
The camera is one of the most much-used components of a mobile devices. It is responsible for capturing optical images of the surrounding environment. If you want to take photos in your app, you have two options which are
- Invoking an implicit intent
Intent(MediaStore.ACTION_IMAGE_CAPTURE)
that redirects to the users device camera. - Using a
Camera API
to capture images.
In this article will be about option 2. I’ll talk about CameraX and how to implement it to your projects.
What is the CameraX ?
CameraX is a Jetpack support library that allows developers to easily control a device’s camera.
How to implement CameraX to projects ?
I’ll show you how to implement it step by step.
Heads of steps we’ll follow ;
- Implementation
- Setting layout
- Image Capture and Storage
- Switch the camera
Dependencies and Permissions
Add the following dependencies in the build.gradle(app) file. Cause we’ll use ViewBinding, we need to set viewBinding true in buildFeatures .
We also need to declare the following permissions in the AndroidManifest.xml file, above the <application> tag:
<uses-feature android:name="android.hardware.camera.any"/>
<uses-permission android:name="android.permission.CAMERA"/>
Camera Preview
Let’s start bu setting layout file named activity_main.xml. It includes three component which are ;
Preview → To display the camera.
Two AppCompatButton→ One for taking pictures and the other for changing the camera direction.
activity_main.xml looks like the following
And binds our views in MainActivity.kt
After that, add these attributes
private lateinit var cameraProviderFuture:ListenableFuture<ProcessCameraProvider>private lateinit var cameraSelector:CameraSelector
cameraProviderFuture → is used to bind the lifecycle of the camera to the lifecycle of the application.
cameraSelector → allows us to control camera direction (front or back).
Initialize these in onCreate() method
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
The codes that will activate the camera will be included in the startCamera() function.
Call this function from onCreate()
override fun onCreate(savedInstanceState: Bundle?) {
...
startCamera()
...
}
Now the camera’s ready to go, but something’s missing. It’s permission , user permission is required to use the camera. And we can handle it by adding this above onCreate() .
We need to launch cameraProviderResult in onCreate()
cameraProviderResult.launch(android.Manifest.permission.CAMERA)
Taking Photo
First, create the following attributes that allow us to capture images
private var imageCapture: ImageCapture? = null
private lateinit var imgCaptureExecutor: ExecutorService
Instantiate imgCaptureExecutor in onCreate()
imgCaptureExecutor = Executors.newSingleThreadExecutor()
Instantiate imageCapture cameraProviderFuture.addListener({ … }) in startCamera()
This is the final look of startCamera() method.
Create an function names takePhoto()
Switch Camera
In conclusion, setting switchCameraBtn like the following.
In this article, I briefly talked about CameraX and showed you how to use it in your project. I hope it helps.You can clap this article , if you like it and can stay tuned for articles like this.
Contact ;
- cobanalitalha@gmail.com
- github.com/carpodok
- linkedin.com/alitalhacoban