由于有了Node.js,JavaScript能够被用于服务端编程。经过各类扩展,Node.js能够变得很是强大。今天分享下怎样用C++建立Node.js扩展。javascript
参考原文:Making Dynamsoft Barcode SDK an Addon for Node.jshtml
要构建扩展,须要安装node-gyp:java
npm install -g node-gyp
这个库里面包涵了JavaScript v8引擎所须要的头文件以及依赖库。node
建立一个C/C++文件dbr.cc以及配置文件binding.gyp。打开配置文件,在里面加入扩展名和源代码文件:git
{ "targets": [ { "target_name": "dbr", "sources": [ "dbr.cc" ] } ] }
如今就能够用来构建dbr.node,用于Node.js的动态连接库,至关于DLL。在命令行中输入:github
node-gyp configure install
这行命令干了两件事,首先生成了Visual Studio的工程文件。而后调用VS的编译器生成了动态连接库。能够看下生成的文件结构:npm
build / binding.sln / dbr.vcxproj / dbr.vcxproj.filters / config.gypi / Release / dbr.node / dbr.pdb / obj
更多内容能够参考官方文档Node.js addons。编程
接下来咱们只须要使用Visual Studio来编写代码,构建工程就能够了。由于node-gyp在配置的时候已经把工程文件里的头文件路径和依赖库路径都加进去了,咱们只须要作很小的修改。如今双击binding.sln导入工程。添加Dynamsoft Barcode SDK相关的头文件路径和库路径。最后添加post-build event用于拷贝DLL到生成目录:api
copy "{installation directory}\Dynamsoft\Barcode Reader 2.0 Trial\Redist\C_C++\*.dll" "$(OutDir)"
如今开始编写C++代码。和Java,Python相似,在编写native代码的时候,须要首先注册native函数。初始化Barcode解码接口:函数
void Init(Handle<Object> exports) { NODE_SET_METHOD(exports, "decodeFile", DecodeFile); } NODE_MODULE(dbr, Init)
接下来咱们把获取的数据转换成v8数据类型。使用Local<Object>来存储一个Barcode结果,使用Local<Array>来存储全部的Local<Object>。最后经过回调函数把结果返回到JavaScript层。
void DecodeFile(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); // convert v8 string to char * String::Utf8Value utfStr(args[0]->ToString()); char *pFileName = *utfStr; int option_iMaxBarcodesNumPerPage = -1; int option_llBarcodeFormat = -1; pBarcodeResultArray pResults = NULL; ReaderOptions option; SetOptions(&option, option_iMaxBarcodesNumPerPage, option_llBarcodeFormat); // decode barcode image file int ret = DBR_DecodeFile( pFileName, &option, &pResults ); if (ret == DBR_OK){ int count = pResults->iBarcodeCount; pBarcodeResult* ppBarcodes = pResults->ppBarcodes; pBarcodeResult tmp = NULL; // javascript callback function Local<Function> cb = Local<Function>::Cast(args[1]); const unsigned argc = 1; // array for storing barcode results 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"), Number::New(isolate, tmp->llFormat)); result->Set(String::NewFromUtf8(isolate, "value"), String::NewFromUtf8(isolate, tmp->pBarcodeData)); barcodeResults->Set(Number::New(isolate, i), result); } // release memory DBR_FreeBarcodeResults(&pResults); Local<Value> argv[argc] = { barcodeResults }; cb->Call(isolate->GetCurrentContext()->Global(), argc, argv); } }
如今建立一个JavaScript脚本文件测试一下:
var dbr = require('./build/Release/dbr'); var readline = require('readline'); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("Please input a barcode image path: ", function(answer) { // e.g. F:\git\Dynamsoft-Barcode-Reader\Images\AllSupportedBarcodeTypes.tif dbr.decodeFile( answer, function(msg){ var result = null; for (index in msg) { result = msg[index] console.log(result['format']); console.log(result['value']); console.log("##################"); } } ); rl.close(); });
最后经过命令行运行查看结果: