"QuaggaJS Barcode Scanner Example: Real-Time Scanning in JavaScript"

Integrating QuaggaJS Barcode Scanner: A Step-by-Step Example

In the realm of web development, barcode scanning has become increasingly prevalent, enabling seamless data entry and streamlined workflows. QuaggaJS is a popular JavaScript library that brings barcode scanning capabilities to the browser, making it an excellent choice for web-based applications. Let's dive into an example of integrating QuaggaJS barcode scanner into a simple HTML page.

Setting Up the Environment

Before we begin, ensure you have a basic understanding of HTML, CSS, and JavaScript. You'll also need a modern web browser that supports the required APIs. For this example, we'll use the following files and folders:

  • index.html
  • styles.css
  • script.js
  • quagga.min.js (Download from QuaggaJS website)

Incorporating QuaggaJS into Your Project

First, include the QuaggaJS library in your HTML file. Add the following script tag just before the closing `` tag in your `index.html` file:

a bar code is shown in black and white, with the word written on it
a bar code is shown in black and white, with the word written on it

<script src="quagga.min.js"></script>

Setting Up the Video Stream

QuaggaJS requires a video stream to scan barcodes. We'll use the HTML5 `video` tag to display the stream. Update your `index.html` file as follows:

<video id="videoInput" width="600" height="400" playsinline autoplay></video>

Initializing QuaggaJS

Now, let's initialize QuaggaJS in our `script.js` file. Add the following code to start the barcode scanner:

Quagga.init({
  inputStream: {
    name: "Live",
    type: "LiveStream",
    target: document.querySelector("#videoInput"),
  },
  decoder: {
    readers: ["code_128_reader"],
  },
}, function (err) {
  if (err) {
    console.log(err);
    return;
  }
  Quagga.start();
});

Handling Scanned Barcodes

To handle scanned barcodes, add an event listener for the `processed` event, which triggers whenever a barcode is detected:

Barcode Scanner On Android And iPhone Last Mile Routing App
Barcode Scanner On Android And iPhone Last Mile Routing App

Quagga.onProcessed(function (result) {
  const drawingCtx = Quagga.canvas.context("drawing");
  const drawingCanvas = Quagga.canvas.dom("drawing");
  if (result) {
    if (result.boxes) {
      drawingCtx.clearRect(0, 0, parseInt(drawingCanvas.getAttribute("width")), parseInt(drawingCanvas.getAttribute("height")));
      result.boxes.forEach(function (box) {
        Quagga.Draw.onCanvas(drawingCtx, box, { color: "green", lineWidth: 2 });
      });
    }
    if (result decoded) {
      console.log(decoded.codeResult.code);
      // Add your custom logic here, e.g., update a form field or send data to a server
    }
  }
});

Styling the Video Stream

To make the video stream more visually appealing, add some basic styling in your `styles.css` file:

#videoInput {
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}

Running the Example

With everything set up, open your `index.html` file in a web browser, grant the necessary permissions for accessing your webcam, and you should now see a live video stream with a green box drawn around any scanned barcodes. The decoded barcode value will be logged to the console.

That's it! You've successfully integrated QuaggaJS barcode scanner into your web application. From here, you can build upon this example to create more advanced features, such as integrating the scanned data with a form or sending it to a server.

SCAN ME!!
SCAN ME!!
QR Code Barcode Scanners 2D-Code PNG
QR Code Barcode Scanners 2D-Code PNG
the barcode is black and white, but it's not very large enough to read
the barcode is black and white, but it's not very large enough to read
sticker
sticker
the barcode logo is black and white with an extra large barcode on it
the barcode logo is black and white with an extra large barcode on it
a bar code on a white background with black and white bars in the bottom right corner
a bar code on a white background with black and white bars in the bottom right corner
Abstract digital code scanner barcode template for social media, payment market and design
Abstract digital code scanner barcode template for social media, payment market and design
An OpenCV barcode and QR code scanner with ZBar - PyImageSearch
An OpenCV barcode and QR code scanner with ZBar - PyImageSearch
Mobile QR Scanner UI Design
Mobile QR Scanner UI Design
a bar code that is black and white
a bar code that is black and white
QR Code Scanner Pro for BlackBerry
QR Code Scanner Pro for BlackBerry
a bar code is shown in black and white
a bar code is shown in black and white
qr code sticker png | Download premium png of Png black barcode element, transparent background
qr code sticker png | Download premium png of Png black barcode element, transparent background
𓆤ּ ་༝ ּ̟ ݂ ࣪ tᥱ꧑ρᥣᥲtᥱ ιᥴ᥆ᥒ᥉ 𓄲 Ꮺ ָ࣪ 🐇۰
𓆤ּ ་༝ ּ̟ ݂ ࣪ tᥱ꧑ρᥣᥲtᥱ ιᥴ᥆ᥒ᥉ 𓄲 Ꮺ ָ࣪ 🐇۰
a barcode pattern that is black and white
a barcode pattern that is black and white
The 7 Best Barcode Scanner Apps
The 7 Best Barcode Scanner Apps
an orange and white qr code on a white background
an orange and white qr code on a white background
the barcode is green and white with an arrow pointing to it's left
the barcode is green and white with an arrow pointing to it's left
Create a Barcode Scanner App in just 3 Minutes with Xamarin.Forms
Create a Barcode Scanner App in just 3 Minutes with Xamarin.Forms
Best free Barcode Generator software and online tools for PC
Best free Barcode Generator software and online tools for PC
the barcode scanner is open to scan and save
the barcode scanner is open to scan and save
a qr code that says if you can read this, you really should be doing something about it
a qr code that says if you can read this, you really should be doing something about it
How to Select the Right Fixed Barcode Scanner for Your Business in 2025
How to Select the Right Fixed Barcode Scanner for Your Business in 2025
스케너
스케너