dynamsoft-capture-vision-for-node
v0.0.1
Published
Dynamsoft Capture Vision (DCV) is a comprehensive SDK that integrates various functional products. It encompasses image capture, content understanding, result parsing, and interactive workflow. Essentially, DCV processes images to extract specific informa
Maintainers
Readme
Dynamsoft Capture Vision for Node
This is a nodejs wrapper for Dynamsoft Capture Vision. You can use it to parse barcodes, recognize partial label text, capture documents, and more.
npm i dynamsoft-capture-vision-for-node
Let's take parsing a QR code in a picture as an example.
const { LiceseManager, CaptureVisionRouter, EnumPresetTemplate } = require('dynamsoft-capture-vision-for-node');
const util = require('node:util');
// You can get your trial license from
// https://www.dynamsoft.com/customer/license/trialLicense -> Barcode Reader -> Desktop/Server
// The current used license is valid for 1 day.
LiceseManager.initLicense('DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9');
//// In some cases you need custom templates provided by Dynamsoft Support
// CaptureVisionRouter.initSettings('path/to/template');
(async()=>{
// you can get the image from https://github.com/Dynamsoft/capture-vision-for-node
// The second parameter `templateName` tells the SDK how to process this image.
let result = await CaptureVisionRouter.captureAsync('./AllSupportedBarcodeTypes.png', EnumPresetTemplate.PT_READ_BARCODES_READ_RATE_FIRST);
// refer to https://www.dynamsoft.com/capture-vision/docs/server/programming/cplusplus/api-reference/capture-vision-router/auxiliary-classes/captured-result.html?product=dbr&lang=cplusplus
// or run `console.log(util.inspect(result, false, null))` to see details
for(let item of result.barcodeResultItems){
console.log(item.text);
}
})();
The capture
like API
CaptureVisionRouter.captureAsync(...)
process image in worker_threads. The max number of worker is defined in CaptureVisionRouter.maxWorkerCount
, by default <logical processor number> - 1
, Minimum 1. If you continue to call captureAsync(...)
when all workers are busy, it will be queued waiting for execution.
The synchronous version is CaptureVisionRouter.capture(...)
, which processes images on the main thread.
The capture
like APIs can accept file path string
or file bytes Uint8Array
as input data. Currently supported file types are jpg, png, bmp, gif, pdf.
The capture
like APIs also accept DCVImageData
as input data. Typically used to process raw data from a camera.
interface DCVImageData{
bytes: Uint8Array;
width: number;
height: number;
stride: number;
format: EnumImagePixelFormat;
/** EXIF orientation; 1 or undefined means no rotate. */
orientation?: number;
}
If input data is file bytes or DCVImageData
, by default, CaptureVisionRouter.captureAsync(...)
will transfer bytes into worker
. Thus you can't access to these bytes in main thread after captureAsync
. This allows for optimal performance.
The following code can prevent bytes from being transferred.
let result = await CaptureVisionRouter.captureAsync(input_data_contains_bytes, {
templateName: EnumPresetTemplate.XXXX,
dataTransferType: 'copy'
});
Web Service
Express sample and koa sample shows how to use the SDK in a web service. You do not need to start multiple instance processes in PM2 Cluster mode. As mentioned above, Dynamsoft Capture Vision for Node already manages a thread pool.
However, pm2 start app.js
is still useful, it can automatically restart app.js
when service crashes.
Supported OS/Arch
| os | arch | |:-----|:-------| | windows | x86, x64 | | linux | x64, amd64 | | mac | x64, amd64 |
If you are sure you don't need to support some of these OS/arch, you can delete some files in node_modules/dynamsoft-capture-vision-for-node/dylib
and node_modules/koffi
, to reduce the size. In some cloud platforms like AWS lambda, size is important.
AWS Lambda
We also made special adaptation for AWS lambda, see this sample. Other similar single-function platforms may have some compatibility issues. If you have any needs, please contact us.
Multiple CaptureVisionRouter
Instances
In some rare cases you may need multiple CaptureVisionRouter
instances, such as customizing different templates for each instance. Here is how to use:
let cvr = new CaptureVisionRouter();
let settings = cvr.outputSettings('<templateName>');
settings.foo = bar;
cvr.initSettings(settings);
let result = await cvr.captureAsync('<image>', '<templateName>');