在這篇文章中,我們將展示一個非常簡單的方法來建立一個自訂的Node 模組,該模組封裝了Dynamsoft Barcode Reader SDK ,支援Windows、Linux 和OS X,同時我們將示範如何整合這塊模組實作一個線上的條碼讀取應用。
越來越多的 Web 開發者選擇 Node 來建立網站,因為使用 JavaScript 來開發複雜的伺服器端 Web 應用程式越來越便利。為了擴展在不同平台下的 Node 的功能,Node 允許開發者使用 C/C++ 來建立擴充。
介紹
Dynamsoft Barcode Reader 為 Windows、Linux 和 OS X 提供條碼解析的 C/C++ 共用程式庫。其最大的優點是適用於多種高階程式語言,包括 JavaScript, Python, Java, Ruby, PHP 等,只要可以封裝 C/C++ API 作為一個擴充功能就可以使用。不管是什麼程式語言,最終只需要簡單幾行程式碼即可完成條碼的解析。
支援1D/2D 條碼類型
Code 39, Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC- A, UPC-E,Industrial 2 of 5
QRCode
DataMatrix
PDF417
支援影像類型
支援影像類型支援圖片類型
運行環境
Windows, Linux & MacNode v5.5.0
Node.js v5.5.0#include#include#include "If_DBR.h" #include "BarcodeFormat.h" #include "BarcodeStructs.h" #include "ErrorCode.h" using namespace v8; void DecodeFile(const FunctionCallbackInfo& args) { } void Init(Handleexports) { NODE_SET_METHOD(exports, "decodeFile", DecodeFile); } NODE_MODULE(dbr, Init)
Node.js 擴充使用C/C++ 編寫的動態連結的共享物件。如果你沒有接觸過這方面的技術,你可以閱讀 官方教學 。
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(); Localcb = Local::Cast(args[3]);
建立一個名為dbr.cc 的文件,並新增方法DecodeFile:
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);
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"; }
LocalbarcodeResults = Array::New(isolate); for (int i = 0; i < count; i++) { tmp = ppBarcodes[i]; Localresult = 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); }
條碼轉成字串:
將結果轉成v8 物件:{ "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': [ { 'destination': '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/Dynamsoft/Barcode/ Reader/ 4.1/Include" ], 'libraries': [ "-lDynamsoftBarcodeReader" ] }] ] } ] }
建置擴充功能
需求:Windows: 需要安裝DBR for Windows, visual Studio, and Python v2.7.
Linux: 安裝DBR for Linux.
Mac: 安裝DBR for Mac 和Xcode.安裝node-gyp:
npm install -g node-gyp
建立binding.gyp 用於多平台編譯:
將DRB 安裝目錄替換成你機器上的實際目錄。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); }); });
error: xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
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.uploadFlag; 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 from 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() { }); }); }); } }); }); });
建置專案:
你已經成功的建構了Node 的條碼解析模組,現在可以建立一個簡單的條碼讀取應用程式。 安裝Express 和Formidable:
npm install express
npm install formidable
使用Express 創建一個簡單應用:
使用Formidable 從表單中擷取影像資料:
匯入條碼模組用來解析圖片檔案: