Building a Barcode and QR Code Detection Module for Node-RED with JavaScript

Node-RED is a powerful flow-based programming tool built on Node.js that allows users to create applications by visually wiring together different nodes in a browser-based editor. In this article, you’ll learn how to create a custom barcode and QR code detection node using the Dynamsoft Barcode Reader, enhancing your Node-RED workflows with robust barcode scanning capabilities.

Prerequisites

  • Node.js: Download and install Node.js
  • Node-RED: Install Node-RED with the following commands:

      npm install -g --unsafe-perm node-red
      node-red
    

    When you run node-red for the first time, a folder named .node-red will be created in your home directory:

    • Windows: %userprofile%\.node-red
    • Linux: ~/.node-red

    If the default port 1880 is occupied, modify it in .node-red/settings.js to use a different port, such as 18800.

    node red

  • Dynamsoft Barcode Reader SDK: Get a free trial license

Building a Barcode Detection Module for Node-RED

To create the barcode detection module, we’ll generate a new project using npm and install the barcode4nodejs package, a high-performance wrapper for the Dynamsoft Barcode Reader C++ SDK.

npm init
npm install barcode4nodejs

In your package.json file, add a node-red section to specify the module configuration:

"node-red": {
    "nodes": {
      "barcode": "barcode.js"
    }
  },

This configuration tells Node-RED where to find the node module. In this setup, the module is located in the same directory as the package.json file, and barcode.js is the entry point.

module.exports = function (RED) {
  var dbr = require('barcode4nodejs');
  var barcodeTypes = dbr.formats.ALL;
  function BarcodeNode(config) {
    RED.nodes.createNode(this, config);
    this.license = config.license;
    this.template = config.template;
    var node = this;
    node.on('input', function (msg) {
      if (msg.filename && msg.filename.indexOf('base64') > -1) {
        var fs = require('fs');

        dbr.initLicense(node.license);
        fs.readFile(msg.filename, 'utf8', (err, data) => {

          dbr.decodeBase64Async(data, barcodeTypes, function (err, results) {
            msg.payload = results;
            node.send(msg);
          }, node.template);
        });
      }
      else if (msg.filename) {
        dbr.initLicense(node.license);
        dbr.decodeFileAsync(msg.filename, barcodeTypes, function (err, results) {
          msg.payload = results;
          node.send(msg);
        }, node.template);
      }
      else {
        msg.payload = msg.payload.toLowerCase();
        node.send(msg);
      }
    });
  }
  RED.nodes.registerType('barcode', BarcodeNode);
}

The barcode node reads the image filename from the input and uses the decodeFileAsync method to detect barcodes from the image file asynchronously. For base64 strings, it uses the decodeBase64Async method.

The UI of the module is defined in the barcode.html file.

<script type="text/javascript">
    RED.nodes.registerType('barcode',{
        category: 'Dynamsoft',
        color: '#a6bbcf',
        defaults: {
            name: {value:""},
            license: {value:""},
            template: {value: ""}
        },
        inputs:1,
        outputs:1,
        icon: "function.png",
        label: function() {
            return this.name||"barcode";
        }
    });
</script>

<script type="text/x-red" data-template-name="barcode">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Barcode Reader">
    </div>
    <div class="form-row">
		<label for="node-input-license"><i class="icon-tag"></i> License</label>
		<input type="text" id="node-input-license" placeholder="LICENSE-KEY">
    </div>
    <div class="form-row">
		<label for="node-input-template"><i class="icon-tag"></i> Template</label>
		<input type="text" id="node-input-template" placeholder="https://www.dynamsoft.com/barcode-reader/docs/core/parameters/structure-and-interfaces-of-parameters.html?ver=latest">
	</div>
</script>

<script type="text/x-red" data-help-name="barcode">
    <p>A simple node that read barcodes from image files.</p>
</script>

Testing the Node-RED Barcode Node

With the barcode node complete, you can test it locally:

  1. Install the local project to the home directory of Node-RED:

    Windows

     cd %userprofile%\.node-red
     npm install <the local project>
     node-red
    

    Linux

     cd ~/.node-red
     npm install <the local project>
     node-red
    
  2. In the Node-RED web editor, add the following nodes to your flow:
    • Inject Node
    • File Node
    • Barcode Node
    • Debug Node

    Configure the File Node to specify the path of the file you wish to read. Supported file types include images (PNG, JPG, BMP, GIF, TIFF, PDF) or files containing base64 strings. Enable the system console option in the Debug Node to view results.

    Node RED barcode

  3. Set the license key and customize the barcode parameter template in Barcode Node. If the parameter template is left empty, the default settings will be applied.

    node red barcode license

  4. Execute the Node-RED flow to see the barcode detection results in the console.

    node red barcode results

Source Code

https://github.com/yushulx/barcode4nodejs/tree/main/examples/node-red