Detecting barcode from the browser!!!
Tags: webdev, javascript, tutorial, html
Published: 2021-11-06 23:46:56

## Background

I have just arranged for my first ever concert that is going to happen in mid-December in Hong Kong. As I was sorting out the logistics for ticket sales, I decided to build a simple system for myself. People can go on the [stripe payment link](https://stripe.com/en-gb/payments/payment-links) to purchase the ticket. Then a webhook will be triggered which generates an email with a QR code. The QR code then will be scanned for entry on the day of concert at the door. As I was looking for solution for QR code detection, I discovered this beautiful [Barcode Detection API](https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API)

I didn't see many tutorials about it yet, so I have decided to make a note about my exploration. Hope you will enjoy!

## Article Synopsis

This article will split into 2 parts:

1. Getting feed from camera
2. Detect barcode from camera feed


## Part 1: Getting feed from camera

In this section, our goal is to put the camera stream onto the page.

First, we need a `<video>` element to show the camera stream.

```html
<video id="stream" style="width: 100vw; height: 100vh;" />
```

Then, we can simply use [`getUserMedia`](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) to grab the media stream; pass that stream directly to the video element.

```js
const stream = await navigator.mediaDevices.getUserMedia({
  video: {
    facingMode: { ideal: 'environment' }
  },
  audio: false
});
const videoEl = document.querySelector('#stream');
videoEl.srcObject = stream;
await videoEl.play();
```

Note that, the `{ ideal: 'environment' }` option is provided so the back camera on a phone is used. Find out more [here](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamConstraints/video).

With these few lines of code, we capture the camera feed and displayed it on the screen. [See codepen](https://codepen.io/ycmjason/pen/PoKRWpp).

## Part 2: Detect barcode from camera feed

The [barcode detection api](https://developer.mozilla.org/en-US/docs/Web/API/Barcode_Detection_API) provides a simple API for barcode detection. All you need is `new BarcodeDetector(...)` then `barcodeDetector.detect(videoElement)`.

So we will add these two lines:

```js
/* code from part one */


const barcodeDetector = new BarcodeDetector({formats: ['qr_code']});
const barcodes = await barcodeDetector.detect(videoEl)
```

Now the barcode detector will be activated at the exact moment the video starts streaming. We probably won't expect to find any QR code at the moment people turn on their camera. So we will need to continuously look at the video stream and call `.detect(...)` until we got some barcodes.

In order to do that, we can `.detect` every X ms until we got some barcodes. Simply use `window.setInterval` for this.


```js
/* code from part one */

const barcodeDetector = new BarcodeDetector({formats: ['qr_code']});
window.setInterval(async () => {
  const barcodes = await barcodeDetector.detect(videoEl);
  if (barcodes.length <= 0) return;
  alert(barcodes.map(barcode => barcode.rawValue));
}, 1000)
```

Now the camera will look for barcode every second! [See codepen](https://codepen.io/ycmjason/pen/rNzdjmP) and try to load up a QR code to test! Here is an QR code for "hello world".

![QR code for "hello world"](https://api.qrserver.com/v1/create-qr-code/?size=250x250&data=hello%20world)

## END

Final results ([codepen](https://codepen.io/ycmjason/pen/rNzdjmP)):

```html
<video id="stream" style="width: 100vw; height: 100vh;"/>
```

```js
(async () => {
const stream = await navigator.mediaDevices.getUserMedia({
  video: {
    facingMode: {
      ideal: "environment"
    }
  },
  audio: false
});
const videoEl = document.querySelector("#stream");
videoEl.srcObject = stream;
await videoEl.play();

const barcodeDetector = new BarcodeDetector({formats: ['qr_code']});
window.setInterval(async () => {
  const barcodes = await barcodeDetector.detect(videoEl);
  if (barcodes.length <= 0) return;
  alert(barcodes.map(barcode => barcode.rawValue));
}, 1000)
})();
```

Happy coding!