翻譯|使用教程|編輯:楊鵬連|2020-07-17 16:55:57.553|閱讀 535 次
概述:上周,我分享了如何在桌面和網絡應用中顯示網絡攝像頭視頻。在此基礎上,我使用Node.js實現了條形碼讀取功能。在本文中,我將分享如何使用Dynamsoft Barcode Reader SDK為桌面和Web構建Node.js條碼閱讀器應用程序。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Dynamic Web TWAIN是一個專為Web應用程序設計的TWAIN掃描識別控件。你只需在TWAIN接口寫幾行代碼,就可以用兼容TWAIN的掃描儀掃描文檔或從數碼相機/采集卡中獲取圖像。然后用戶可以編輯圖像并將圖像保存為多種格式,用戶可保存圖像到遠程數據庫或者SharePoint。該TWAIN控件還支持上傳和處理本地圖像。
本文匯集了一些Dynamic Web TWAIN編程方面的常見問題,并且針對這些問題進行了回答,感興趣的朋友快來了解一下吧~
將Dynamsoft Barcode SDK的C / C ++ API與JavaScript綁定
讓我們開始使用Node.js條碼C / C ++插件。
為了支持OpenCV Mat,我創建了一個新的API encodingBufferAsync()。C / C ++代碼如下:
void DecodeBufferAsync(const FunctionCallbackInfo第一個參數是Node.js緩沖區指針。您可以調用getData()從Mat獲取字節數組:& 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的構建配置與binding.gyp文件中的Linux配置略有不同:
'copies': [
{
'destination': '/usr/local/lib/',
'files': [
'./platforms/macos/libDynamsoftBarcodeReader.dylib'
]
}
]
與在Linux上不同,一旦構建完成,動態庫文件將被復制到/ usr / local / lib /目錄。原因是RPATH無法在macOS上運行。我們可以使用“ otool -L dbr.node ”來檢查依賴庫,然后獲取路徑/usr/local/lib/libDynamsoftBarcodeReader.dylib。
如果要將庫文件和dbr.node保留在同一文件夾中,則可以手動更改庫路徑:
cd build/Release install_name_tool -change /usr/local/lib/libDynamsoftBarcodeReader.dylib @loader_path/libDynamsoftBarcodeReader.dylib dbr.node
我已經將該軟件包發布到//ke049m.cn/product/1313。要安裝該軟件包,您需要安裝C ++開發工具,然后運行:
npm install -g node-gyp npm install barcode4nodejs在5分鐘內為桌面和Web構建Node.js條形碼閱讀器
桌面
基本上,我們可以使用無限循環來捕獲攝像頭幀并將其顯示在窗口中:
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;}
}
但是,如果我們在循環中調用異步條形碼解碼功能,則回調函數將永遠不會返回。為了使其工作,我們可以使用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()
由于連續的網絡攝像頭圖像相似,因此可以在不同的幀上繪制結果:
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);
}
}

網頁
將條形碼檢測代碼復制到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();
現在我們可以運行服務器端條形碼檢測。它與任何Web瀏覽器完全兼容。這是Microsoft Internet Explorer的屏幕截圖。
Dynamic Web TWAIN的常見問答到這里就結束了,對相關問答感興趣的朋友可以點擊下方鏈接查看!
相關內容推薦:
Dynamic Web TWAIN常見問題(一):如何選擇使用哪個版本?
Dynamic Web TWAIN常見問題(二):相比其他的TWAIN SDK,主要優勢是什么?
Dynamic Web TWAIN常見問題(三):Dynamic Web TWAIN將使用哪些操作系統?
Dynamic Web TWAIN常見問題(四):編程問題-沒有用戶界面怎么工作?
Dynamic Web TWAIN常見問題(五):編程問題-如何分別設置水平和垂直分辨率?
Dynamic Web TWAIN常見問題(六):編程問題-如何使用SSL?
Dynamic Web TWAIN常見問題(七):編程問題 - 如何進行雙面掃描?
如何使用OpenCV為桌面和Web構建簡單的Webcam應用程序(一)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: