ML Kit 분석 도구
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Google의 ML Kit는 얼굴 인식, 바코드 스캔, 이미지 라벨 지정 등을 위한 기기 내 머신러닝 Vision API를 제공합니다. ML Kit 분석 도구를 사용하면 ML Kit를 CameraX 앱과 더 쉽게 통합할 수 있습니다.
ML Kit 분석 도구는 ImageAnalysis.Analyzer
인터페이스의 구현입니다. 기본 타겟 해상도를 재정의하여(필요한 경우) ML Kit 사용을 최적화하고, 좌표 변환을 처리하고, 프레임을 ML Kit에 전달하여 집계된 분석 결과를 반환합니다.
ML Kit 분석 도구 구현
ML Kit 분석 도구를 구현하려면 PreviewView
와 함께 작동하여 UI 요소를 표시하는 CameraController
클래스를 사용하는 것이 좋습니다. CameraController
를 사용하여 구현하면 ML Kit 분석 도구에서 원래 ImageAnalysis
스트림과 PreviewView
간의 좌표 변환을 처리합니다. CameraX에서 타겟 좌표계를 수신하여 좌표 변환을 계산한 후 분석을 위해 ML Kit의 Detector
클래스로 전달합니다.
CameraController
와 함께 ML Kit 분석 도구를 사용하려면 setImageAnalysisAnalyzer()
를 호출하고 생성자에 다음이 포함된 새 ML Kit 분석 도구 객체를 전달합니다.
- CameraX가 순차적으로 호출하는 ML Kit
Detector
목록
ML Kit 출력의 좌표를 결정하는 타겟 좌표계:
Executor
: 소비자 콜백을 호출하고 MlKitAnalyzer.Result
또는 카메라 프레임의 집계된 ML Kit 결과를 앱에 전달합니다.
Consumer
: 새 ML Kit 출력이 있을 때 CameraX에서 호출합니다.
다음 코드는 QR 코드를 감지하는 BarcodeScanner
를 설정하도록 CameraController
를 사용하여 ML Kit 분석 도구를 구현합니다.
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.
}
)
자바
// 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
클래스에 다음을 전달합니다.
- 타겟 좌표계를 나타내는
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 샘플을 참고하세요.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","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(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."]]