您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Node.js 中怎么實(shí)現(xiàn)一個(gè)條形碼識別程序,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Node.js 擴(kuò)展使用 C/C++ 編寫的動態(tài)鏈接的共享對象。如果你沒有接觸過這方面的技術(shù),可以閱讀 官方教程 。
創(chuàng)建名為 dbr.cc 的文件,并添加方法 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) { } //在此我向大家推薦一個(gè)前端全棧開發(fā)交流圈:619586920 突破技術(shù)瓶頸,提升思維能力 void Init(Handle<Object> exports) { NODE_SET_METHOD(exports, "decodeFile", DecodeFile); } NODE_MODULE(dbr, Init)
解析來自 JavaScript 傳遞過來的參數(shù)
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]);
解析條形碼圖像:
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);
將條形碼轉(zhuǎn)成字符串:
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"; }
將結(jié)果轉(zhuǎn)成 v8 對象:
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); } //在此我向大家推薦一個(gè)前端全棧開發(fā)交流圈:619586920 突破技術(shù)瓶頸,提升思維能力
要求:
Windows: 需要安裝 DBR for Windows, Visual Studio, and Python .
Linux: 安裝 DBR for Linux.
Mac: 安裝 DBR for Mac 和 Xcode.
安裝 node-gyp:
npm install -g node-gyp
創(chuàng)建 binding.gyp 用于多平臺編譯:
{ "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" ] }] ] } //在此我向大家推薦一個(gè)前端全棧開發(fā)交流圈:619586920 突破技術(shù)瓶頸,提升思維能力 ] }
將 DRB 安裝目錄替換成你機(jī)器上的實(shí)際目錄。
配置構(gòu)建環(huán)境:
node-gyp configure</pre>
可以在 Mac 上你會碰到下面的錯(cuò)誤:
error: xcodeselect: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
解決辦法是:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
構(gòu)建項(xiàng)目:
node-gyp build
你已經(jīng)成功的構(gòu)建了 Node 的條形碼解析模塊,現(xiàn)在可以創(chuàng)建一個(gè)簡單的條形碼讀取應(yīng)用。
安裝 Express 和 Formidable:
npm install express npm install formidable
使用 Express 創(chuàng)建一個(gè)簡單應(yīng)用:
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(2019, function() { var host = server.address().address; var port = server.address().port; console.log('listening at http://%s:%s', host, port); }); });
使用 Formidable 從表單中提取圖像數(shù)據(jù):
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() { }); }); }); } //在此我向大家推薦一個(gè)前端全棧開發(fā)交流圈:619586920 突破技術(shù)瓶頸,提升思維能力 }); }); });
導(dǎo)入條形碼模塊用來解析圖像文件:
decodeBarcode(res, license, tmpFileName, barcodeType);
運(yùn)行應(yīng)用:
node server.js
訪問 http://localhost:2019/index.htm:
上述就是小編為大家分享的Node.js 中怎么實(shí)現(xiàn)一個(gè)條形碼識別程序了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。