Over a million developers have joined DZone.

Barcode Detection Using the Shape Detection API

DZone 's Guide to

Barcode Detection Using the Shape Detection API

Nearly everything that you buy has a barcode that has to be scanned, but have you ever wondered how barcodes are actually detected?

· Integration Zone ·
Free Resource

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, but 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 underlying hardware features on the user’s device, as well as 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 value, 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 or 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();
  .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. 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 into 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 into 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.

api ,integration ,shape detection ,barcodes

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}