DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Barcode Detection Using the Shape Detection API

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?

Paul Kinlan user avatar by
Paul Kinlan
·
Dec. 09, 16 · Tutorial
Like (1)
Save
Tweet
Share
6.34K Views

Join the DZone community and get the full member experience.

Join For Free

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();
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. 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 Barcode

Published at DZone with permission of Paul Kinlan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Three SQL Keywords in QuestDB for Finding Missing Data
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Best Practices for Writing Clean and Maintainable Code
  • Silver Bullet or False Panacea? 3 Questions for Data Contracts

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: