Skip to content

CameraX

Jetpack provided with cameraX to provide global use for camera usability. This codebase provide CameraXHandler to support cameraX implementation with ease. First, design layout with PreviewView like this:

<androidx.constraintlayout.widget.ConstraintLayout>
    <androidx.camera.view.PreviewView android:id="@+id/pvCamera"
     />
</androidx.constraintlayout.widget.ConstraintLayout>

Then initialize CameraXHandler like this:

binding.pvCamera.post { handler = CameraXHandler(context, binding.PvCamera) }

By doing it, the handler will automatically configure cameraX to use basic feature. And by default, the preview use case is embedded to the camera. There are two other use cases provided by the codebase, which is:

QRScanner

This codebase use ML Kit to scan QR that detected through cameraX, initialize it like this:

// success if the QR is read
// message will return the content if succeeded and errorMessage if not
val scanner = QRScanner(handler) { success, message ->
    if (success) {
        cameraProvider.unbindAll()
        displayDialog(this, "Qr Scanned", message, Pair("close") {
            finishActivity()
        })
    } else
        displayToast(this, message)
}

to start scanning, call this method:

scanner.startScan()

StoreImage

For saving image using cameraX, initialize StoreImage like this:

val imageCapture = StoreImage(handler)

Then call this method to save the image:

imageCapture.saveImage(toStoreFile) { success, message ->
    if (success)
        displayToast(requireContext(), "image saved!")
    else
    debug { message }
}

Demo

Click here