Turning Raspberry Pi into a Barcode Scanner with Node.js and UVC Camera

Raspberry Pi is a versatile tool, especially in industrial settings. Imagine transforming it into a barcode scanner using just a camera. This guide is tailored for developers eager to craft Node.js barcode reader apps for IoT gadgets. Dive in to explore the power of Dynamsoft’s barcode SDK for ARM, and the steps to design a streamlined barcode application using JavaScript.


  • A UVC camera connected to Raspberry Pi.
  • Install Node.js modules: v4l2camera and barcode4nodejs:

      npm install v4l2camera barcode4nodejs

What’s the Frame Color Format of UVC Camera

The following code can get the frame color format of a UVC camera:

var v4l2camera = require("v4l2camera");
var dbr = require('barcode4nodejs');

var cam = new v4l2camera.Camera("/dev/video0");

// list all supported formats

v4l2 camera configuration

Mine is YUYV:


How to Read Barcodes from YUYV Frames in C++

To decode barcodes from YUYV, we simply utilize the Y values. Here is the code implemented in C++:

int width = worker->width, height = worker->height;
int size = width * height;
int index = 0;
unsigned char *data = new unsigned char[size];
// get Y from YUYV
for (int i = 0; i < size; i++)
  data[i] = worker->buffer[index];
  index += 2;
// read barcode
ret = DBR_DecodeBuffer(worker->handler, data, width, height, width, IPF_GRAYSCALED, "");
// release memory
delete[] data, data = NULL;

You can clone the source code from https://github.com/yushulx/nodejs-barcode and modify it to add more format support.

Node.js App for Barcode Detection on Raspberry Pi

  1. Get a 30-day free trial license.
  2. Create a camera_barcode_reader.js file.
  3. Add the following code to continuously capture frames from camera and decode barcodes:

     var dbr = require('barcode4nodejs');
     var barcodeTypes = dbr.barcodeTypes;
     var v4l2camera = require("v4l2camera");
     var cam = new v4l2camera.Camera("/dev/video0");
     // list all supported formats
     // set frame format as YUYV
     var format = cam.formats[0];
     if (cam.configGet().formatName !== "YUYV") {
         console.log("YUYV camera required");
     function capture() {
         cam.capture(function(success) {
             var frame = cam.frameRaw();
             dbr.decodeYUYVAsync(frame, format.width, format.height, barcodeTypes,
                 function(msg) {
                     var result = null;
                     for (index in msg) {
                         result = msg[index]
                         console.log("Format: " + result['format']);
                         console.log("Value : " + result['value']);
                     setTimeout(capture, 0);
                 }, "");
     setTimeout(capture, 0);
  4. Run camera_barcode_reader.js:

     node camera_barcode_reader.js

    node.js barcode scanner for raspberry pi

Source Code