In order to get the information from the barcode and trigger the action, We can use ML kit’s barcode scanning API.
Steps to implement Firebase ML kit
1. We have to add Firebase in the project.
2. Add the dependencies for the ML Kit Android libraries to your module (app-level) Gradle file (usually app/build.gradle)<
1 2 3 4 5 6 7 8 9 |
apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.1' implementation 'com.google.firebase:firebase-ml-vision-barcode-model:16.0.1' } |
Configure Barcode detector
It will be better to know the formate of the barcode in order to improve performance and getting the information
The following formats are supported:-Code 128 (FORMAT_CODE_128), Code 39 (FORMAT_CODE_39), Code 93 (FORMAT_CODE_93), Codabar (FORMAT_CODABAR), EAN-13, (FORMAT_EAN_13), Aztec (FORMAT_AZTEC), Data Matrix (FORMAT_DATA_MATRIX)
1 2 3 4 5 6 |
FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder() .setBarcodeFormats( FirebaseVisionBarcode.FORMAT_QR_CODE, FirebaseVisionBarcode.FORMAT_AZTEC) .build(); |
Run the Barcode Detector
1. Create FirebaseVisionImage object.
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(media_image, rotation);
We can also get the instance of FirebaseVisionImage object from a file URI.
1 2 3 4 5 6 |
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); } |
2. Create an instance of FirebaseVisionBarcodeDetector:
1 2 3 4 5 |
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance() .getVisionBarcodeDetector(); // Or, to specify the formats to recognize: // FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance() // .getVisionBarcodeDetector(options); |
3. Finally, pass the image to the “detectInImage” method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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 // ... } }); |
Get Information from barcodes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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; } } |
You can also go through the following link in order to get details:-
https://firebase.google.com/docs/ml-kit/android/read-barcodes
https://github.com/firebase/mlkit-material-android