In this article, we'll show you a very simple way to build a custom Node module that encapsulates the Dynamsoft Barcode Reader SDK, supports Windows, Linux, and OS X, We will also demonstrate how to integrate this module to implement an online barcode reading application.
More and more web developers are choosing Node to build a Web site because it is increasingly convenient to use JavaScript to develop complex server-side Web applications. To extend the functionality of node under different platforms, node allows developers to create extensions using C/D + +.
Introduced
Dynamsoft Barcode Reader provides a C + + shared library for barcode resolution for Windows, Linux, and OS X. Its greatest advantage is that it applies to a variety of advanced programming languages, including JavaScript, Python, Java, Ruby, PHP, and so on, as long as you can encapsulate the C + + API as an extension can be used. No matter what programming language, ultimately only a few lines of code to complete the interpretation of barcodes.
Support for 1d/2d barcode types
Code, code, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, Upc-a, upc-e,industrial 2 of 5
QRCode
Datamatrix
PDF417
Support Image Types
BMP, JPEG, PNG, GIF, TIFF, PDF
Operating Environment
Windows, Linux & Mac
Node v5.5.0
Node.js Barcode Extension
Node.js expands the shared objects that are dynamically linked by using C + +. If you don't have access to this technology, you can read the official tutorials.
Create an extension
Create a file named dbr.cc and add a method DecodeFile:
#include <node.h>
#include <string.h>
#include "if_dbr.h"
#include "BarcodeFormat.h"
Include "BarcodeStructs.h"
#include "ErrorCode.h"
using namespace V8;
void DecodeFile (const functioncallbackinfo<value>& args) {
}
void Init (handle<object> Exports) {
Node_set_method (exports, "DecodeFile", decodefile);
}
Node_module (DBR, Init)
Parse parameters from JavaScript Pass
isolate* isolate = Isolate::getcurrent ();
Handlescope scope (isolate);
String::utf8value License (args[0]->tostring ());
String::utf8value FileName (args[1]->tostring ());
char *pfilename = *filename;
char *pszlicense = *license;
__int64 Llformat = Args[2]->integervalue ();
Local<function> cb = Local<function>::cast (args[3]);
To parse a barcode image:
int imaxcount = 0x7fffffff;
Readeroptions ro = {0};
Pbarcoderesultarray presults = NULL;
Ro.llbarcodeformat = Llformat;
Ro.imaxbarcodesnumperpage = Imaxcount;
Dbr_initlicense (pszlicense);
Decode barcode image
int ret = Dbr_decodefile (Pfilename, &ro, &presults);
To turn a barcode into a string:
const char * GETFORMATSTR (__int64 format)
{
if (format = = code_39) return
"code_39";
if (format = = code_128) return
"code_128";
if (format = = code_93) return
"code_93";
if (format = = Codabar) return
"Codabar";
if (format = = ITF) return
"ITF";
if (format = = upc_a) return
"Upc_a";
if (format = = upc_e) return
"Upc_e";
if (format = = ean_13) return
"ean_13";
if (format = = ean_8) return
"Ean_8";
if (format = = industrial_25) return
"industrial_25";
if (format = = Qr_code) return
"Qr_code";
if (format = = PDF417) return
"PDF417";
if (format = = Datamatrix) return
"Datamatrix";
return "UNKNOWN";
}
To turn the results into V8 objects:
local<array> barcoderesults = array::new (isolate);
for (int i = 0; i < count; i++)
{
tmp = ppbarcodes[i];
local<object> result = object::new (isolate);
Result->set (String::newfromutf8 (Isolate, "format"), String::newfromutf8 (Isolate, Getformatstr (Tmp->llFormat )));
Result->set (String::newfromutf8 (Isolate, "value"), String::newfromutf8 (Isolate, tmp->pbarcodedata));
Barcoderesults->set (number::new (Isolate, i), result);
}
Building extensions
Requirements:
Windows: You need to install DBR for Windows, Visual Studio, and Python v2.7.
Linux: Install DBR for Linux.
Mac: Install DBR for Mac and Xcode.
Install Node-gyp:
NPM install-g Node-gyp
Create Binding.gyp for multiplatform compilation:
{"Targets": [{' Target_name ': "DBR", ' Sources ': ["dbr.cc"], ' conditions ': [[' os== ' Linux '], {' defines ': [' LINUX_DBR ',], ' include_dirs ': ["/home/xiao/dynamsoft/barcodereader4.0/include"], ' libraries ': ["-ldynamsoftbarcodereaderx64 ","-l/home/xiao/dynamsoft/barcodereader4.0/redist "], ' copies ': [{' Destination ': ' build/release/', ' Files ': ['/home/ Xiao/dynamsoft/barcodereader4.0/redist/libdynamsoftbarcodereaderx64.so ']}]}, [' os== ' win ', {' defines ': [' WINDOWS _dbr ',], ' include_dirs ': ["F:/program Files (x86)/dynamsoft/barcode Reader 4.1/components/c_c++/include"], ' Libraries ': [-lf:/program Files (x86)/dynamsoft/barcode Reader 4.1/components/c_c++/lib/dbrx64.lib "], ' copies ': [{' d Estination ': ' build/release/', ' Files ': [' F:/program files (x86)/dynamsoft/barcode Reader 4.1/components/c_c++/redist /dynamsoftbarcodereaderx64.dll ']}], [' os== ' mac ', {' defines ': [' MAC_DBR ',], ' include_dirs ': ['/applications/dyna Msoft/barcode/reader/4.1/include "], ' libraries ': ['-ldynamsoftbarcodereader ']}]}]}
Replace the DRB installation directory with the actual directory on your machine.
To configure the build environment:
Node-gyp Configure
You may encounter the following error on the MAC:
Error:xcode-select:error:tool ' Xcodebuild ' requires Xcode, but active developer directory '/library/developer/commandl Inetools ' is a command line tools instance
The solution is:
sudo xcode-select--switch/applications/xcode.app/contents/developer
Build Project:
NODE-GYP Build
Online Barcode Analysis
You have successfully built Node's barcode parsing module and can now create a simple barcode reader application.
Install Express and formidable:
NPM Install Express
NPM Install formidable
Use Express to create a simple application:
var formidable = require (' formidable ');
var util = require (' util ');
var express = require (' Express ');
var fs = require (' FS ');
var app = Express ();
var path = require (' path ');
var DBR = require ('./build/release/dbr ');
var http = require (' http ');
Fs.readfile ('./license.txt ', ' UTF8 ', function (err, data) {
app.use (express.static (__dirname));
App.use (function (req, res, next) {
Res.header ("Access-control-allow-origin", "*");
Res.header ("Access-control-allow-methods", "Put, POST, get, DELETE, Options");
Res.header ("Access-control-allow-headers", "X-requested-with, Content-type");
Res.header ("Access-control-allow-credentials", true);
Next ();
});
var server = App.listen (2016, function () {
var host = Server.address (). Address;
var port = server.address (). Port;
Console.log (' Listening at http://%s:%s ', host, port);
});
Use formidable to extract image data from a form:
App.post ('/upload ', function (req, res) {var form = new Formidable.
Incomingform (); Form.parse (req, Function (Err, fields, files) {var dir = ' uploads '; Fs.mkdir (dir, function (err) {var flag = Fields.upload
Flag;
var barcodetype = parseint (Fields.barcodetype);
Console.log (' flag: ' + flag); if (flag = = ' 1 ') {//Read barcode image file Fs.readfile (Files.fileToUpload.path, function (err, data) {//Save file Fro
M temp dir to new dir var fileName = Path.join (__dirname, dir, files.fileToUpload.name);
Console.log (FileName);
Fs.writefile (fileName, data, function (ERR) {if (err) throw err;
});
}); else {//read barcode image URL var tmpfilename = Path.join (__dirname, dir, ' tmp.jpg '); var tmp = Fs.createwritestream (
Tmpfilename);
var url = fields.filetodownload;
Console.log (' url: ' + URL);
Http.get (URL, function (response) {response.pipe (TMP); Tmp.on (' Finish ', function () {Tmp.close (function () {});};});
}
});
});
});
Import Barcode module to parse image file:
Decodebarcode (res, license, Tmpfilename, Barcodetype);
Run Application:
Node Server.js
Visit http://localhost:2016/index.htm:
The above is a small set to introduce the Node.js barcode recognition Program to build a detailed idea, I hope to help.