ML Kit 分析工具
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google 的 ML Kit 提供裝置端機器學習 Vision API,用於偵測臉孔、掃描條碼、為圖片加上標籤等。有了 ML Kit 分析工具,就能更輕鬆整合 ML Kit 與 CameraX 應用程式。
ML Kit 分析工具是 ImageAnalysis.Analyzer
介面的其中一個實作項目。這項工具會視需要覆寫預設目標解析度來將 ML Kit 使用情形最佳化、處理座標轉換,並將影格傳遞至 ML Kit,以便傳回匯總分析結果。
實作 ML Kit 分析工具
如要實作 ML Kit 分析工具,建議使用 CameraController
類別,這個類別與 PreviewView
搭配運作可顯示 UI 元素。使用 CameraController
進行實作時,ML Kit 分析工具會代為處理原始 ImageAnalysis
串流與 PreviewView
之間的座標轉換。這項工具會從 CameraX 接收目標座標系統、計算座標轉換,並轉送至 ML Kit 的 Detector
類別用於分析。
如要將 ML Kit 分析工具與 CameraController
搭配使用,請呼叫 setImageAnalysisAnalyzer()
、向這段程式碼傳遞新的 ML Kit 分析工具物件,並將下列項目加入其建構函式:
以下程式碼使用 CameraController
實作 ML Kit 分析工具,設定用於偵測 QR code 的 BarcodeScanner
:
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.
});
在上方的程式碼範例中,ML Kit 分析工具將以下項目傳遞至 BarcodeScanner
的 Detector
類別:
- 轉換 Matrix,內容以代表目標座標系統的
COORDINATE_SYSTEM_VIEW_REFERENCED
為基礎。
- 相機影格。
如果 BarcodeScanner
發生任何問題,其 Detector
就會擲回錯誤,且 ML Kit 分析工具會將這項資訊傳送到應用程式。成功後,ML Kit 分析工具將傳回 MLKitAnalyzer.Result#getValue()
(在本例中為 Barcode
物件)。
您也可以使用 camera-core
包含的 ImageAnalysis
類別實作 ML Kit 分析工具。不過,由於 ImageAnalysis
未與 PreviewView
整合,因此您必須手動處理座標轉換。詳情請見 ML Kit 分析工具參考說明文件。
其他資源
如需具有 ML Kit 分析工具功能的可運作相機應用程式,請參閱 CameraX-MLKit 範例。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]