คุณใช้ ML Kit เพื่อจดจำและถอดรหัสบาร์โค้ดได้
ฟีเจอร์ | แบบแยก | รวมกลุ่ม |
---|---|---|
การใช้งาน | ระบบจะดาวน์โหลดโมเดลแบบไดนามิกผ่านบริการ Google Play | โมเดลจะลิงก์กับแอปแบบคงที่ในเวลาที่สร้าง |
ขนาดแอป | เพิ่มขนาดประมาณ 200 KB | ขนาดเพิ่มขึ้นประมาณ 2.4 MB |
เวลาเริ่มต้น | คุณอาจต้องรอให้ระบบดาวน์โหลดโมเดลก่อนจึงจะใช้งานได้เป็นครั้งแรก | โมเดลพร้อมใช้งานทันที |
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
- ดูการใช้งาน API นี้ตั้งแต่ต้นจนจบได้ในแอป Material Design Showcase
ก่อนเริ่มต้น
ในไฟล์
build.gradle
ระดับโปรเจ็กต์ ให้ตรวจสอบว่าได้รวมที่เก็บ Maven ของ Google ไว้ทั้งในส่วนbuildscript
และallprojects
เพิ่มทรัพยากร Dependency สำหรับคลัง Android ของ ML Kit ลงในไฟล์ Gradle ระดับแอปของโมดูล ซึ่งโดยปกติคือ
app/build.gradle
เลือกการขึ้นต่อกันรายการใดรายการหนึ่งต่อไปนี้ตามความต้องการของคุณสำหรับการรวมโมเดลกับแอป
dependencies { // ... // Use this dependency to bundle the model with your app implementation 'com.google.mlkit:barcode-scanning:17.3.0' }
สำหรับการใช้โมเดลในบริการ Google Play
dependencies { // ... // Use this dependency to use the dynamically downloaded model in Google Play Services implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.1' }
หากเลือกใช้โมเดลในบริการ Google Play คุณสามารถกำหนดค่า แอปให้ดาวน์โหลดโมเดลลงในอุปกรณ์โดยอัตโนมัติหลังจากติดตั้งแอปจาก Play Store แล้ว โดยเพิ่มประกาศต่อไปนี้ลงในไฟล์
AndroidManifest.xml
ของแอป<application ...> ... <meta-data android:name="com.google.mlkit.vision.DEPENDENCIES" android:value="barcode" > <!-- To use multiple models: android:value="barcode,model2,model3" --> </application>
นอกจากนี้ คุณยังตรวจสอบความพร้อมใช้งานของโมเดลและขอให้ดาวน์โหลดผ่าน ModuleInstallClient API ของบริการ Google Play ได้ด้วย
หากคุณไม่ได้เปิดใช้การดาวน์โหลดโมเดลในเวลาที่ติดตั้งหรือขอให้ดาวน์โหลดอย่างชัดเจน ระบบจะดาวน์โหลดโมเดลเมื่อคุณเรียกใช้เครื่องสแกนเป็นครั้งแรก คำขอที่คุณส่งก่อนการดาวน์โหลดเสร็จสมบูรณ์จะไม่มีผลลัพธ์
หลักเกณฑ์เกี่ยวกับรูปภาพที่ป้อน
-
ML Kit จะอ่านบาร์โค้ดได้อย่างแม่นยำก็ต่อเมื่อรูปภาพอินพุตมี บาร์โค้ดที่แสดงด้วยข้อมูลพิกเซลที่เพียงพอ
ข้อกำหนดเกี่ยวกับข้อมูลพิกเซลที่เฉพาะเจาะจงจะขึ้นอยู่กับทั้งประเภทของ บาร์โค้ดและปริมาณข้อมูลที่เข้ารหัสในบาร์โค้ด เนื่องจากบาร์โค้ดจำนวนมาก รองรับเพย์โหลดขนาดตัวแปร โดยทั่วไปแล้ว หน่วยที่เล็กที่สุดที่มีความหมาย ของบาร์โค้ดควรมีความกว้างอย่างน้อย 2 พิกเซล และสำหรับ โค้ด 2 มิติ ควรมีความสูง 2 พิกเซล
ตัวอย่างเช่น บาร์โค้ด EAN-13 ประกอบด้วยแถบและช่องว่างที่มีความกว้าง 1, 2, 3 หรือ 4 หน่วย ดังนั้นรูปภาพบาร์โค้ด EAN-13 จึงควรมีแถบและช่องว่างที่มีความกว้างอย่างน้อย 2, 4, 6 และ 8 พิกเซล เนื่องจากบาร์โค้ด EAN-13 มีความกว้างทั้งหมด 95 หน่วย บาร์โค้ดจึงควรมีความกว้างอย่างน้อย 190 พิกเซล
รูปแบบที่มีความหนาแน่นสูง เช่น PDF417 จะต้องมีขนาดพิกเซลที่ใหญ่ขึ้นเพื่อให้ ML Kit อ่านได้อย่างน่าเชื่อถือ ตัวอย่างเช่น รหัส PDF417 อาจมี "คำ" กว้าง 17 หน่วยได้สูงสุด 34 คำในแถวเดียว ซึ่งควรมีความกว้างอย่างน้อย 1,156 พิกเซล
-
การโฟกัสรูปภาพไม่ดีอาจส่งผลต่อความแม่นยำในการสแกน หากแอปไม่ได้รับผลลัพธ์ที่ยอมรับได้ ให้ขอให้ผู้ใช้ถ่ายภาพอีกครั้ง
-
สำหรับการใช้งานทั่วไป เราขอแนะนำให้ใช้รูปภาพที่มีความละเอียดสูงขึ้น เช่น 1280x720 หรือ 1920x1080 ซึ่งจะทำให้สแกนบาร์โค้ดจากระยะไกลจากกล้องได้
อย่างไรก็ตาม ในแอปพลิเคชันที่เวลาในการตอบสนองมีความสำคัญ คุณสามารถปรับปรุง ประสิทธิภาพได้โดยการจับภาพที่ความละเอียดต่ำกว่า แต่ต้องให้ บาร์โค้ดเป็นส่วนใหญ่ของรูปภาพอินพุต ดูเพิ่มเติมได้ที่ เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
1. กำหนดค่าเครื่องสแกนบาร์โค้ด
หากทราบรูปแบบบาร์โค้ดที่ต้องการอ่าน คุณจะเพิ่มความเร็วของเครื่องตรวจจับบาร์โค้ดได้โดยกำหนดค่าให้ตรวจจับเฉพาะรูปแบบเหล่านั้นเช่น หากต้องการตรวจหาเฉพาะรหัส Aztec และคิวอาร์โค้ด ให้สร้างออบเจ็กต์ BarcodeScannerOptions
ดังตัวอย่างต่อไปนี้
Kotlin
val options = BarcodeScannerOptions.Builder() .setBarcodeFormats( Barcode.FORMAT_QR_CODE, Barcode.FORMAT_AZTEC) .build()
Java
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder() .setBarcodeFormats( Barcode.FORMAT_QR_CODE, Barcode.FORMAT_AZTEC) .build();
ระบบรองรับรูปแบบต่อไปนี้
- Code 128 (
FORMAT_CODE_128
) - Code 39 (
FORMAT_CODE_39
) - รหัส 93 (
FORMAT_CODE_93
) - โคดาบาร์ (
FORMAT_CODABAR
) - EAN-13 (
FORMAT_EAN_13
) - EAN-8 (
FORMAT_EAN_8
) - ITF (
FORMAT_ITF
) - UPC-A (
FORMAT_UPC_A
) - UPC-E (
FORMAT_UPC_E
) - คิวอาร์โค้ด (
FORMAT_QR_CODE
) - PDF417 (
FORMAT_PDF417
) - Aztec (
FORMAT_AZTEC
) - Data Matrix (
FORMAT_DATA_MATRIX
)
ตั้งแต่โมเดลแบบรวม 17.1.0 และโมเดลแบบแยก 18.2.0 คุณยังเรียกใช้
enableAllPotentialBarcodes()
เพื่อแสดงบาร์โค้ดที่เป็นไปได้ทั้งหมดได้ แม้ว่าจะถอดรหัสไม่ได้ก็ตาม
ซึ่งสามารถใช้เพื่ออำนวยความสะดวกในการตรวจจับเพิ่มเติมได้ เช่น
โดยการซูมกล้องเพื่อรับภาพบาร์โค้ดที่ชัดเจนยิ่งขึ้นในกรอบล้อมที่ส่งคืน
Kotlin
val options = BarcodeScannerOptions.Builder() .setBarcodeFormats(...) .enableAllPotentialBarcodes() // Optional .build()
Java
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder() .setBarcodeFormats(...) .enableAllPotentialBarcodes() // Optional .build();
Further on, starting from bundled library 17.2.0 and unbundled library 18.3.0, a new feature called auto-zoom has been introduced to further enhance the barcode scanning experience. With this feature enabled, the app is notified when all barcodes within the view are too distant for decoding. As a result, the app can effortlessly adjust the camera's zoom ratio to the recommended setting provided by the library, ensuring optimal focus and readability. This feature will significantly enhance the accuracy and success rate of barcode scanning, making it easier for apps to capture information precisely.
To enable auto-zooming and customize the experience, you can utilize the
setZoomSuggestionOptions()
method along with your
own ZoomCallback
handler and desired maximum zoom
ratio, as demonstrated in the code below.
Kotlin
val options = BarcodeScannerOptions.Builder() .setBarcodeFormats(...) .setZoomSuggestionOptions( new ZoomSuggestionOptions.Builder(zoomCallback) .setMaxSupportedZoomRatio(maxSupportedZoomRatio) .build()) // Optional .build()
Java
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder() .setBarcodeFormats(...) .setZoomSuggestionOptions( new ZoomSuggestionOptions.Builder(zoomCallback) .setMaxSupportedZoomRatio(maxSupportedZoomRatio) .build()) // Optional .build();
zoomCallback
is required to be provided to handle whenever the library
suggests a zoom should be performed and this callback will always be called on
the main thread.
The following code snippet shows an example of defining a simple callback.
Kotlin
fun setZoom(ZoomRatio: Float): Boolean { if (camera.isClosed()) return false camera.getCameraControl().setZoomRatio(zoomRatio) return true }
Java
boolean setZoom(float zoomRatio) { if (camera.isClosed()) { return false; } camera.getCameraControl().setZoomRatio(zoomRatio); return true; }
maxSupportedZoomRatio
is related to the camera hardware, and different camera
libraries have different ways to fetch it (see the javadoc of the setter
method). In case this is not provided, an
unbounded zoom ratio might be produced by the library which might not be
supported. Refer to the
setMaxSupportedZoomRatio()
method
introduction to see how to get the max supported zoom ratio with different
Camera libraries.
When auto-zooming is enabled and no barcodes are successfully decoded within
the view, BarcodeScanner
triggers your zoomCallback
with the requested
zoomRatio
. If the callback correctly adjusts the camera to this zoomRatio
,
it is highly probable that the most centered potential barcode will be decoded
and returned.
A barcode may remain undecodable even after a successful zoom-in. In such cases,
BarcodeScanner
may either invoke the callback for another round of zoom-in
until the maxSupportedZoomRatio
is reached, or provide an empty list (or a
list containing potential barcodes that were not decoded, if
enableAllPotentialBarcodes()
was called) to the OnSuccessListener
(which
will be defined in step 4. Process the image).
2. Prepare the input image
To recognize barcodes in an image, create anInputImage
object
from either a Bitmap
, media.Image
, ByteBuffer
, byte array, or a file on
the device. Then, pass the InputImage
object to the
BarcodeScanner
's process
method.
You can create an InputImage
object from different sources, each is explained below.
Using a media.Image
To create an InputImage
object from a media.Image
object, such as when you capture an image from a
device's camera, pass the media.Image
object and the image's
rotation to InputImage.fromMediaImage()
.
If you use the
CameraX library, the OnImageCapturedListener
and
ImageAnalysis.Analyzer
classes calculate the rotation value
for you.
Kotlin
private class YourImageAnalyzer : ImageAnalysis.Analyzer { override fun analyze(imageProxy: ImageProxy) { val mediaImage = imageProxy.image if (mediaImage != null) { val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees) // Pass image to an ML Kit Vision API // ... } } }
Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { @Override public void analyze(ImageProxy imageProxy) { Image mediaImage = imageProxy.getImage(); if (mediaImage != null) { InputImage image = InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees()); // Pass image to an ML Kit Vision API // ... } } }
หากไม่ได้ใช้คลังกล้องที่ให้องศาการหมุนของรูปภาพ คุณ สามารถคำนวณได้จากองศาการหมุนของอุปกรณ์และการวางแนวของเซ็นเซอร์กล้อง ในอุปกรณ์
Kotlin
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 0) ORIENTATIONS.append(Surface.ROTATION_90, 90) ORIENTATIONS.append(Surface.ROTATION_180, 180) ORIENTATIONS.append(Surface.ROTATION_270, 270) } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Throws(CameraAccessException::class) private fun getRotationCompensation(cameraId: String, activity: Activity, isFrontFacing: Boolean): Int { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. val deviceRotation = activity.windowManager.defaultDisplay.rotation var rotationCompensation = ORIENTATIONS.get(deviceRotation) // Get the device's sensor orientation. val cameraManager = activity.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! if (isFrontFacing) { rotationCompensation = (sensorOrientation + rotationCompensation) % 360 } else { // back-facing rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360 } return rotationCompensation }
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 0); ORIENTATIONS.append(Surface.ROTATION_90, 90); ORIENTATIONS.append(Surface.ROTATION_180, 180); ORIENTATIONS.append(Surface.ROTATION_270, 270); } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private int getRotationCompensation(String cameraId, Activity activity, boolean isFrontFacing) throws CameraAccessException { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int rotationCompensation = ORIENTATIONS.get(deviceRotation); // Get the device's sensor orientation. CameraManager cameraManager = (CameraManager) activity.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); if (isFrontFacing) { rotationCompensation = (sensorOrientation + rotationCompensation) % 360; } else { // back-facing rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360; } return rotationCompensation; }
จากนั้นส่งออบเจ็กต์ media.Image
และค่าองศาการหมุนไปยัง InputImage.fromMediaImage()
:
Kotlin
val image = InputImage.fromMediaImage(mediaImage, rotation)
Java
InputImage image = InputImage.fromMediaImage(mediaImage, rotation);
การใช้ URI ของไฟล์
หากต้องการสร้างออบเจ็กต์ InputImage
จาก URI ของไฟล์ ให้ส่งบริบทของแอปและ URI ของไฟล์ไปยัง
InputImage.fromFilePath()
ซึ่งจะมีประโยชน์เมื่อคุณ
ใช้ACTION_GET_CONTENT
Intent เพื่อแจ้งให้ผู้ใช้เลือก
รูปภาพจากแอปแกลเลอรี
Kotlin
val image: InputImage try { image = InputImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
Java
InputImage image; try { image = InputImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
การใช้ ByteBuffer
หรือ ByteArray
หากต้องการสร้างออบเจ็กต์ InputImage
จาก ByteBuffer
หรือ ByteArray
ให้คำนวณองศาการหมุนของรูปภาพก่อน
ตามที่อธิบายไว้ก่อนหน้านี้สำหรับอินพุต media.Image
จากนั้นสร้างออบเจ็กต์ InputImage
ด้วยบัฟเฟอร์หรืออาร์เรย์ พร้อมกับความสูง ความกว้าง รูปแบบการเข้ารหัสสี และองศาการหมุนของรูปภาพ
Kotlin
val image = InputImage.fromByteBuffer( byteBuffer, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 ) // Or: val image = InputImage.fromByteArray( byteArray, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 )
Java
InputImage image = InputImage.fromByteBuffer(byteBuffer, /* image width */ 480, /* image height */ 360, rotationDegrees, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 ); // Or: InputImage image = InputImage.fromByteArray( byteArray, /* image width */480, /* image height */360, rotation, InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12 );
การใช้ Bitmap
หากต้องการสร้างออบเจ็กต์ InputImage
จากออบเจ็กต์ Bitmap
ให้ประกาศดังนี้
Kotlin
val image = InputImage.fromBitmap(bitmap, 0)
Java
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);
รูปภาพแสดงด้วยออบเจ็กต์ Bitmap
พร้อมกับองศาการหมุน
3. รับอินสแตนซ์ของ BarcodeScanner
Kotlin
val scanner = BarcodeScanning.getClient() // Or, to specify the formats to recognize: // val scanner = BarcodeScanning.getClient(options)
Java
BarcodeScanner scanner = BarcodeScanning.getClient(); // Or, to specify the formats to recognize: // BarcodeScanner scanner = BarcodeScanning.getClient(options);
4. ประมวลผลรูปภาพ
ส่งรูปภาพไปยังเมธอดprocess
Kotlin
val result = scanner.process(image) .addOnSuccessListener { barcodes -> // Task completed successfully // ... } .addOnFailureListener { // Task failed with an exception // ... }
Java
Task<List<Barcode>> result = scanner.process(image) .addOnSuccessListener(new OnSuccessListener<List<Barcode>>() { @Override public void onSuccess(List<Barcode> barcodes) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
5. รับข้อมูลจากบาร์โค้ด
หากการดำเนินการจดจำบาร์โค้ดสำเร็จ ระบบจะส่งรายการออบเจ็กต์Barcode
ไปยัง Listener ที่สำเร็จ ออบเจ็กต์ Barcode
แต่ละรายการแสดง
บาร์โค้ดที่ตรวจพบในรูปภาพ สำหรับบาร์โค้ดแต่ละรายการ คุณจะได้รับ
พิกัดขอบเขตในรูปภาพอินพุต รวมถึงข้อมูลดิบที่เข้ารหัสโดย
บาร์โค้ด นอกจากนี้ หากเครื่องสแกนบาร์โค้ดระบุประเภทข้อมูล
ที่บาร์โค้ดเข้ารหัสได้ คุณจะได้รับออบเจ็กต์ที่มีข้อมูลที่แยกวิเคราะห์แล้ว
เช่น
Kotlin
for (barcode in barcodes) { val bounds = barcode.boundingBox val corners = barcode.cornerPoints val rawValue = barcode.rawValue val valueType = barcode.valueType // See API reference for complete list of supported types when (valueType) { Barcode.TYPE_WIFI -> { val ssid = barcode.wifi!!.ssid val password = barcode.wifi!!.password val type = barcode.wifi!!.encryptionType } Barcode.TYPE_URL -> { val title = barcode.url!!.title val url = barcode.url!!.url } } }
Java
for (Barcode barcode: barcodes) { Rect bounds = barcode.getBoundingBox(); Point[] corners = barcode.getCornerPoints(); String rawValue = barcode.getRawValue(); int valueType = barcode.getValueType(); // See API reference for complete list of supported types switch (valueType) { case Barcode.TYPE_WIFI: String ssid = barcode.getWifi().getSsid(); String password = barcode.getWifi().getPassword(); int type = barcode.getWifi().getEncryptionType(); break; case Barcode.TYPE_URL: String title = barcode.getUrl().getTitle(); String url = barcode.getUrl().getUrl(); break; } }
เคล็ดลับในการปรับปรุงประสิทธิภาพแบบเรียลไทม์
หากต้องการสแกนบาร์โค้ดในแอปพลิเคชันแบบเรียลไทม์ ให้ทำตามหลักเกณฑ์ต่อไปนี้เพื่อให้อัตราเฟรมดีที่สุด
-
อย่าบันทึกอินพุตที่ความละเอียดดั้งเดิมของกล้อง ในอุปกรณ์บางรุ่น การบันทึกอินพุตที่ความละเอียดดั้งเดิมจะทำให้ได้รูปภาพขนาดใหญ่มาก (10+ เมกะพิกเซล) ซึ่งส่งผลให้เกิดเวลาในการตอบสนองที่แย่มากโดยไม่มีประโยชน์ต่อ ความแม่นยำ แต่ให้ขอขนาดจากกล้องที่จำเป็น สำหรับการตรวจจับบาร์โค้ด ซึ่งโดยปกติแล้วจะไม่เกิน 2 เมกะพิกเซล
หากความเร็วในการสแกนเป็นสิ่งสำคัญ คุณสามารถลดความละเอียดในการจับภาพลงได้อีก อย่างไรก็ตาม โปรดคำนึงถึงข้อกำหนดด้านขนาดบาร์โค้ดขั้นต่ำ ที่ระบุไว้ข้างต้น
หากคุณพยายามจดจำบาร์โค้ดจากลำดับของเฟรมวิดีโอสตรีมมิง ตัวจดจำอาจให้ผลลัพธ์ที่แตกต่างกันในแต่ละเฟรม คุณควรรอจนกว่าจะได้รับค่าเดียวกัน ติดต่อกันหลายครั้งเพื่อให้แน่ใจว่าคุณแสดงผลลัพธ์ที่ดี
ITF และ CODE-39 ไม่รองรับตัวเลขตรวจสอบ
- หากคุณใช้ API ของ
Camera
หรือcamera2
ให้จำกัดการเรียกไปยังเครื่องตรวจจับ หากมีเฟรมวิดีโอใหม่ ขณะที่เครื่องตรวจจับทำงาน ให้ทิ้งเฟรม ดูตัวอย่างได้ที่คลาสVisionProcessorBase
ในแอปตัวอย่างการเริ่มต้นอย่างรวดเร็ว - หากคุณใช้
CameraX
API โปรดตรวจสอบว่าได้ตั้งค่ากลยุทธ์การควบคุมปริมาณการรับส่งเป็นค่าเริ่มต้นImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
ซึ่งจะรับประกันว่าระบบจะส่งรูปภาพเพียงรูปเดียวเพื่อวิเคราะห์ในแต่ละครั้ง หากมีการสร้างรูปภาพเพิ่มเติมเมื่อเครื่องมือวิเคราะห์ทำงานอยู่ ระบบจะทิ้งรูปภาพเหล่านั้นโดยอัตโนมัติและจะไม่จัดคิวเพื่อส่ง เมื่อปิดรูปภาพที่กำลังวิเคราะห์โดยเรียกใช้ ImageProxy.close() ระบบจะส่งรูปภาพล่าสุดถัดไป - หากคุณใช้เอาต์พุตของเครื่องตรวจจับเพื่อซ้อนทับกราฟิกบน
รูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นจึงแสดงรูปภาพ
และซ้อนทับในขั้นตอนเดียว ซึ่งจะแสดงในพื้นผิวการแสดงผล
เพียงครั้งเดียวสำหรับแต่ละเฟรมอินพุต ดูตัวอย่างได้ที่คลาส
CameraSourcePreview
และGraphicOverlay
ในแอปตัวอย่างการเริ่มต้นอย่างรวดเร็ว - หากใช้ API ของ Camera2 ให้ถ่ายภาพในรูปแบบ
ImageFormat.YUV_420_888
หากใช้ Camera API เวอร์ชันเก่า ให้ถ่ายภาพในรูปแบบImageFormat.NV21
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-04 UTC