ML Kit Analyzer
Stay organized with collections
Save and categorize content based on your preferences.
Google’s ML Kit provides on-device machine learning Vision APIs for detecting
faces, scanning barcodes, labeling images, and more. ML Kit Analyzer makes it
easier to integrate ML Kit with your CameraX app.
ML Kit Analyzer is an implementation of the ImageAnalysis.Analyzer
interface. It overrides the default target resolution
(if needed) to optimize for ML Kit usage, handles the coordinate transformations,
and passes the frames to ML Kit, which returns the aggregated analysis results.
Implement ML Kit Analyzer
To implement ML Kit Analyzer, we recommend using the CameraController
class, which works with PreviewView
to display UI elements. When implemented using CameraController
, ML Kit Analyzer
handles the coordinate transformations between the original ImageAnalysis
stream and PreviewView
for you. It receives the target coordinate system from
CameraX, calculates the coordinate transformation,
and forwards it to ML Kit’s Detector
class for analysis.
To use ML Kit Analyzer with CameraController
, call setImageAnalysisAnalyzer()
and pass it
a new ML Kit Analyzer object with the following in its constructor:
- A list of ML Kit
Detector
s, which CameraX invokes sequentially in order.
The target coordinate system that determines the coordinates of the ML Kit output:
An Executor
that invokes the Consumer callback and delivers
the MlKitAnalyzer.Result
, or the aggregated ML Kit result of a camera frame, to the app.
A Consumer
, which CameraX invokes when there is new ML Kit output.
The following code implements ML Kit Analyzer using CameraController
to set up
a BarcodeScanner
to detect QR codes:
Kotlin
// create BarcodeScanner object
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.build()
val barcodeScanner = BarcodeScanning.getClient(options)
cameraController.setImageAnalysisAnalyzer(
ContextCompat.getMainExecutor(this),
MlKitAnalyzer(
listOf(barcodeScanner),
COORDINATE_SYSTEM_VIEW_REFERENCED,
ContextCompat.getMainExecutor(this)
) { result: MlKitAnalyzer.Result? ->
// The value of result.getResult(barcodeScanner) can be used directly for drawing UI overlay.
}
)
Java
// create BarcodeScanner object
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
.build();
BarcodeScanner barcodeScanner = BarcodeScanning.getClient(options);
cameraController.setImageAnalysisAnalyzer(executor,
new MlKitAnalyzer(List.of(barcodeScanner), COORDINATE_SYSTEM_VIEW_REFERENCED,
executor, result -> {
// The value of result.getResult(barcodeScanner) can be used directly for drawing UI overlay.
});
In the code sample above, ML Kit Analyzer passes the following to
BarcodeScanner
’s Detector
class:
- The transformation Matrix
based on
COORDINATE_SYSTEM_VIEW_REFERENCED
that represents the target coordinate system.
- The camera frames.
If BarcodeScanner
runs into any issues, then its Detector
throws an error,
and ML Kit Analyzer propagates it to your app. If successful, then ML Kit Analyzer returns MLKitAnalyzer.Result#getValue()
, which
in this case is the Barcode
object.
You can also implement ML Kit Analyzer using the ImageAnalysis
class that is part of camera-core
. However, because ImageAnalysis
is not integrated with PreviewView
,
you must manually handle the coordinate transformations. For more information,
see the ML Kit Analyzer reference documentation.
Additional resources
For a working camera app with ML Kit Analyzer functionality,
see the CameraX-MLKit sample.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-03-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-04 UTC."],[],[],null,["# ML Kit Analyzer\n\nGoogle's [ML Kit](https://developers.google.com/ml-kit/guides) provides on-device machine learning Vision APIs for detecting\nfaces, scanning barcodes, labeling images, and more. ML Kit Analyzer makes it\neasier to integrate ML Kit with your CameraX app.\n\nML Kit Analyzer is an implementation of the [`ImageAnalysis.Analyzer`](/reference/androidx/camera/core/ImageAnalysis.Analyzer) interface. It overrides the [default target resolution](/reference/androidx/camera/core/ImageAnalysis.Analyzer#getDefaultTargetResolution())\n(if needed) to optimize for ML Kit usage, handles the coordinate transformations,\nand passes the frames to ML Kit, which returns the aggregated analysis results.\n\nImplement ML Kit Analyzer\n-------------------------\n\nTo implement ML Kit Analyzer, we recommend using the [`CameraController`](/reference/androidx/camera/view/CameraController) class, which works with [`PreviewView`](/reference/androidx/camera/view/PreviewView) to display UI elements. When implemented using `CameraController`, ML Kit Analyzer\nhandles the coordinate transformations between the original `ImageAnalysis`\nstream and `PreviewView` for you. It receives the target coordinate system from\nCameraX, calculates the coordinate transformation,\nand forwards it to ML Kit's [`Detector`](https://developers.google.com/android/reference/com/google/mlkit/vision/interfaces/Detector) class for analysis.\n\nTo use ML Kit Analyzer with `CameraController`, call [`setImageAnalysisAnalyzer()`](/reference/androidx/camera/view/CameraController#setImageAnalysisAnalyzer(java.util.concurrent.Executor,androidx.camera.core.ImageAnalysis.Analyzer)) and pass it\na new ML Kit Analyzer object with the following in its constructor:\n\n- A list of ML Kit `Detector`s, which CameraX invokes sequentially in order.\n- The target coordinate system that determines the coordinates of the ML Kit output:\n\n - [`COORDINATE_SYSTEM_VIEW_REFERENCED`](/reference/androidx/camera/view/CameraController#COORDINATE_SYSTEM_VIEW_REFERENCED()): the transformed `PreviewView` coordinates.\n - [`COORDINATE_SYSTEM_ORIGINAL`](/reference/androidx/camera/core/ImageAnalysis#COORDINATE_SYSTEM_ORIGINAL()): the original `ImageAnalysis` stream coordinates.\n- An [`Executor`](/reference/java/util/concurrent/Executor) that invokes the Consumer callback and delivers\n the [`MlKitAnalyzer.Result`](/reference/androidx/camera/mlkit/vision/MlKitAnalyzer.Result), or the aggregated ML Kit result of a camera frame, to the app.\n\n- A [`Consumer`](/reference/androidx/core/util/Consumer), which CameraX invokes when there is new ML Kit output.\n\nThe following code implements ML Kit Analyzer using `CameraController` to set up\na [`BarcodeScanner`](https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/BarcodeScanner) to detect QR codes: \n\n### Kotlin\n\n```kotlin\n// create BarcodeScanner object\nval options = BarcodeScannerOptions.Builder()\n .setBarcodeFormats(Barcode.FORMAT_QR_CODE)\n .build()\nval barcodeScanner = BarcodeScanning.getClient(options)\n\ncameraController.setImageAnalysisAnalyzer(\n ContextCompat.getMainExecutor(this),\n MlKitAnalyzer(\n listOf(barcodeScanner),\n COORDINATE_SYSTEM_VIEW_REFERENCED,\n ContextCompat.getMainExecutor(this)\n ) { result: MlKitAnalyzer.Result? -\u003e\n // The value of result.getResult(barcodeScanner) can be used directly for drawing UI overlay.\n }\n)\n```\n\n### Java\n\n```java\n// create BarcodeScanner object\nBarcodeScannerOptions options = new BarcodeScannerOptions.Builder()\n .setBarcodeFormats(Barcode.FORMAT_QR_CODE)\n .build();\nBarcodeScanner barcodeScanner = BarcodeScanning.getClient(options);\n\ncameraController.setImageAnalysisAnalyzer(executor,\n new MlKitAnalyzer(List.of(barcodeScanner), COORDINATE_SYSTEM_VIEW_REFERENCED,\n executor, result -\u003e {\n // The value of result.getResult(barcodeScanner) can be used directly for drawing UI overlay.\n });\n```\n\nIn the code sample above, ML Kit Analyzer passes the following to\n`BarcodeScanner`'s `Detector` class:\n\n- The transformation [Matrix](/reference/android/graphics/Matrix) based on `COORDINATE_SYSTEM_VIEW_REFERENCED` that represents the target coordinate system.\n- The camera frames.\n\nIf `BarcodeScanner` runs into any issues, then its `Detector` [throws an error](/reference/androidx/camera/mlkit/vision/MlKitAnalyzer.Result#getThrowable(com.google.mlkit.vision.interfaces.Detector%3C?%3E)),\nand ML Kit Analyzer propagates it to your app. If successful, then ML Kit Analyzer returns [`MLKitAnalyzer.Result#getValue()`](/reference/androidx/camera/mlkit/vision/MlKitAnalyzer.Result#getValue(com.google.mlkit.vision.interfaces.Detector%3CT%3E)), which\nin this case is the [`Barcode`](https://developers.google.com/android/reference/com/google/mlkit/vision/barcode/common/Barcode) object.\n\nYou can also implement ML Kit Analyzer using the [`ImageAnalysis`](/reference/androidx/camera/core/ImageAnalysis) class that is part of `camera-core`. However, because `ImageAnalysis`\nis not integrated with `PreviewView`,\nyou must manually handle the coordinate transformations. For more information,\nsee the [ML Kit Analyzer](/reference/androidx/camera/mlkit/vision/MlKitAnalyzer) reference documentation.\n\nAdditional resources\n--------------------\n\nFor a working camera app with ML Kit Analyzer functionality,\nsee the [CameraX-MLKit](https://github.com/android/camera-samples/tree/main/CameraX-MLKit) sample."]]