Razgriz
Razgriz

Reputation: 7343

QR Code Return Type Extraction in Android using ZXing

I have an app that can successfully retrive the data returned by a QR Code via sending an intent to the Barcode Scanner App made by ZXing.

However, I want to make the app better by being able to decipher if the format of the QR Code is a phone number. Is it possible to also obtain the format of the QR Code (ie URL, SMS, Phone number, text)?

I need this because I don't want loopholes like people creating their own QR Codes to scan. The QR Codes I created for a system I was asked to create has the phone number format. People may be able to do counterfeit QR Codes that have Text as the format. I want the app to be able to differentiate between the formats so this can be avoided. Thank you!

Upvotes: 0

Views: 4756

Answers (4)

Prasenjeet Paul
Prasenjeet Paul

Reputation: 121

Try using this:

String codeType = ResultParser.parseResult(result).getType().toString();

The ResultParser class will give you the type of QR Code's Result i.e.: URL, TEXT, MSG, etc.

You can use the below code to compare the types:

if(ResultParser.parseResult(result).getType() == ParsedResultType.ADDRESSBOOK) {
    // Implement your logic
}

Instead it is always appreciated id you use enum type rather than string i.e., use ParsedResultType.ADDRESSBOOK instead of String type = "addressbook"

Upvotes: 1

STT
STT

Reputation: 274

You can use the following to get the QR Code type:

String type = resultHandler.getResult().getType().toString();

This gives you the type of QR Code. For e.g. URI, TEL, SMS, etc.

Hope this helps.

Upvotes: 0

Sean Owen
Sean Owen

Reputation: 66896

If you really mean the barcode format, this is also returned in the Intent. Look at extra SCAN_RESULT_FORMAT which will have a String like "QR_CODE". These are names of enum values in BarcodeFormat.

If you mean you want to parse the result of the scan like Barcode Scanner does, use the ResultParser class from core/.

I don't see how any of this prevents making up your own barcode though.

Upvotes: 3

Christine
Christine

Reputation: 5575

I would use a regular expression to test for the format. Depending on the type of strings you want to test, there should be plenty of examples online. Here's more info on regular expressions: http://www.regular-expressions.info/reference.html Java (Android) has builtin regex functions ("matcher" and "pattern")

Upvotes: -1

Related Questions