Reputation: 51
I'm having trouble with ML Kit Barcode Scanner. When I try to decode a sample QR code,
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qr_code_sample);
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
FirebaseVisionBarcodeDetector detector = FirebaseVision.getInstance().getVisionBarcodeDetector();
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image)
.addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionBarcode>>() {
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {
for (FirebaseVisionBarcode barcode:barcodes) {
Log.e("Log", "QR Code: "+barcode.getUrl().getUrl());
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Log", "Decode QR Code failed");
}
});
The output is like this:
QR Code: ""
How to solve this problem?
Upvotes: 5
Views: 3103
Reputation: 1
To extract title and url from barcode, you need to have Url Bookmark inside barcode, not just Url.
Raw Data of barcode that contains url bookmark would look something like this: MEBKM:TITLE:MyBookmark;URL:www.google.com;;
When you use ML KIT to scan barcode that consists of url only you get Raw Data like this: www.google.com
So to be able to extract title and url data from object of type FirebaseVisionBarcode.UrlBookmark you need to have those data inside that object.
Try to generate QR code here: https://www.montreallisting.ca/article/qr-code-quick-response-scan-mobile-android-iphone-blackberry/ and then use that picture to extract data you want and you will see as difference.
Upvotes: 0
Reputation: 11326
According to the API Reference, the getUrl()
is:
set iff getValueType() is TYPE_URL
So your barcode is probably not an URL/Bookmark, or ML Kit does not recognize it as such.
I recommend printing these 3 values:
@Override
public void onSuccess(List<FirebaseVisionBarcode> barcodes) {
for (FirebaseVisionBarcode barcode:barcodes) {
Log.e("Log", "QR Code: "+barcode.getDisplayValue()); //Returns barcode value in a user-friendly format.
Log.e("Log", "Raw Value: "+barcode.getRawValue());//Returns barcode value as it was encoded in the barcode.
Log.e("Log", "Code Type: "+barcode.getValueType()); //This will tell you the type of your barcode
}
}
You'll probably find your desired output in one of the first 2 lines. The third line tells you what type is the barcode you've scanned.
Upvotes: 2