How to read barcodes

There are a few options for reading bar codes: for web apps, for Phonegap apps and using an external scanner. Let’s look at each one and see what works.

1. Web App: Bar codes cannot be read directly using web apps. The integration with the camera is just not good enough. However, you can get another app to read the barcode for you and return the result.

Pic2Shop is one of these. It runs on iOS. You need to install it on your device separately. Then, you can ask it to scan by doing:

location="pic2shop://scan?callback=" + location.href

The “pic2shop:” in the above code is a URL Scheme. There are hundreds of these, plus individual apps can define their own. Pic2Shop’s is simple: you ask it to scan and give it the URL of your app so it knows where to return the result. After the scan is complete, your app is called again with the barcode. You can catch result in Sub Main:

Sub Main()
  'If we are returning from Pic2Scan, ean will be set.
  ean=GetURLParameter("ean")
  If Len(ean)>1 Then
    Textarea1.value="EAN scanned is " & ean
  End If
End Sub

See the ‘BarCode’ sample in the Web Services folder. (Both BASIC and Javascript samples are available.)

2. Hybrid App: Use this if you are compiling your app using PhoneGap. Include the PhoneGap BarCode plugin to your app, then do this:

Function butBarcode_onclick()
  cordova.plugins.barcodeScanner.scan(BarCodeSuccess, BarCodeFail)
End Function

Function BarCodeSuccess(result) 
  TextArea1.value = "We got a barcode" & vbCRLF & _
    "Result: " & result.text & vbCRLF & _
    "Format: " & result.format & vbCRLF & _
    "Cancelled: " & result.cancelled
End Function

Function BarCodeFail(Error) 
  TextArea1.value = "Scanning failed: " & Error
End Function

See the “PhoneGapAPI” sample in the “PhoneGap and EXE” folder. (Both BASIC and Javascript samples are available.)

Update: In May 2016, a major update was released for this plugin. Make sure you are using at least version 6.1 of PhoneGap. You can do this by putting ‘cli-6.1.0’ into ‘version’ in the PhoneGap group in your Project Properties.

3. Hardware (suggested by Lee Church): If you want barcode scanning that is slow, bad on first-time read rates, and not easy to integrate into AppStudio, stick with the camera and whatever add-on app you want. If you want reliable, fast, and easy to use, you’ll need an external scanner with dedicated scan engine. The easiest ones to integrate come from Socket Mobile. They comes in models that use Bluetooth and can emulate keyboard entry.

In one of my apps I set up the scanner to prefix barcode data with a special code, and suffix it as well. Then all keyboard entry on a specific form is interrogated for the start and stop characters; if they exist in the string then I know it’s a barcode.

That app is typically set up with the mode 8 (https://www.socketmobile.com/products/series-8-attachable/overview, about $200 on Amazon). The other great thing is that my code is exactly the same in iOS and Android, and can be run as a web app or compiled with Phonegap.

One case:

I have the scanner set to prefix any barcode data with a “*” and suffix it with a “-“. So the scanner sends “*1234567890-“ for a barcode of “1234567890”

I test for those characters and the length of what I received:

  
If left(field.value,1) = "*"
  If Right(field.value,1) = "-" Then ' you could test using the ASCII value or the text
    If len(field.value) >= 12 Then
      If Len(field.value) <= 18 Then
      ' length of barcode is between 10 and 16, 
      '   with correct prefix and suffix,  
      '   so good barcode (based upon UPC)
      ' Look up the barcode number in the Items table to see 
      '   if this is a valid item to sell (not a scan of a Coke can)
      ' process the barcode

This is air code, to help you understand the logic.

To make setting the prefix and suffix easy, support at Socket created a custom barcode that is scanned on the unit when a new scanner is set up for the first time, or on scanner reset. The prefix/suffix were chosen because I wanted characters that a user would not typically input manually, but could input if need be.

All of this takes less than a second, from scan to validation and lookup in an item table that typically has several thousand items (assuming a SQLite database on the device). User's experience shows the Socket 8 has a first-time read rate of over 90%; that means 9 out 10 time one scan is all it takes to get a good read.

When we tested with camera scanners and code, first-time read rates were down around 40% and barcode decode, validation and lookup took a couple of seconds. Users were not impressed!