ML Kit Analyzer
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
ML Kit Google menyediakan Vision API machine learning di perangkat untuk mendeteksi
wajah, memindai kode batang, melabeli gambar, dan lainnya. ML Kit Analyzer mempermudah
integrasi ML Kit dengan aplikasi CameraX Anda.
ML Kit Analyzer adalah implementasi antarmuka ImageAnalysis.Analyzer
. ML Kit Analyzer menggantikan resolusi target default
(jika diperlukan) untuk mengoptimalkan penggunaan ML Kit, menangani transformasi koordinat,
dan meneruskan bingkai ke ML Kit, yang menampilkan hasil analisis gabungan.
Mengimplementasikan ML Kit Analyzer
Untuk mengimplementasikan ML Kit Analyzer, sebaiknya gunakan class CameraController
, yang berfungsi dengan PreviewView
untuk menampilkan elemen UI. Jika diimplementasikan menggunakan CameraController
, ML Kit Analyzer
akan menangani transformasi koordinat antara aliran ImageAnalysis
asli dan PreviewView
untuk Anda. ML Kit Analyzer menerima sistem koordinat target dari
CameraX, menghitung transformasi koordinat,
dan meneruskannya ke class Detector
ML Kit untuk dianalisis.
Untuk menggunakan ML Kit Analyzer dengan CameraController
, panggil setImageAnalysisAnalyzer()
dan teruskan
objek ML Kit Analyzer baru dengan yang berikut ini di konstruktornya:
- Daftar
Detector
ML Kit, yang dipanggil CameraX secara berurutan.
Sistem koordinat target yang menentukan koordinat output ML Kit:
Executor
yang memanggil callback Konsumen dan mengirimkan
MlKitAnalyzer.Result
, atau hasil ML Kit gabungan dari bingkai kamera, ke aplikasi.
Consumer
, yang dipanggil CameraX saat ada output ML Kit baru.
Kode berikut mengimplementasikan ML Kit Analyzer menggunakan CameraController
untuk menyiapkan
BarcodeScanner
guna mendeteksi kode QR:
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.
});
Pada contoh kode di atas, ML Kit Analyzer meneruskan yang berikut ke
class Detector
BarcodeScanner
:
- Matrix
transformasi berdasarkan
COORDINATE_SYSTEM_VIEW_REFERENCED
yang mewakili sistem koordinat target.
- Bingkai kamera.
Jika BarcodeScanner
mengalami masalah, Detector
akan menampilkan error,
dan ML Kit Analyzer akan mendistribusikannya ke aplikasi Anda. Jika berhasil, ML Kit Analyzer akan menampilkan MLKitAnalyzer.Result#getValue()
, yang
dalam hal ini adalah objek Barcode
.
Anda juga dapat mengimplementasikan ML Kit Analyzer menggunakan class ImageAnalysis
yang merupakan bagian dari camera-core
. Namun, karena ImageAnalysis
tidak terintegrasi dengan PreviewView
,
Anda harus menangani transformasi koordinat secara manual. Untuk informasi selengkapnya,
lihat dokumentasi referensi ML Kit Analyzer.
Referensi lainnya
Untuk aplikasi kamera yang berfungsi dengan fungsionalitas ML Kit Analyzer, lihat contoh CameraX-MLKit.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-07-27 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."]]