Now available in beta, CameraX 1.2 offers out-of-the-box integration with some of MLKit’s vision APIs and a new feature aimed at reducing shutter button lag when taking photos.
To make it easier for developers to use an Android camera with MLKit, Camera 1.2 introduces MlKitAnalyzer to handle much of the necessary configuration. MLKitAnalyzer is a wrapper around MLKit detectors that forwards all camera frames to them for processing. At the end of the parsing, it will call Consumer.accept(T) to send the aggregated results.
The following snippet shows how you can use MLKitAnalyzer to scan barcodes:
val options = BarcodeScannerOptions.Builder() .setBarcodeFormats(Barcode.FORMAT_QR_CODE) .build() val barcodeScanner = BarcodeScanning.getClient(options) cameraController.setImageAnalysisAnalyzer(executor, new MlKitAnalyzer), (BARCODERESULT, LIST > { . .. });
In addition to 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, called zero shutter lag, doesn’t actually reduce the device’s intrinsic latency, but instead 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 buffer frame with the closest timestamp to when takePicture(OutputFileOptions, Executor, OnImageSavedCallback) is called.
Zero shutter lag is enabled via CAPTURE_MODE_ZERO_SHOT_LAG with ImageCapture.Builder.setCaptureMode(). This feature may not work on all devices as it requires more memory to store the circular buffer of photos. The CameraInfo.isZslSupported() API can be used to query device capabilities. Also, zero shutter lag cannot be used when capturing video or with vendor extensions that implement special effects.
On a final note, CameraX 1.2 adds a new API for setting location metadata for saved videos and includes a number of bug fixes, including incorrect Exif metadata, errors when recording video without an available microphone, and others.
CameraX is part of Android Jetpack, a set of libraries that aim to simplify building Android apps by adopting best practices, reducing boilerplate, and writing code that works across versions and Android devices.