MMS • Sergio De Simone
Article originally posted on InfoQ. Visit InfoQ
Now available in beta, CameraX 1.2 brings out-of-the-box integration with some of MLKit vision APIs and a new feature aimed to reduce shutter button lag when taking pictures.
To make it easier for developers to use an Android camera with MLKit, Camera 1.2 introduces MlKitAnalyzer
to handle much of the required setup. MLKitAnalyzer
is a wrapper around MLKit detectors which forwards them all the camera frames so they can be processed. At the end of the analysis, it will invoke Consumer.accept(T)
to send on the aggregated results.
The following snippet shows how you can use MLKitAnalyzer
for barcode scanning:
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.build()
val barcodeScanner = BarcodeScanning.getClient(options)
cameraController.setImageAnalysisAnalyzer(executor,
new MlKitAnalyzer(List.of(barcodeScanner), COORDINATE_SYSTEM_VIEW_REFERENCED,
executor, result -> {
...
});
Besides barcode scanning, CameraX 1.2 supports face detection, text detection, and object detection.
Another new feature in CameraX 1.2 aims to reduce the delay between pressing the shutter button and the actual frame being captured. This feature, dubbed zero-shutter lag, does not actually reduce the intrinsic latency of the device, rather it uses a circular buffer to store the most recent frames captured by the camera. Then, when the user presses the shutter button, it picks the buffered frame with the closest timestamp to the moment when takePicture(OutputFileOptions, Executor, OnImageSavedCallback)
is invoked.
Zero-shutter lag is enabled using CAPTURE_MODE_ZERO_SHOT_LAG
with ImageCapture.Builder.setCaptureMode()
. This feature may not work on all devices, since it requires more memory to store the circular photo buffer. The CameraInfo.isZslSupported()
API can be used to query device capabilities. Additionally, zero-shutter lag cannot be used when capturing video or with vendor extensions implementing special effects.
As a last note, CameraX 1.2 adds a new API to set location metadata for saved videos and includes a number of bug fixes, such as incorrect Exif metadata, crashing when recording video with not microphone available, and others.
CameraX is part of Android Jetpack, a suite of libraries aiming to simplify the creation of Android apps by adopting best practices, reducing boilerplate, and writing code working across Android versions and devices.