Android पर एमएल किट से बारकोड स्कैन करें

बारकोड को पहचानने और डिकोड करने के लिए, ML Kit का इस्तेमाल किया जा सकता है.

सुविधाअलग-अलग बेचे जाने वालेबंडल किए गए
लागू करनामॉडल, Google Play services की मदद से डाइनैमिक तौर पर डाउनलोड किया जाता है.मॉडल, बिल्ड के समय आपके ऐप्लिकेशन से स्टैटिक तौर पर लिंक होता है.
ऐप्लिकेशन का साइज़साइज़ में करीब 200 केबी की बढ़ोतरी.साइज़ में करीब 2.4 एमबी की बढ़ोतरी होती है.
डेटा लेयर में इवेंट बनाने की प्रोसेस में लगने वाला समयपहली बार इस्तेमाल करने से पहले, मॉडल डाउनलोड होने का इंतज़ार करना पड़ सकता है.मॉडल तुरंत उपलब्ध हो जाता है.

इसे आज़माएं

शुरू करने से पहले

  1. प्रोजेक्ट-लेवल की build.gradle फ़ाइल में, buildscript और allprojects, दोनों सेक्शन में Google की Maven रिपॉज़िटरी को शामिल करना न भूलें.

  2. अपने मॉड्यूल की ऐप्लिकेशन-लेवल की Gradle फ़ाइल में, ML Kit की Android लाइब्रेरी की डिपेंडेंसी जोड़ें. आम तौर पर, यह फ़ाइल 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 services में मॉडल का इस्तेमाल करने के लिए:

    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'
    }
    
  3. अगर आपको Google Play services में मॉडल का इस्तेमाल करना है, तो अपने ऐप्लिकेशन को इस तरह कॉन्फ़िगर किया जा सकता है कि 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 services के ModuleInstallClient API की मदद से, मॉडल की उपलब्धता की साफ़ तौर पर जांच की जा सकती है. साथ ही, डाउनलोड करने का अनुरोध भी किया जा सकता है.

    अगर आपने इंस्टॉल के समय मॉडल डाउनलोड करने की सुविधा चालू नहीं की है या साफ़ तौर पर डाउनलोड करने का अनुरोध नहीं किया है, तो स्कैनर को पहली बार चलाने पर मॉडल डाउनलोड हो जाता है. डाउनलोड पूरा होने से पहले किए गए अनुरोधों से कोई नतीजा नहीं मिलता.

इनपुट इमेज के लिए दिशा-निर्देश

  • ML Kit को बारकोड को सटीक तरीके से पढ़ने के लिए, इनपुट इमेज में ऐसे बारकोड होने चाहिए जिनमें ज़रूरत के मुताबिक पिक्सल डेटा हो.

    पिक्सल डेटा की खास ज़रूरी शर्तें, बारकोड के टाइप और उसमें एन्कोड किए गए डेटा की संख्या, दोनों पर निर्भर करती हैं. ऐसा इसलिए है, क्योंकि कई बारकोड में अलग-अलग साइज़ के पेलोड का इस्तेमाल किया जा सकता है. आम तौर पर, बारकोड की सबसे छोटी यूनिट कम से कम दो पिक्सल चौड़ी होनी चाहिए. साथ ही, दो डाइमेंशन वाले कोड के लिए, दो पिक्सल लंबी होनी चाहिए.

    उदाहरण के लिए, 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 ऑब्जेक्ट बनाएं:

KotlinJava
val options = BarcodeScannerOptions.Builder()
        .setBarcodeFormats(
                Barcode.FORMAT_QR_CODE,
                Barcode.FORMAT_AZTEC)
        .build()
BarcodeScannerOptions options =
        new BarcodeScannerOptions.Builder()
        .setBarcodeFormats(
                Barcode.FORMAT_QR_CODE,
                Barcode.FORMAT_AZTEC)
        .build();

ये फ़ॉर्मैट काम करते हैं:

  • Code 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)
  • Aztec (FORMAT_AZTEC)
  • डेटा मैट्रिक्स (FORMAT_DATA_MATRIX)

बंडल किए गए मॉडल 17.1.0 और अनबंड किए गए मॉडल 18.2.0 से, सभी संभावित बारकोड दिखाने के लिए enableAllPotentialBarcodes() को भी कॉल किया जा सकता है. भले ही, उन्हें डिकोड न किया जा सके. इसका इस्तेमाल, बारकोड की पहचान करने के लिए किया जा सकता है. उदाहरण के लिए, कैमरे को ज़ूम करके, दिखाए गए बाउंडिंग बॉक्स में मौजूद किसी बारकोड की साफ़ इमेज पाना.

KotlinJava
val options = BarcodeScannerOptions.Builder()
        .setBarcodeFormats(...)
        .enableAllPotentialBarcodes() // Optional
        .build()
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.

KotlinJava
val options = BarcodeScannerOptions.Builder()
        .setBarcodeFormats(...)
        .setZoomSuggestionOptions(
            new ZoomSuggestionOptions.Builder(zoomCallback)
                .setMaxSupportedZoomRatio(maxSupportedZoomRatio)
                .build()) // Optional
        .build()
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.

KotlinJava
fun setZoom(ZoomRatio: Float): Boolean {
    if (camera.isClosed()) return false
    camera.getCameraControl().setZoomRatio(zoomRatio)
    return true
}
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.

KotlinJava
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
            // ...
        }
    }
}
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
          // ...
        }
    }
}

अगर आपने ऐसी कैमरा लाइब्रेरी का इस्तेमाल नहीं किया है जो इमेज के घूमने की डिग्री बताती है, तो डिवाइस के घूमने की डिग्री और डिवाइस में कैमरे के सेंसर के ओरिएंटेशन से इसका हिसाब लगाया जा सकता है:

KotlinJava
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
}
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() में घुमाव की डिग्री की वैल्यू पास करें:

KotlinJava
val image = InputImage.fromMediaImage(mediaImage, rotation)
InputImage image = InputImage.fromMediaImage(mediaImage, rotation);

फ़ाइल के यूआरआई का इस्तेमाल करना

फ़ाइल यूआरआई से InputImage ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन कॉन्टेक्स्ट और फ़ाइल यूआरआई को InputImage.fromFilePath() में पास करें. यह तब काम आता है, जब उपयोगकर्ता को अपने गैलरी ऐप्लिकेशन से कोई इमेज चुनने के लिए कहने के लिए, ACTION_GET_CONTENT इंटेंट का इस्तेमाल किया जाता है.

KotlinJava
val image: InputImage
try {
    image = InputImage.fromFilePath(context, uri)
} catch (e: IOException) {
    e.printStackTrace()
}
InputImage image;
try {
    image = InputImage.fromFilePath(context, uri);
} catch (IOException e) {
    e.printStackTrace();
}

ByteBuffer या ByteArray का इस्तेमाल करना

ByteBuffer या ByteArray से InputImage आइटम बनाने के लिए, सबसे पहले इमेज के घूमने की डिग्री का हिसाब लगाएं. यह हिसाब लगाने का तरीका, media.Image इनपुट के लिए पहले बताया गया है. इसके बाद, बफ़र या ऐरे के साथ InputImage ऑब्जेक्ट बनाएं. साथ ही, इमेज की ऊंचाई, चौड़ाई, कलर कोडिंग फ़ॉर्मैट, और घुमाव की डिग्री भी डालें:

KotlinJava
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
)
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 का इस्तेमाल करना

Bitmap ऑब्जेक्ट से InputImage ऑब्जेक्ट बनाने के लिए, यह एलान करें:

KotlinJava
val image = InputImage.fromBitmap(bitmap, 0)
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);

इमेज को घुमाने के डिग्री के साथ Bitmap ऑब्जेक्ट से दिखाया जाता है.

3. BarcodeScanner का इंस्टेंस पाना

KotlinJava
val scanner = BarcodeScanning.getClient()
// Or, to specify the formats to recognize:
// val scanner = BarcodeScanning.getClient(options)
BarcodeScanner scanner = BarcodeScanning.getClient();
// Or, to specify the formats to recognize:
// BarcodeScanner scanner = BarcodeScanning.getClient(options);

4. इमेज को प्रोसेस करना

इमेज को process वाले तरीके पर पास करें:

KotlinJava
val result = scanner.process(image)
        .addOnSuccessListener { barcodes ->
            // Task completed successfully
            // ...
        }
        .addOnFailureListener {
            // Task failed with an exception
            // ...
        }
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 ऑब्जेक्ट की सूची, सफलता के बारे में बताने वाले लिसनर को भेजी जाती है. हर Barcode ऑब्जेक्ट, इमेज में पहचाने गए बारकोड को दिखाता है. हर बारकोड के लिए, इनपुट इमेज में उसके बॉउंडिंग कोऑर्डिनेट के साथ-साथ, बारकोड से कोड में बदला गया रॉ डेटा भी मिल सकता है. अगर बारकोड स्कैनर, बारकोड से एन्क्रिप्ट किए गए डेटा का टाइप पता लगा पाता है, तो आपको पार्स किए गए डेटा वाला ऑब्जेक्ट मिल सकता है.

उदाहरण के लिए:

KotlinJava
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
        }
    }
}
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 से ज़्यादा मेगापिक्सल) इमेज बनती हैं. इस वजह से, रिस्पॉन्स में बहुत ज़्यादा समय लगता है और सटीक जानकारी भी नहीं मिलती. इसके बजाय, कैमरे से सिर्फ़ उतने साइज़ का अनुरोध करें जितने की ज़रूरत है, ताकि बारकोड का पता लगाया जा सके. आम तौर पर, यह साइज़ दो मेगापिक्सल से ज़्यादा नहीं होता.

    अगर आपको स्कैन करने की स्पीड ज़्यादा अहम है, तो इमेज कैप्चर करने के रिज़ॉल्यूशन को और भी कम किया जा सकता है. हालांकि, ऊपर बताए गए बारकोड के साइज़ से जुड़ी ज़रूरी शर्तों को ध्यान में रखें.

    अगर आपको स्ट्रीमिंग वीडियो के फ़्रेम के क्रम से बारकोड पहचानने की कोशिश करनी है, तो हो सकता है कि बारकोड पहचानने वाली सुविधा, हर फ़्रेम के लिए अलग-अलग नतीजे दिखाए. आपको तब तक इंतज़ार करना चाहिए, जब तक आपको एक ही वैल्यू की लगातार सीरीज़ न मिल जाए. इससे आपको यह पक्का करने में मदद मिलेगी कि आपको अच्छा नतीजा मिल रहा है.

    चेकसम अंक, ITF और CODE-39 के लिए काम नहीं करता.

  • अगर Camera या camera2 एपीआई का इस्तेमाल किया जाता है, तो डिटेक्टर को कॉल को कम करें. अगर डिटेक्टर चालू होने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध होता है, तो फ़्रेम को छोड़ दें. उदाहरण के लिए, क्विकस्टार्ट सैंपल ऐप्लिकेशन में VisionProcessorBase क्लास देखें.
  • अगर CameraX एपीआई का इस्तेमाल किया जाता है, तो पक्का करें कि बैकप्रेशर की रणनीति, डिफ़ॉल्ट वैल्यू पर सेट हो ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST. इससे यह पक्का होता है कि विश्लेषण के लिए एक बार में सिर्फ़ एक इमेज डिलीवर की जाएगी. अगर विश्लेषक व्यस्त होने पर ज़्यादा इमेज जनरेट होती हैं, तो वे अपने-आप हट जाएंगी और डिलीवरी के लिए कतार में नहीं जोड़ी जाएंगी. ImageProxy.close() को कॉल करके, जिस इमेज का विश्लेषण किया जा रहा है उसे बंद करने के बाद, अगली नई इमेज डिलीवर की जाएगी.
  • अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो पहले ML Kit से नतीजा पाएं. इसके बाद, एक ही चरण में इमेज को रेंडर करें और ओवरले करें. यह हर इनपुट फ़्रेम के लिए, डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर होता है. उदाहरण के लिए, क्विकस्टार्ट सैंपल ऐप्लिकेशन में CameraSourcePreview और GraphicOverlay क्लास देखें.
  • अगर Camera2 API का इस्तेमाल किया जा रहा है, तो इमेज को ImageFormat.YUV_420_888 फ़ॉर्मैट में कैप्चर करें. अगर पुराने Camera API का इस्तेमाल किया जा रहा है, तो इमेज को ImageFormat.NV21 फ़ॉर्मैट में कैप्चर करें.