Javascript native barcode detector API

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today I'm looking at a super cool new API, the Barcode detector API. This is now shipped as a stable version since Chrome 83.
Be aware it's not a fully supported API by all browsers yet.
The Barcode detector API, as its name suggests can detect barcodes of several formats from an image or video source.
The end result for today will look like this:

Note: You can find a full demo below
Using the barcode API is actually pretty simple. We can create a detector like this:
// Plain one:
const barcodeDetector = new BarcodeDetector();
// Specific format one:
const barcodeDetector = new BarcodeDetector({
formats: [
'aztec',
'code_128',
'code_39',
'code_93',
'codabar',
'data_matrix',
'ean_13',
'ean_8',
'itf',
'pdf417',
'qr_code',
'upc_a',
'upc_e'
]
});
As you can see we can pass an array of formats we want to be scanning for, this might come in handy if you are only looking for one type of barcode.
Then we can simply call the detect function and pass an image stream.
try {
const barcodes = await barcodeDetector.detect(image);
barcodes.forEach(barcode => doSomething(barcode));
} catch (e) {
console.error('Barcode detection failed:', e);
}
In our case, we will be using a fixed image to detect the barcode.
<img
src="https://cdn.hashnode.com/res/hashnode/image/upload/v1619338701344/-rJKsBrhI.png"
crossorigin
alt="QR Coden Daily Dev Tips"
/>
And now we can simply create a barcode detector that will use this image, and output all data in a newly created pre tag.
const barcodeDetector = new BarcodeDetector();
const image = document.querySelector('img');
barcodeDetector
.detect(image)
.then(barcodes => {
let pre = document.createElement('pre');
pre.innerHTML = JSON.stringify(barcodes, null, 2);
image.after(pre);
})
.catch(console.error);
And it gives us the result of a bounding box, corner-points, and the actual value.
You can try this out on the following Codepen.
As mentioned the API is still in progress being rolled out, as of Chrome 83 and Edge 82 we can use it. However, Firefox does not yet support it.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter