Barcode detection using Shape Detection API

The other week I talked about Face Detection via the Shape API that is in the Canary channel in Chrome. Now barcode detection is in Chrome Canary too (Miguel is my hero ;)

Barcodes are huge! they are on nearly every product we buy. Even the much maligned QRCode is huge outside of the US and Europe. The barcode and the QRcode provide a simple way for you to bridge the physical world and the digital world by transferring small amounts of data between the medium and you. This might not have been a huge amount of use in the era of the desktop, in the era of mobile it is critical. You should never have to install an app just to get access to this data.

The Shape Detection API is interesting because it creates a standard interface on top of some underlaying hardware features on the user's device and opens up a new set of capabilities to the web platform, primarily Face Detection and barcode detection.

The barcode detection API is built upon the Shape Detection API that is currently in the WICG which means it is in an incubation and experimentation phase. On Android you can detect a number of different 1D and 2D barcodes:

1D barcodes: EAN-13, EAN-8, UPC-A, UPC-E, Code-39, Code-93, Code-128, ITF, Codabar

2D barcodes: QR Code, Data Matrix, PDF-417, AZTEC

Furthermore:

It automatically parses QR Codes, Data Matrix, PDF-417, and Aztec values, for the following supported formats:

  • URL
  • Contact information (VCARD, etc.)
  • Calendar event
  • Email
  • Phone
  • SMS
  • ISBN
  • WiFi
  • Geo-location (latitude and longitude)
  • AAMVA driver license/ID

The Shape Detection API is currently in Chrome Canary (M57) and you need to enable it via chrome://flags/#enable-experimental-web-platform-features

Like with face detection, the API is relatively simple to use. You invoke the API via detect on the BarcodeDetector API and you get back a promise that resolves to a list of decoded barcodes.

var barcodeDetector = new BarcodeDetector();
barcodeDetector.detect(image)
  .then(barcodes => {
    barcodes.forEach(barcode => console.log(barcodes.rawValue))
  })
  .catch((e) => {
    console.error("Boo, BarcodeDetection failed: " + e);
  });

It takes an image object (either a CanvasImageSource, Blob, ImageData or an <img> element) and then passes that to the underlying system API and it will return an array of DetectedBarcode objects that implement DetectedObject which essentially gives you the bounds of each face in the image.

I've also integrated it in to my QRCode Scanner Application but I am waiting for a fix to land that lets me pass in a Canvas or ImageData object into the API.

The interesting thing is that because I have already built this app in plain JS using the LazarSoft jsqrcode API I can detect the availability of native Barcode scanning and if it is not there then I fail back to the pure JS implementation.

Here are some videos of it in action:

I didn't mention it in the previous article, but this should also work on a worker thread (and consequentially inside a Service Worker). For my use-case this is brilliant because it allows me to delegate my logic in to another thread and keep everything away from the UI thread.

I think it is a very compelling addition to the web platform and I am excited to see this get used.

I lead the Chrome Developer Relations team at Google.

We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.

Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on web.dev, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.

I love to learn about what you are building, and how I can help with Chrome or Web development in general, so if you want to chat with me directly, please feel free to book a consultation.

I'm trialing a newsletter, you can subscribe below (thank you!)