开启左侧

构建基于 Node.js 的条形码识别程序

[复制链接]
宋包明海 发表于 2018-12-6 07:17:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
In this post, let’s take a glimpse of how easy to build a custom Node module on Windows, Linux, and Mac by wrapping Dynamsoft Barcode Reader SDK, as well as how to integrate the module to quickly implement an online barcode reader.
Editorial Note
This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.
More and more Web developers tend to choose Node.js to build their Website because it is convenient to build complex server-side Web applications using only JavaScript. To extend and empower the functionalities of Node.js applications on different platforms, Node.js allows developers to create addons in C/C++. In this post, let’s take a glimpse of how easy to build a custom Node module on Windows, Linux, and Mac by wrapping Dynamsoft Barcode Reader SDK, as well as how to integrate the module to quickly implement an online barcode reader.
介绍
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
支持图像类型、
运行环境
Node.js 条形码扩展
Node.js 扩展使用 C/C++ 编写的动态链接的共享对象。如果你没有接触过这方面的技术,可以阅读 官方教程 。
创建扩展
创建名为 dbr.cc 的文件,并添加方法 DecodeFile:
#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(Handle exports) {
NODE_SET_METHOD(exports, "decodeFile", DecodeFile);
NODE_MODULE(dbr, Init)
解析来自 JavaScript 传递过来的参数
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 cb = Local::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);
将条形码转成字符串:
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";
将结果转成 v8 对象:
Local barcodeResults = Array::New(isolate);
for (int i = 0; i < count; i++)
tmp = ppBarcodes;
Local 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);
构建扩展
要求:
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 用于多平台编译:
" 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"', {
'WINDOWS_DBR',
" F:\Program Files (x86)\Dynamsoft\Barcode Reader 4.1\Components\C_C++\Include"
" -lF:\Program Files (x86)\Dynamsoft\Barcode Reader 4.1\Components\C_C++\Lib\DBRx64.lib"
'F:\Program Files (x86)\Dynamsoft\Barcode Reader 4.1\Components\C_C++\Redist\DynamsoftBarcodeReaderx64.dll'
['OS=="mac"', {
'MAC_DBR',
'include_dirs' : [
" /Applications/Dynamsoft/Barcode\ Reader\ 4.1/Include"
" -lDynamsoftBarcodeReader"
将 DRB 安装目录替换成你机器上的实际目录。
配置构建环境:
node-gyp configure
可以在 Mac 上你会碰到下面的错误:
error: xcode-select: 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
构建项目:
node-gyp build
在线条形码解析
你已经成功的构建了 Node 的条形码解析模块,现在可以创建一个简单的条形码读取应用。
安装 Express 和 Formidable:
npm install express
npm install formidable
使用 Express 创建一个简单应用:
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);
使用 Formidable 从表单中提取图像数据:
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() {
导入条形码模块用来解析图像文件:
decodeBarcode(res, license, tmpFileName, barcodeType);
运行应用:
node server.js
访问 http://localhost:2016/index.htm:

如果你要在 Windows、Linux 和 Mac 下构建条形码读取应用,可以直接下载示例程序 Dynamsoft Barcode Reader, 也可以直接咨询 support@dynamsoft.com.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝0

帖子64

发布主题
阅读排行 更多
广告位
源码搜论坛官方二维码 官方交流平台

域名:codesou.com

名称:源码搜论坛

站长:1943759704

QQ群:831818825

全国服务热线:

400-123-456789

(工作日:周一至周五 9:00-16:00)
重庆市渝中区中山三路21号
1943759704@qq.com

Archiver-手机版-小黑屋- 源码搜论坛

Copyright © 2018-2022 源码搜论坛版权所有!   渝ICP备14006307号-4

工商网监 安全联盟实名认证 安全联盟认证 财付通合作商家 支付宝合作商家