สแกนบาร์โค้ดด้วย ML Kit ใน Android

คุณใช้ ML Kit เพื่อจดจำและถอดรหัสบาร์โค้ดได้

ฟีเจอร์ไม่ได้จัดกลุ่มรวมกลุ่ม
การใช้งานระบบจะดาวน์โหลดโมเดลแบบไดนามิกผ่านบริการ Google Playโมเดลจะลิงก์แบบคงที่กับแอปของคุณ ณ เวลาบิลด์
ขนาดแอปเพิ่มขนาดได้ประมาณ 200 KBขนาดเพิ่มขึ้นประมาณ 2.4 MB
เวลาเริ่มต้นอาจต้องรอดาวน์โหลดโมเดลก่อนใช้งานครั้งแรกโมเดลจะพร้อมใช้งานทันที

ลองเลย

ก่อนเริ่มต้น

  1. ในไฟล์ build.gradle ระดับโปรเจ็กต์ ให้ตรวจสอบว่ามีที่เก็บ Maven ของ Google ไว้ในส่วน buildscript และ allprojects

  2. เพิ่มทรัพยากร Dependency สำหรับไลบรารี Android ของ ML Kit ไปยังไฟล์ Gradle ระดับแอปของโมดูล ซึ่งปกติจะอยู่ที่ app/build.gradle เลือกทรัพยากร Dependency รายการใดรายการหนึ่งต่อไปนี้ตามความต้องการของคุณ

    สำหรับการรวมโมเดลกับแอป ให้ทำดังนี้

    dependencies {
      // ...
      // Use this dependency to bundle the model with your app
      implementation 'com.google.mlkit:barcode-scanning:17.2.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.0'
    }
    
  3. หากเลือกใช้โมเดลในบริการ 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>
    

    นอกจากนี้คุณยังตรวจสอบความพร้อมใช้งานของโมเดลอย่างชัดแจ้งและขอการดาวน์โหลดผ่านบริการ Google Play ได้ ModuleInstallClient API

    หากคุณไม่เปิดใช้การดาวน์โหลดโมเดลเวลาติดตั้งหรือขอดาวน์โหลดอย่างชัดแจ้ง ระบบจะดาวน์โหลดโมเดลในครั้งแรกที่คุณเรียกใช้โปรแกรมสแกน คำขอที่คุณส่งก่อนการดาวน์โหลดเสร็จสมบูรณ์จะไม่มีผลลัพธ์ใดๆ

หลักเกณฑ์เกี่ยวกับรูปภาพอินพุต

  • เพื่อให้ 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 มี "คำ" แบบกว้างได้สูงสุด 34 17 หน่วยในแถวเดียว ซึ่งควรจะกว้างอย่างน้อย 1156 พิกเซล

  • การโฟกัสรูปภาพต่ำอาจส่งผลต่อความแม่นยำในการสแกน หากแอปไม่ได้รับผลลัพธ์ที่ยอมรับได้ โปรดขอให้ผู้ใช้ทบทวนรูปภาพอีกครั้ง

  • สำหรับการใช้งานโดยทั่วไป ขอแนะนำให้ส่งรูปภาพที่มีความละเอียดสูงกว่า เช่น 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();

รูปแบบที่รองรับมีดังนี้

  • รหัส 128 (FORMAT_CODE_128)
  • โค้ด 39 (FORMAT_CODE_39)
  • รหัส 93 (FORMAT_CODE_93)
  • โคดาบาร์ (FORMAT_CODABAR)
  • EAN-13 (FORMAT_EAN_13)
  • EAN-8 (FORMAT_EAN_8)
  • อิตาลี (FORMAT_ITF)
  • UPC-A (FORMAT_UPC_A)
  • UPC-E (FORMAT_UPC_E)
  • คิวอาร์โค้ด (FORMAT_QR_CODE)
  • PDF417 (FORMAT_PDF417)
  • แอซเท็ก (FORMAT_AZTEC)
  • เมทริกซ์ข้อมูล (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 an InputImage 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() ซึ่งจะเป็นประโยชน์เมื่อคุณใช้ Intent ACTION_GET_CONTENT เพื่อแจ้งให้ผู้ใช้เลือกรูปภาพจากแอปแกลเลอรี

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 เมกะพิกเซล

    หากความเร็วในการสแกนเป็นสิ่งสำคัญ คุณอาจลดความละเอียดในการจับภาพลงไปอีกได้ อย่างไรก็ตาม โปรดคำนึงถึงข้อกำหนดเกี่ยวกับขนาดบาร์โค้ดขั้นต่ำที่ระบุไว้ข้างต้น

    หากคุณกำลังพยายามจดจำบาร์โค้ดจากลำดับของเฟรมวิดีโอสตรีมมิง เครื่องมือจดจำอาจให้ผลลัพธ์ที่แตกต่างกันในแต่ละเฟรม คุณควรรอจนกว่าจะได้รับชุดค่าเดียวกันติดต่อกัน เพื่อให้มั่นใจว่ากำลังแสดงผลลัพธ์ที่ดี

    ไม่รองรับหมายเลข Checksum สำหรับ ITF และ CODE-39

  • หากคุณใช้ Camera หรือ camera2 API ให้ควบคุมการเรียกใช้ตัวตรวจจับ หากมีเฟรมวิดีโอใหม่พร้อมใช้งานขณะที่ตัวตรวจจับกำลังทำงาน ให้วางเฟรมลง ดูตัวอย่างคลาส VisionProcessorBase ในแอปตัวอย่าง Quickstart
  • หากคุณใช้ CameraX API โปรดตรวจสอบว่าได้ตั้งค่ากลยุทธ์ Backpressure เป็นค่าเริ่มต้น ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST แล้ว ซึ่งจะช่วยรับประกันว่าระบบจะส่งรูปภาพเพื่อทำการวิเคราะห์ได้ทีละ 1 ภาพเท่านั้น หากมีการสร้างรูปภาพเพิ่มเติมเมื่อตัววิเคราะห์ไม่ว่าง ระบบจะปล่อยรูปภาพเหล่านั้นโดยอัตโนมัติและไม่เข้าคิวเพื่อนำส่ง เมื่อปิดรูปภาพที่วิเคราะห์แล้วโดยการเรียกใช้ ImageProxy.close() ระบบจะส่งรูปภาพล่าสุดถัดไป
  • หากใช้เอาต์พุตของตัวตรวจจับเพื่อวางซ้อนกราฟิกบนรูปภาพอินพุต ให้รับผลลัพธ์จาก ML Kit ก่อน จากนั้นจึงแสดงผลรูปภาพและการวางซ้อนในขั้นตอนเดียว ซึ่งจะแสดงบนพื้นผิวจอแสดงผลเพียงครั้งเดียวต่อเฟรมอินพุตแต่ละเฟรม ดูตัวอย่างคลาส CameraSourcePreview และ GraphicOverlay ในแอปตัวอย่าง Quickstart
  • หากคุณใช้ Camera2 API ให้จับภาพในรูปแบบ ImageFormat.YUV_420_888 หากคุณใช้ Camera API เวอร์ชันเก่า ให้จับภาพในรูปแบบ ImageFormat.NV21