翻譯|使用教程|編輯:楊鵬連|2020-07-17 16:55:57.553|閱讀 518 次
概述:上周,我分享了如何在桌面和網(wǎng)絡(luò)應(yīng)用中顯示網(wǎng)絡(luò)攝像頭視頻。在此基礎(chǔ)上,我使用Node.js實(shí)現(xiàn)了條形碼讀取功能。在本文中,我將分享如何使用Dynamsoft Barcode Reader SDK為桌面和Web構(gòu)建Node.js條碼閱讀器應(yīng)用程序。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
Dynamic Web TWAIN是一個(gè)專為Web應(yīng)用程序設(shè)計(jì)的TWAIN掃描識(shí)別控件。你只需在TWAIN接口寫(xiě)幾行代碼,就可以用兼容TWAIN的掃描儀掃描文檔或從數(shù)碼相機(jī)/采集卡中獲取圖像。然后用戶可以編輯圖像并將圖像保存為多種格式,用戶可保存圖像到遠(yuǎn)程數(shù)據(jù)庫(kù)或者SharePoint。該TWAIN控件還支持上傳和處理本地圖像。
本文匯集了一些Dynamic Web TWAIN編程方面的常見(jiàn)問(wèn)題,并且針對(duì)這些問(wèn)題進(jìn)行了回答,感興趣的朋友快來(lái)了解一下吧~
點(diǎn)擊下載Dynamic Web TWAIN正式版
將Dynamsoft Barcode SDK的C / C ++ API與JavaScript綁定
讓我們開(kāi)始使用Node.js條碼C / C ++插件。
為了支持OpenCV Mat,我創(chuàng)建了一個(gè)新的API encodingBufferAsync()。C / C ++代碼如下:
void DecodeBufferAsync(const FunctionCallbackInfo第一個(gè)參數(shù)是Node.js緩沖區(qū)指針。您可以調(diào)用getData()從Mat獲取字節(jié)數(shù)組:& args) { if (!createDBR()) {return;} Isolate* isolate = Isolate::GetCurrent(); Local context = isolate->GetCurrentContext(); // get arguments unsigned char* buffer = (unsigned char*) node::Buffer::Data(args[0]); // file stream int width = args[1]->Int32Value(context).ToChecked(); // image width int height = args[2]->Int32Value(context).ToChecked(); // image height int stride = args[3]->Int32Value(context).ToChecked(); // stride int iFormat = args[4]->Int32Value(context).ToChecked(); // barcode types Local cb = Local ::Cast(args[5]); // javascript callback function String::Utf8Value templateName(isolate, args[6]); // template name char *pTemplateName = *templateName; // initialize BarcodeWorker BarcodeWorker *worker = new BarcodeWorker; worker->request.data = worker; worker->callback.Reset(isolate, cb); worker->iFormat = iFormat; worker->pResults = NULL; worker->buffer = buffer; worker->width = width; worker->height = height; worker->bufferType = RGB_BUFFER; worker->stride = stride; if (hasTemplate(pTemplateName)) { // Load the template. char szErrorMsg[256]; DBR_InitRuntimeSettingsWithString(hBarcode, pTemplateName, CM_OVERWRITE, szErrorMsg, 256); worker->useTemplate = true; } else { worker->useTemplate = false; } uv_queue_work(uv_default_loop(), &worker->request, (uv_work_cb)DetectionWorking, (uv_after_work_cb)DetectionDone); }
const vCap = new cv.VideoCapture(0); var img = vCap.read(); dbr.decodeBufferAsync(img.getData(), img.cols, img.rows, img.step, barcodeTypes, function (err, msg) { results = msg }, "");注意:macOS的構(gòu)建配置與binding.gyp文件中的Linux配置略有不同:
'copies': [ { 'destination': '/usr/local/lib/', 'files': [ './platforms/macos/libDynamsoftBarcodeReader.dylib' ] } ]與在Linux上不同,一旦構(gòu)建完成,動(dòng)態(tài)庫(kù)文件將被復(fù)制到/ usr / local / lib /目錄。原因是RPATH無(wú)法在macOS上運(yùn)行。我們可以使用“ otool -L dbr.node ”來(lái)檢查依賴庫(kù),然后獲取路徑/usr/local/lib/libDynamsoftBarcodeReader.dylib。
如果要將庫(kù)文件和dbr.node保留在同一文件夾中,則可以手動(dòng)更改庫(kù)路徑:
cd build/Release install_name_tool -change /usr/local/lib/libDynamsoftBarcodeReader.dylib @loader_path/libDynamsoftBarcodeReader.dylib dbr.node
我已經(jīng)將該軟件包發(fā)布到//fc6vip.cn/product/1313。要安裝該軟件包,您需要安裝C ++開(kāi)發(fā)工具,然后運(yùn)行:
npm install -g node-gyp npm install barcode4nodejs在5分鐘內(nèi)為桌面和Web構(gòu)建Node.js條形碼閱讀器
桌面
基本上,我們可以使用無(wú)限循環(huán)來(lái)捕獲攝像頭幀并將其顯示在窗口中:
const cv = require('opencv4nodejs'); const vCap = new cv.VideoCapture(0); const delay = 10; while (true) { let frame = vCap.read(); if (frame.empty) { vCap.reset(); frame = vCap.read(); } cv.imshow('OpenCV Node.js', frame); const key = cv.waitKey(delay); // Press ESC to quit if (key == 27) {break;} }但是,如果我們?cè)谘h(huán)中調(diào)用異步條形碼解碼功能,則回調(diào)函數(shù)將永遠(yuǎn)不會(huì)返回。為了使其工作,我們可以使用setTimeout() 代替:
const dbr = require('barcode4nodejs'); const cv = require('opencv4nodejs'); dbr.initLicense("LICENSE-KEY") barcodeTypes = dbr.barcodeTypes const vCap = new cv.VideoCapture(0); const drawParams = { color: new cv.Vec(0, 255, 0), thickness: 2 } const fontFace = cv.FONT_HERSHEY_SIMPLEX; const fontScale = 0.5; const textColor = new cv.Vec(255, 0, 0); const thickness = 2; results = null; function getframe() { let img = vCap.read(); dbr.decodeBufferAsync(img.getData(), img.cols, img.rows, img.step, barcodeTypes, function (err, msg) { results = msg }, "", 1); cv.imshow('Webcam', img); const key = cv.waitKey(10); // Press ESC to quit if (key != 27) { setTimeout(getframe, 30); } } getframe()由于連續(xù)的網(wǎng)絡(luò)攝像頭圖像相似,因此可以在不同的幀上繪制結(jié)果:
if (results != null) { for (index in results) { let result = results[index]; let upperLeft = new cv.Point(result.x1, result.y1) let bottomLeft = new cv.Point(result.x2, result.y2) let upperRight = new cv.Point(result.x3, result.y3) let bottomRight = new cv.Point(result.x4, result.y4) img.drawLine( upperLeft, bottomLeft, drawParams ) img.drawLine( bottomLeft, upperRight, drawParams ) img.drawLine( upperRight, bottomRight, drawParams ) img.drawLine( bottomRight, upperLeft, drawParams ) img.putText(result.value, new cv.Point(result.x1, result.y1 + 10), fontFace, fontScale, textColor, thickness); } }
網(wǎng)頁(yè)
將條形碼檢測(cè)代碼復(fù)制到web.js文件中:
function capture() { var frame = wCap.read() if (frame.empty) { wCap.reset(); frame = wCap.read(); } dbr.decodeBufferAsync(frame.getData(), frame.cols, frame.rows, frame.step, barcodeTypes, function (err, msg) { // console.log(results) results = msg }, "", 1); if (results != null) { for (index in results) { let result = results[index]; let upperLeft = new cv.Point(result.x1, result.y1) let bottomLeft = new cv.Point(result.x2, result.y2) let upperRight = new cv.Point(result.x3, result.y3) let bottomRight = new cv.Point(result.x4, result.y4) frame.drawLine( upperLeft, bottomLeft, drawParams ) frame.drawLine( bottomLeft, upperRight, drawParams ) frame.drawLine( upperRight, bottomRight, drawParams ) frame.drawLine( bottomRight, upperLeft, drawParams ) frame.putText(result.value, new cv.Point(result.x1, result.y1 + 10), fontFace, fontScale, textColor, thickness); } } img = cv.imencode('.jpg', frame); setTimeout(capture, 30); } capture();現(xiàn)在我們可以運(yùn)行服務(wù)器端條形碼檢測(cè)。它與任何Web瀏覽器完全兼容。這是Microsoft Internet Explorer的屏幕截圖。
Dynamic Web TWAIN的常見(jiàn)問(wèn)答到這里就結(jié)束了,對(duì)相關(guān)問(wèn)答感興趣的朋友可以點(diǎn)擊下方鏈接查看!
相關(guān)內(nèi)容推薦:
Dynamic Web TWAIN常見(jiàn)問(wèn)題(一):如何選擇使用哪個(gè)版本?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(二):相比其他的TWAIN SDK,主要優(yōu)勢(shì)是什么?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(三):Dynamic Web TWAIN將使用哪些操作系統(tǒng)?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(四):編程問(wèn)題-沒(méi)有用戶界面怎么工作?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(五):編程問(wèn)題-如何分別設(shè)置水平和垂直分辨率?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(六):編程問(wèn)題-如何使用SSL?
Dynamic Web TWAIN常見(jiàn)問(wèn)題(七):編程問(wèn)題 - 如何進(jìn)行雙面掃描?
如何使用OpenCV為桌面和Web構(gòu)建簡(jiǎn)單的Webcam應(yīng)用程序(一)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: