बारकोड को पहचानने और उसे डिकोड करने के लिए, ML Kit का इस्तेमाल किया जा सकता है.
शुरू करने से पहले
- अगर आपने पहले से Firebase को नहीं जोड़ा है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
- अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर
app/build.gradle
) में ML Kit Android लाइब्रेरी के लिए डिपेंडेंसी जोड़ें:apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.3' implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.1' }
इनपुट इमेज के लिए दिशा-निर्देश
-
ML Kit में बारकोड को सही तरीके से पढ़ने के लिए, इनपुट इमेज में बारकोड होने चाहिए. इन्हें ज़रूरत के मुताबिक पिक्सल डेटा के ज़रिए दिखाया जाता है.
पिक्सल डेटा से जुड़ी ज़रूरी शर्तें, बारकोड के टाइप और उसमें कोड में बदले गए डेटा की संख्या, दोनों पर निर्भर करती हैं. इसकी वजह यह है कि ज़्यादातर बारकोड, वैरिएबल लेंथ पेलोड के साथ काम करते हैं. आम तौर पर, बारकोड की सबसे छोटी और काम की यूनिट, कम से कम दो पिक्सल चौड़ी होनी चाहिए. साथ ही, दो डाइमेंशन वाले कोड के लिए, इसकी लंबाई 2 पिक्सल होनी चाहिए.
उदाहरण के लिए, EAN-13 बारकोड बार और स्पेस से बने होते हैं जिनकी चौड़ाई 1, 2, 3 या 4 यूनिट होती है. इसलिए, EAN-13 बारकोड इमेज में बार और स्पेस होते हैं और इनकी चौड़ाई कम से कम 2, 4, 6, और 8 पिक्सल होती है. EAN-13 बारकोड कुल 95 यूनिट चौड़ा होता है. इसलिए, बारकोड कम से कम 190 पिक्सल चौड़ा होना चाहिए.
PDF417 जैसे डेंसर फ़ॉर्मैट में, एमएल किट को सही तरीके से पढ़ने के लिए ज़्यादा पिक्सल डाइमेंशन की ज़रूरत होती है. उदाहरण के लिए, PDF417 कोड में एक लाइन में 17 यूनिट चौड़े "शब्द" हो सकते हैं. आम तौर पर, इनकी चौड़ाई कम से कम 1156 पिक्सल होनी चाहिए.
-
इमेज पर फ़ोकस खराब होने से, स्कैन करने के तरीके पर बुरा असर पड़ सकता है. अगर आपको स्वीकार किए जाने वाले नतीजे नहीं मिल रहे हैं, तो उपयोगकर्ता को इमेज दोबारा कैप्चर करने के लिए कहें.
-
आम तौर पर इस्तेमाल किए जाने वाले ऐप्लिकेशन के लिए, ज़्यादा रिज़ॉल्यूशन वाली इमेज (जैसे, 1280x720 या 1920x1080) दें. इससे बारकोड को कैमरे से ज़्यादा दूरी से भी पहचाना जा सकता है.
हालांकि, जिन ऐप्लिकेशन में इंतज़ार का समय बहुत ज़्यादा ज़रूरी होता है उनमें कम रिज़ॉल्यूशन वाली इमेज कैप्चर करके, परफ़ॉर्मेंस को बेहतर बनाया जा सकता है. हालांकि, यह ज़रूरी है कि इनपुट इमेज के ज़्यादातर हिस्से में बारकोड डाला गया हो. रीयल-टाइम में परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह भी देखें.
1. बारकोड डिटेक्टर को कॉन्फ़िगर करें
अगर आपको पता है कि आपको किस बारकोड फ़ॉर्मैट में पढ़ने की उम्मीद है, तो बारकोड डिटेक्टर को उसकी स्पीड बढ़ाने के लिए, उसे सिर्फ़ उन फ़ॉर्मैट का पता लगाने के लिए कॉन्फ़िगर करें.उदाहरण के लिए, सिर्फ़ Aztec कोड और क्यूआर कोड का पता लगाने के लिए, FirebaseVisionBarcodeDetectorOptions
ऑब्जेक्ट बनाएं. इसका उदाहरण नीचे दिया गया है:
Java
FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder() .setBarcodeFormats( FirebaseVisionBarcode.FORMAT_QR_CODE, FirebaseVisionBarcode.FORMAT_AZTEC) .build();
Kotlin+KTX
val options = FirebaseVisionBarcodeDetectorOptions.Builder() .setBarcodeFormats( FirebaseVisionBarcode.FORMAT_QR_CODE, FirebaseVisionBarcode.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
) - यूपीसी-ए (
FORMAT_UPC_A
) - यूपीसी-ई (
FORMAT_UPC_E
) - क्यूआर कोड (
FORMAT_QR_CODE
) - PDF417 (
FORMAT_PDF417
) - ऐज़टेक (
FORMAT_AZTEC
) - डेटा मैट्रिक्स (
FORMAT_DATA_MATRIX
)
2. बारकोड डिटेक्टर चलाएं
किसी इमेज में बारकोड की पहचान करने के लिए,Bitmap
, media.Image
, ByteBuffer
, बाइट कलेक्शन या डिवाइस पर मौजूद किसी फ़ाइल से FirebaseVisionImage
ऑब्जेक्ट बनाएं. इसके बाद, FirebaseVisionImage
ऑब्जेक्ट को
FirebaseVisionBarcodeDetector
के detectInImage
तरीके में पास करें.
अपनी इमेज से
FirebaseVisionImage
ऑब्जेक्ट बनाएं.-
किसी
media.Image
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, जैसे कि किसी डिवाइस के कैमरे से इमेज कैप्चर करते समय,media.Image
ऑब्जेक्ट को पास करें और इमेज कोFirebaseVisionImage.fromMediaImage()
पर घुमाएं.अगर CameraX लाइब्रेरी का इस्तेमाल किया जाता है, तो
OnImageCapturedListener
औरImageAnalysis.Analyzer
क्लास आपके लिए रोटेशन वैल्यू का हिसाब लगाती हैं. इसलिए, आपकोFirebaseVisionImage.fromMediaImage()
को कॉल करने से पहले, रोटेशन वैल्यू को एमएल किट केROTATION_
कॉन्सटेंट में से किसी एक में बदलना होगा:Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { private int degreesToFirebaseRotation(int degrees) { switch (degrees) { case 0: return FirebaseVisionImageMetadata.ROTATION_0; case 90: return FirebaseVisionImageMetadata.ROTATION_90; case 180: return FirebaseVisionImageMetadata.ROTATION_180; case 270: return FirebaseVisionImageMetadata.ROTATION_270; default: throw new IllegalArgumentException( "Rotation must be 0, 90, 180, or 270."); } } @Override public void analyze(ImageProxy imageProxy, int degrees) { if (imageProxy == null || imageProxy.getImage() == null) { return; } Image mediaImage = imageProxy.getImage(); int rotation = degreesToFirebaseRotation(degrees); FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); // Pass image to an ML Kit Vision API // ... } }
Kotlin+KTX
private class YourImageAnalyzer : ImageAnalysis.Analyzer { private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) { 0 -> FirebaseVisionImageMetadata.ROTATION_0 90 -> FirebaseVisionImageMetadata.ROTATION_90 180 -> FirebaseVisionImageMetadata.ROTATION_180 270 -> FirebaseVisionImageMetadata.ROTATION_270 else -> throw Exception("Rotation must be 0, 90, 180, or 270.") } override fun analyze(imageProxy: ImageProxy?, degrees: Int) { val mediaImage = imageProxy?.image val imageRotation = degreesToFirebaseRotation(degrees) if (mediaImage != null) { val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) // Pass image to an ML Kit Vision API // ... } } }
अगर इमेज का रोटेशन देने वाली कैमरा लाइब्रेरी का इस्तेमाल नहीं किया जाता है, तो इमेज को डिवाइस के रोटेशन और डिवाइस में कैमरा सेंसर के ओरिएंटेशन के आधार पर कैलकुलेट किया जा सकता है:
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * 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, Context context) 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); // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360; // Return the corresponding FirebaseVisionImageMetadata rotation value. int result; switch (rotationCompensation) { case 0: result = FirebaseVisionImageMetadata.ROTATION_0; break; case 90: result = FirebaseVisionImageMetadata.ROTATION_90; break; case 180: result = FirebaseVisionImageMetadata.ROTATION_180; break; case 270: result = FirebaseVisionImageMetadata.ROTATION_270; break; default: result = FirebaseVisionImageMetadata.ROTATION_0; Log.e(TAG, "Bad rotation value: " + rotationCompensation); } return result; }
Kotlin+KTX
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 90) ORIENTATIONS.append(Surface.ROTATION_90, 0) ORIENTATIONS.append(Surface.ROTATION_180, 270) ORIENTATIONS.append(Surface.ROTATION_270, 180) } /** * 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, context: Context): 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) // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360 // Return the corresponding FirebaseVisionImageMetadata rotation value. val result: Int when (rotationCompensation) { 0 -> result = FirebaseVisionImageMetadata.ROTATION_0 90 -> result = FirebaseVisionImageMetadata.ROTATION_90 180 -> result = FirebaseVisionImageMetadata.ROTATION_180 270 -> result = FirebaseVisionImageMetadata.ROTATION_270 else -> { result = FirebaseVisionImageMetadata.ROTATION_0 Log.e(TAG, "Bad rotation value: $rotationCompensation") } } return result }
इसके बाद,
media.Image
ऑब्जेक्ट और रोटेशन वैल्यू कोFirebaseVisionImage.fromMediaImage()
को पास करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin+KTX
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- फ़ाइल यूआरआई से
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन का कॉन्टेक्स्ट और फ़ाइल यूआरआई कोFirebaseVisionImage.fromFilePath()
पर पास करें. यह तब काम आता है, जब आप उपयोगकर्ता को उसके गैलरी ऐप्लिकेशन से कोई इमेज चुनने का अनुरोध करने के लिएACTION_GET_CONTENT
इंटेंट का इस्तेमाल करते हैं.Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin+KTX
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
ByteBuffer
या बाइट कलेक्शन सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, सबसे पहलेmedia.Image
इनपुट के लिए ऊपर बताए गए तरीके से इमेज रोटेशन का हिसाब लगाएं.इसके बाद, ऐसा
FirebaseVisionImageMetadata
ऑब्जेक्ट बनाएं जिसमें इमेज की ऊंचाई, चौड़ाई, कलर एन्कोडिंग फ़ॉर्मैट, और रोटेशन शामिल हो:Java
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build();
Kotlin+KTX
val metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build()
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, बफ़र या अरे और मेटाडेटा ऑब्जेक्ट का इस्तेमाल करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin+KTX
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
- किसी
Bitmap
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए:Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin+KTX
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmap
ऑब्जेक्ट के ज़रिए दिखाई गई इमेज, सीधी होनी चाहिए. इसमें अलग से घुमाने की ज़रूरत नहीं है.
-
FirebaseVisionBarcodeDetector
का इंस्टेंस पाएं:Java
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance() .getVisionBarcodeDetector(); // Or, to specify the formats to recognize: // FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance() // .getVisionBarcodeDetector(options);
Kotlin+KTX
val detector = FirebaseVision.getInstance() .visionBarcodeDetector // Or, to specify the formats to recognize: // val detector = FirebaseVision.getInstance() // .getVisionBarcodeDetector(options)
आखिर में, इमेज को
detectInImage
तरीके से पास करें:Java
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image) .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() { @Override public void onSuccess(List<FirebaseVisionBarcode> barcodes) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
Kotlin+KTX
val result = detector.detectInImage(image) .addOnSuccessListener { barcodes -> // Task completed successfully // ... } .addOnFailureListener { // Task failed with an exception // ... }
3. बारकोड से जानकारी पाएं
अगर बारकोड की पहचान करने की कार्रवाई पूरी हो जाती है, तो सक्सेस लिसनर कोFirebaseVisionBarcode
ऑब्जेक्ट की सूची पास कर दी जाएगी. हर FirebaseVisionBarcode
ऑब्जेक्ट, इमेज में मिले बारकोड को दिखाता है. हर बारकोड के लिए, इनपुट इमेज में उसके बाउंडिंग कोऑर्डिनेट और
बारकोड से एन्कोड किया गया रॉ डेटा भी देखा जा सकता है. साथ ही, अगर बारकोड डिटेक्टर, बारकोड से एन्कोड किए गए डेटा के टाइप का पता लगा पाता है, तो आपको पार्स किया गया डेटा वाला ऑब्जेक्ट मिल सकता है.
उदाहरण के लिए:
Java
for (FirebaseVisionBarcode 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 FirebaseVisionBarcode.TYPE_WIFI: String ssid = barcode.getWifi().getSsid(); String password = barcode.getWifi().getPassword(); int type = barcode.getWifi().getEncryptionType(); break; case FirebaseVisionBarcode.TYPE_URL: String title = barcode.getUrl().getTitle(); String url = barcode.getUrl().getUrl(); break; } }
Kotlin+KTX
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) { FirebaseVisionBarcode.TYPE_WIFI -> { val ssid = barcode.wifi!!.ssid val password = barcode.wifi!!.password val type = barcode.wifi!!.encryptionType } FirebaseVisionBarcode.TYPE_URL -> { val title = barcode.url!!.title val url = barcode.url!!.url } } }
रीयल-टाइम परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह
अगर आपको रीयल-टाइम ऐप्लिकेशन में बारकोड स्कैन करने हैं, तो सबसे सही फ़्रेमरेट पाने के लिए इन दिशा-निर्देशों का पालन करें:
-
कैमरे के मूल रिज़ॉल्यूशन पर इनपुट कैप्चर न करें. कुछ डिवाइसों में, नेटिव रिज़ॉल्यूशन पर इनपुट कैप्चर करने से बहुत बड़ी (10+ मेगापिक्सल) इमेज बनती हैं. इस वजह से, इंतज़ार का समय बहुत कम हो जाता है और इसे सटीक बनाने का कोई फ़ायदा नहीं होता. इसके बजाय, कैमरे से सिर्फ़ उस साइज़ का अनुरोध करें जो बारकोड की पहचान करने के लिए ज़रूरी है. आम तौर पर, साइज़ दो मेगापिक्सल से ज़्यादा नहीं होना चाहिए.
अगर स्कैन करने की स्पीड ज़रूरी है, तो इमेज कैप्चर के रिज़ॉल्यूशन को और कम किया जा सकता है. हालांकि, बारकोड साइज़ की कम से कम ज़रूरी शर्तों का ध्यान रखें. इनके बारे में ऊपर बताया गया है.
- डिटेक्टर को कॉल थ्रॉटल करें. अगर डिटेक्टर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें.
- अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, डिटेक्टर के आउटपुट का इस्तेमाल किया जाता है, तो सबसे पहले एमएल किट का इस्तेमाल करें. इसके बाद, एक ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, हर इनपुट फ़्रेम के लिए आपको डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर करना होगा.
-
अगर Camera2 API का इस्तेमाल किया जा रहा है, तो
ImageFormat.YUV_420_888
फ़ॉर्मैट में इमेज कैप्चर करें.अगर पुराने Camera API का इस्तेमाल किया जा रहा है, तो
ImageFormat.NV21
फ़ॉर्मैट में इमेज कैप्चर करें.