How to Build Node.js Barcode Reader on Raspberry Pi

Node.js is available for all platforms including IoT devices like Raspberry Pi. In this post, I will demonstrate how to quickly create an online barcode reader on Raspberry Pi using Node.js and Dynamsoft Barcode Reader SDK.

Node.js for Raspberry Pi

If you flashed Rasbian Jessie, the latest Linux ROM for Raspberry Pi, you might have noticed that Node.js is pre-installed by default. Nevertheless, the Node version is out of date. To get new features of Node.js, download the latest version.

Here are the steps to install Node.js for Raspberry Pi:

  1. Remove the pre-installed Node.js:

     sudo apt-get remove node
    
  2. Check Pi hardware information:

     uname –m
     armv7l
    
  3. Download the matched Node version for ARM.
  4. Extract the package:

     tar –xf node-v4.4.1-linux-armv7l.tar.xz
    
  5. Add the bin path to ~/.bashrc:

     vim ~/.bashrc
     export PATH=$PATH:/home/pi/node-v4.4.0-linux-armv7l/bin
     source ~/.bashrc
    
  6. Check whether Node can work:

     node –v
     v4.4.0
    

Building Online Barcode Reader

Download Dynamsoft Barcode Reader 4.2 for Raspberry Pi.

Extract the package and generate a symbolic link:

sudo ln -s <DynamsoftBarcodeReader>/lib/libDynamsoftBarcodeReader.so /usr/lib/libDynamsoftBarcodeReader.so

Install node-gyp for building Node C/C++ addons:

npm install -g node-gyp

Create dbr.cc and binding.gyp:

        
    // dbr.cc
    Isolate* isolate = Isolate::GetCurrent();
	HandleScope scope(isolate);

	// convert v8 string to char *
	String::Utf8Value license(args[0]->ToString());
	String::Utf8Value fileName(args[1]->ToString());
	char *pFileName = *fileName;
	char *pszLicense = *license;
	// Dynamsoft Barcode Reader: init
	__int64 llFormat = args[2]->IntegerValue();
	int iMaxCount = 0x7FFFFFFF;
	ReaderOptions ro = {0};
	pBarcodeResultArray pResults = NULL;

	// Initialize license
	if (pszLicense && (strcmp(pszLicense, "null") !=0) )
	{
		printf("license: %s\n", pszLicense);
		DBR_InitLicense(pszLicense);
	}
	else {
		#ifdef LINUX_DBR
				printf("Linux dbr license\n");
				DBR_InitLicense("<license>");
		#endif
	}

	ro.llBarcodeFormat = llFormat;
	ro.iMaxBarcodesNumPerPage = iMaxCount;

	// Decode barcode image
	int ret = DBR_DecodeFile(pFileName, &ro, &pResults);
// binding.gyp
{
  "targets": [
    {
      'target_name': "dbr",
      'sources': [ "dbr.cc" ],
      'conditions': [
          ['OS=="linux"', {
            'defines': [
              'LINUX_DBR',
            ],
            'include_dirs': [
                "/home/pi/Desktop/dbr/include"
            ],
            'libraries': [
                "-lDynamsoftBarcodeReader", "-L/home/pi/Desktop/dbr/lib"
            ]
          }]
      ]
    }
  ]
}

Build dbr.so:

node-gyp build

Install Node modules express and formidable for rapidly making a Web project:

npm install express formidable

Use the barcode module in server.js:

var dbr = require('./build/Release/dbr');

dbr.decodeFile(
    license, fileName, barcodeType,
    function(msg) {
        var response = 'Totol count: ' + msg.length;

        var result = null;

        for (index in msg) {
            result = msg[index]
            response += '<p>' + result['format'] + ': ';
            response += result['value'] + '<p>';
        }
        res.send(response);
    }
);

Start the server:

node server.js

Visit http://localhost:2016/index.htm:

raspberry pi node.js barcode reader

Source Code

https://github.com/dynamsoftlabs/raspberry-pi-nodejs-barcode-reader-