Quantcast
Channel: Barcode – Dynamsoft Developers
Viewing all articles
Browse latest Browse all 145

Node.js Barcode Extension with DBR 6.0

$
0
0

When searching for barcode SDK online, you can find lots of free and commercial SDKs. However, there are only a few of SDKs cross-platform, which is why Dynamsoft Barcode Reader is outstanding. Dynamsoft Barcode Reader SDK supports Windows, Linux, macOS, Android, iOS, and Raspberry Pi. Currently, the latest DBR 6.0 is better and faster than the previous version. The imperfection is it only supports Windows so far. If you need an SDK available for all platforms, please choose DBR 5.2. The post will help developers create a Node.js barcode reader with DBR 6.0 on Windows.

Prerequisites

Creating Node.js Barcode Extension Using C/C++

Node-gyp

To build C/C++ code, install node-gyp:

npm install -g node-gyp

Create binding.gyp file:

{
  "targets": [
    {
      'target_name': "dbr",
      'sources': [ "dbr.cc" ],
      'conditions': [
          ['OS=="win"', {
            'defines': [
              'WINDOWS_DBR',
            ],
            'include_dirs': [
                "E:\\Program Files (x86)\\Dynamsoft\\Barcode Reader 6.0\\Components\\C_C++\\Include"
            ],
            'libraries': [
                "-lE:\\Program Files (x86)\\Dynamsoft\\Barcode Reader 6.0\\Components\\C_C++\\Lib\\DBRx64.lib"
            ],
            'copies': [
            {
              'destination': 'build/Release/',
              'files': [
                'E:\\Program Files (x86)\\Dynamsoft\\Barcode Reader 6.0\\Components\\C_C++\\Redist\\x64\\DynamsoftBarcodeReaderx64.dll'
              ]
            }]
          }]
      ]
    }
  ]
}
  • ‘dbr.cc’: the C/C++ source file.
  • ‘include_dir’: DynamsoftBarcodeReader.h header file directory.
  • ‘libraries’: linking DBRx64.lib.
  • ‘copies’: copy DynamsoftBarcodeReaderx64.dll to the output folder.

Build Node.js extension:

node-gyp configure
node-gyp build

C/C++ APIs

Create native methods:

#include <node.h>
#include <node_buffer.h>
#include <string.h>
#include <uv.h>
#include "DynamsoftBarcodeReader.h"

using namespace v8;

void Init(Handle<Object> exports) {
    NODE_SET_METHOD(exports, "create", Create);
    NODE_SET_METHOD(exports, "destroy", Destroy);
    NODE_SET_METHOD(exports, "decodeYUYVAsync", DecodeYUYVAsync);
    NODE_SET_METHOD(exports, "decodeFileStreamAsync", DecodeFileStreamAsync);
    NODE_SET_METHOD(exports, "initLicense", InitLicense);
    NODE_SET_METHOD(exports, "decodeFileAsync", DecodeFileAsync);
    NODE_SET_METHOD(exports, "loadTemplates", LoadTemplates);
}

NODE_MODULE(dbr, Init)

DBR 6.0 allows developers to create and load parameter templates using JSON files.

void LoadTemplates(const FunctionCallbackInfo<Value>& args) 
{
    if (!createDBR()) {return;}

    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    // Get arguments.
    String::Utf8Value fileName(args[0]->ToString()); // file name
    char *pszFileName = *fileName;

    // Load the template file.
    char szErrorMsg[256];
    DBR_LoadSettingsFromFile(hBarcode, pszFileName, szErrorMsg, 256);
}

Decode a file stream:

DBR_DecodeFileInMemory(hBarcode, worker->buffer, worker->size, worker->templateName);

Decode a file:

DBR_DecodeFile(hBarcode, worker->filename, worker->templateName);

How to detect barcodes

Here are the necessary steps of reading barcodes:

  1. Create an instance of the barcode reader.
  2. Set a valid license.
  3. Load a template.
  4. Invoke barcode reading APIs with a template name.
  5. Destroy the barcode reader.

Node.js Barcode Reader

Create a test.js file.

Get current directory to construct the template file path.

var templateFile = path.join(process.cwd(), 'templates', 'default.settings.json');

Initialize license and parameter template.

dbr.initLicense(
    "t0068MgAAACItg+NVEOPRujuNx/KXoEsTqRKBAkSCJjwJZZJt/fb7dYfsd/S86yrjQL0KvqHQ6wfh9/Xgu7MwiTqbEnAFFyA=");
dbr.loadTemplates(templateFile);

Read barcodes from an image file:

function decodeFileAsync(fileName) {
  dbr.decodeFileAsync(fileName, barcodeTypes, function(err, msg) {
    let result = null;
    for (index in msg) {
      result = msg[index];
      console.log("Format: " + result['format']);
      console.log("Value : " + result['value']);
      console.log("##################");
    }
  }, "CUSTOM");
}

Node.js barcode reader

Source Code

https://github.com/yushulx/nodejs-barcode-for-win-linux-mac/tree/DBR6.0

The post Node.js Barcode Extension with DBR 6.0 appeared first on Code Pool.


Viewing all articles
Browse latest Browse all 145

Trending Articles