CameraX is a Jetpack support library that provides an API to control a device's camera. This library allows displaying a camera preview, capture images, perform image processing.
If we want to use CameraX, we need to add dependencies in the module's build.gradle
file. CameraX requires methods that are part of Java 8, so we need to add compile options.
android {
// Other configuration
// ...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// Other dependencies
// ...
implementation 'androidx.camera:camera-camera2:1.0.0-beta07'
implementation 'androidx.camera:camera-lifecycle:1.0.0-beta07'
implementation 'androidx.camera:camera-view:1.0.0-alpha14'
}
The application should be able to access the camera device. So, we need to request the CAMERA
permission in the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.CAMERA" />
<application>
...
</application>
</manifest>
In the layout XML file, we added the PreviewView
element which is used to display a camera preview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.camera.view.PreviewView
android:id="@+id/myPreviewView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Using the method ContextCompat.checkSelfPermission()
we check if required permission was granted. If not, then we request permission by calling the method ActivityCompat.requestPermissions()
.
We request an instance of the ProcessCameraProvider
that allows to bind the camera's lifecycle to the lifecycle owner. It manages camera's opening and closing. We build a Preview
and connect it to the PreviewView
. We select the default back-facing camera.
package com.example.app
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
private val permission: String = Manifest.permission.CAMERA
private val requestCode: Int = 1
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(this, arrayOf(permission), requestCode)
} else {
displayCameraPreview()
}
}
private fun displayCameraPreview()
{
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder().build()
preview.setSurfaceProvider(myPreviewView.createSurfaceProvider())
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
cameraProvider.bindToLifecycle(this, cameraSelector, preview)
}, ContextCompat.getMainExecutor(this))
}
}
Leave a Comment
Cancel reply