mrz4nodejs
v2.0.1
Published
Node.js MRZ (passport, ID card, visa) scanner built on Dynamsoft Capture Vision - pure JavaScript, no native build step.
Maintainers
Readme
Node.js MRZ Scanner SDK
Node.js MRZ (Machine Readable Zone) scanner for passports, ID cards and visas. It exposes the familiar
mrz4nodejs API on top of
dynamsoft-capture-vision-for-node,
which means:
- No native build step. No
node-gyp, no C++ compiler, no prebuilt.nodebinaries. - Always the latest Dynamsoft MRZ engine. The engine ships as a regular npm dependency.
- Unchanged JavaScript API. Existing
mrz4nodejscode keeps working without modification.
const MrzScanner = require('mrz4nodejs');
MrzScanner.initLicense('LICENSE-KEY');
const scanner = new MrzScanner();
const lines = await scanner.decodeFileAsync('passport.png');
console.log(scanner.parseTwoLines(lines[0].text, lines[1].text));License Key
Get a 30-day FREE trial license to activate the SDK.
MrzScanner.initLicense('DLS2eyJoYW5kc2hha2VDb2RlIjoi...');Installation
npm install mrz4nodejsRequirements:
- Node.js >= 16
- Windows (x64 / x86), Linux (x64 / arm64) or macOS (x64 / arm64). The platform specific native library is
pulled in automatically as an optional dependency of
dynamsoft-capture-vision-for-node.
Supported Documents
| Document | MRZ format | Lines | |---|---|---| | Passport | TD-3 | 2 | | ID card | TD-1 | 3 | | ID card | TD-2 | 2 | | Visa | MRV-A / MRV-B | 2 |
API
MrzScanner.initLicense(license: string): numberMrzScanner.getVersionNumber(): stringscanner.loadModel(modelDir: string): numberscanner.setTemplate(templateName: string): voidscanner.decodeFileAsync(filePath: string): Promise<MrzLineResult[]>scanner.decodeBufferAsync(buffer, width, height, stride): Promise<MrzLineResult[]>scanner.parseTwoLines(line1, line2): MrzInfo | falsescanner.parseThreeLines(line1, line2, line3): MrzInfo | false
MrzLineResult
{
confidence: number, // recognition confidence of the MRZ block
text: string, // one MRZ line, e.g. "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<"
x1, y1, x2, y2, x3, y3, x4, y4: number // bounding quad of that line
}decodeFileAsync() and decodeBufferAsync() return one item per MRZ line, so
parseTwoLines(result[0].text, result[1].text) keeps working exactly as before.
Usage
Basic steps:
Set the license key.
MrzScanner.initLicense('LICENSE-KEY');Create an MRZ scanner object.
const scanner = new MrzScanner();(Optional) Point the scanner at a custom template directory. The MRZ models ship with the npm package, so this step is only needed when
modelDircontains a Capture Vision template (MRZ.jsonwith aCaptureVisionTemplatesarray). It is kept so existing code keeps working.scanner.loadModel(path.dirname(require.resolve('mrz4nodejs')));Recognize MRZ lines from an image file.
const result = await scanner.decodeFileAsync('passport.png'); console.log(result);Parse the MRZ information.
const info = result.length === 3 ? scanner.parseThreeLines(result[0].text, result[1].text, result[2].text) : scanner.parseTwoLines(result[0].text, result[1].text); console.log(info); // { // type: 'PASSPORT (TD-3)', // nationality: 'UTO', // surname: 'ERIKSSON', // givenname: 'ANNA MARIA', // passportnumber: 'L898902C3', // issuecountry: 'UTO', // birth: '1974-08-12', // gender: 'F', // expiry: '2012-04-15' // }Or recognize raw pixels (for example a camera frame).
// Buffer holding RGB pixels const result = await scanner.decodeBufferAsync(rgbBuffer, width, height, width * 3);
Templates
setTemplate() switches the preset used by the decode calls:
| Template | Purpose |
|---|---|
| ReadPassportAndId (default) | Passports, ID cards and visas |
| ReadPassport | Passports (TD-3) |
| ReadId | ID cards (TD-1 / TD-2) |
| ReadVisa | Visas (MRV-A / MRV-B) |
| ReadMRZ | Any MRZ |
Behaviour Notes
- One result per MRZ line. The engine returns one block per document; the block text is split on
\nand a quad is interpolated per line, so the historicalresult[0..2].textshape is preserved. - Errors are reported. Failures (invalid license, unreadable file) reject the promise. Older versions resolved with an empty array.
getVersionNumber()returns the version string reported by the SDK itself, e.g.js:3.6.4002.20261019;cpp:3.6.40.9167(Dynamsoft did not bump the JS prefix in 3.6.4003).- Decoding runs in worker threads managed by the SDK, so it never blocks the event loop. The worker pool is shut down shortly after the last decode, which lets short-lived scripts exit on their own.
Examples
node examples/app.js node examples/app.js images/3.jpg --jsonOfficial command-line example (raw Capture Vision API)
cd examples/official && npm install node mrz.js ../../images/1.png
Testing
npm test
DCV_LICENSE=YOUR-LICENSE-KEY npm testLicense
MIT
