Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed examples/auth/client/IR.jpg
Binary file not shown.
Binary file removed examples/auth/client/UV.jpg
Binary file not shown.
161 changes: 49 additions & 112 deletions examples/auth/client/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import * as fs from 'fs';
import {
DocumentReaderApi,
Result,
Scenario,
Source,
TextFieldType,
GraphicFieldType,
Light,
SecurityFeatureType,
} from '@regulaforensics/document-reader-webclient';
import axios from 'axios';
import qs from 'qs';

const GATEWAY_BASE_URL = 'http://localhost:8080';

const { PORTRAIT, DOCUMENT_FRONT } = GraphicFieldType;
const { DOCUMENT_NUMBER } = TextFieldType;

async function get_authorization_token() {
let data = qs.stringify({
grant_type: 'password',
Expand Down Expand Up @@ -45,116 +39,59 @@ async function get_authorization_token() {
});
}

(async () => {
const apiBasePath = process.env.API_BASE_PATH || 'http://localhost:4333';
let license = process.env.TEST_LICENSE; // optional, used here only for smoke test purposes
const apiBasePath = process.env.API_BASE_PATH || 'http://localhost:4333';
let license = process.env.TEST_LICENSE;

if (fs.existsSync('regula.license')) {
license = fs.readFileSync('regula.license');
}
const token = await get_authorization_token();
if (fs.existsSync('regula.license')) {
license = fs.readFileSync('regula.license');
}
const token = await get_authorization_token();

const api = new DocumentReaderApi({
basePath: apiBasePath,
baseOptions: { headers: { Authorization: `Bearer ${token}` } },
});
const api = new DocumentReaderApi({
basePath: apiBasePath,
baseOptions: { headers: { Authorization: `Bearer ${token}` } },
});

api.setLicense(license);
// Uncomment the line below if you want to transfer the license with each request
// api.setLicense(license);

const serverInfo = await api.ping();
const serverInfo = await api.ping();

const white_page_0 = fs.readFileSync('WHITE.jpg').buffer;
const ir_page_0 = fs.readFileSync('IR.jpg').buffer;
const uv_page_0 = fs.readFileSync('UV.jpg').buffer;
const white_page_0 = fs.readFileSync('WHITE.jpg').buffer;

const request = {
images: [
{
ImageData: white_page_0,
light: Light.WHITE,
page_idx: 0,
},
{
ImageData: ir_page_0,
light: Light.IR,
page_idx: 0,
},
{
ImageData: uv_page_0,
light: Light.UV,
page_idx: 0,
},
],
processParam: {
scenario: Scenario.FULL_AUTH,
resultTypeOutput: [
// actual results
Result.STATUS,
Result.AUTHENTICITY,
Result.TEXT,
Result.IMAGES,
Result.DOCUMENT_TYPE,
Result.DOCUMENT_TYPE_CANDIDATES,
Result.IMAGE_QUALITY,
// legacy results
Result.MRZ_TEXT,
Result.VISUAL_TEXT,
Result.BARCODE_TEXT,
Result.RFID_TEXT,
Result.VISUAL_GRAPHICS,
Result.BARCODE_GRAPHICS,
Result.RFID_GRAPHICS,
Result.LEXICAL_ANALYSIS,
],
const request = {
images: [
{
ImageData: white_page_0,
light: Light.WHITE,
page_idx: 0,
},
};

const response = await api.process(request);

const requestJson = JSON.stringify(request);
const responseJson = response.json();

const docOverallStatus = response.status.overallStatus;
const docOpticalTextStatus = response.status.detailsOptical.text;

// text fields example
const docNumberField = response.text.getField(DOCUMENT_NUMBER);
const docNumberFieldByName = response.text.getFieldByName('Document Number');

const docNumberVisual = docNumberField.getValue(Source.VISUAL);
const docNumberMrz = docNumberField.getValue(Source.MRZ);
const docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL);
const docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ);
const docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL);

const docAuthenticity = response.authenticity();

const docIrB900 = docAuthenticity.irB900Checks();
const docIrB900BlankChecks = docIrB900.checksByElement(SecurityFeatureType.BLANK);

const docImagePattern = docAuthenticity.imagePatternChecks();
const docImagePatternBlankChecks = docImagePattern.checksByElement(SecurityFeatureType.BLANK);

// images example
const documentImage = response.images.getField(DOCUMENT_FRONT).getValue();
const portraitField = response.images.getField(PORTRAIT);
const portraitFromVisual = portraitField.getValue(Source.VISUAL);
fs.appendFileSync('portrait.jpg', Buffer.from(portraitFromVisual));
fs.appendFileSync('document-image.jpg', Buffer.from(documentImage));

const docImageQuality = response.imageQualityChecks();

console.log('-----------------------------------------------------------------');
console.log(` Web API version: ${serverInfo.version}`);
console.log('-----------------------------------------------------------------');
console.log(` Document Overall Status: ${docOverallStatus}`);
console.log(` Document Number Visual: ${docNumberVisual}`);
console.log(` Document Number MRZ: ${docNumberMrz}`);
console.log(`Validity Of Document Number Visual: ${docNumberVisualValidity}`);
console.log(` Validity Of Document Number MRZ: ${docNumberMrzValidity}`);
console.log(` MRZ-Visual values comparison: ${docNumberMrzVisualMatching}`);
console.log('-----------------------------------------------------------------');

// how to get low lvl individual results
const lexResult = response.lowLvlResponse.resultByType(Result.LEXICAL_ANALYSIS);
})();
],
processParam: {
scenario: Scenario.FULL_PROCESS,
alreadyCropped: true,
},
};

const response = await api.process(request);

// images example
const documentImage = response.images.getField(GraphicFieldType.DOCUMENT_FRONT).getValue();
const portraitField = response.images.getField(GraphicFieldType.PORTRAIT);
const portraitFromVisual = portraitField.getValue(Source.VISUAL);
fs.appendFileSync('portrait.jpg', Buffer.from(portraitFromVisual));
fs.appendFileSync('document-image.jpg', Buffer.from(documentImage));

console.log('---------------------------------------------------------------');
console.log(`Document name: ${response.documentType().DocumentName}`);
console.log('---------------------------------------------------------------');

response.text.fieldList.forEach((field) => {
console.log(`\n[${field.fieldName}]`);
field.valueList.forEach(({ source, value }) => {
console.log(` - Source: ${source}`);
console.log(` Value : ${value}`);
});
console.log('---------------------------------------------------------------');
});
console.log(` -Web API version: ${serverInfo.version}-`);
1 change: 0 additions & 1 deletion examples/auth/client/license.txt

This file was deleted.

6 changes: 3 additions & 3 deletions examples/auth/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed examples/basic/IR.jpg
Binary file not shown.
Binary file removed examples/basic/UV.jpg
Binary file not shown.
155 changes: 46 additions & 109 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
@@ -1,124 +1,61 @@
import * as fs from 'fs';
import {
DocumentReaderApi,
Result,
Scenario,
Light,
Source,
TextFieldType,
GraphicFieldType,
Light,
SecurityFeatureType,
} from '@regulaforensics/document-reader-webclient';

const { PORTRAIT, DOCUMENT_FRONT } = GraphicFieldType;
const { DOCUMENT_NUMBER } = TextFieldType;
const apiBasePath = process.env.API_BASE_PATH || 'https://api.regulaforensics.com';
let license = process.env.TEST_LICENSE;

(async () => {
const apiBasePath = process.env.API_BASE_PATH || 'https://api.regulaforensics.com';
let license = process.env.TEST_LICENSE; // optional, used here only for smoke test purposes
if (fs.existsSync('regula.license')) {
license = fs.readFileSync('regula.license');
}

if (fs.existsSync('regula.license')) {
license = fs.readFileSync('regula.license');
}
const api = new DocumentReaderApi({ basePath: apiBasePath });

const api = new DocumentReaderApi({ basePath: apiBasePath });
// Uncomment the line below if you want to transfer the license with each request
// api.setLicense(license);

api.setLicense(license);
const serverInfo = await api.ping();

const serverInfo = await api.ping();
const white_page_0 = fs.readFileSync('WHITE.jpg').buffer;

const white_page_0 = fs.readFileSync('WHITE.jpg').buffer;
const ir_page_0 = fs.readFileSync('IR.jpg').buffer;
const uv_page_0 = fs.readFileSync('UV.jpg').buffer;

const request = {
images: [
{
ImageData: white_page_0,
light: Light.WHITE,
page_idx: 0,
},
{
ImageData: ir_page_0,
light: Light.IR,
page_idx: 0,
},
{
ImageData: uv_page_0,
light: Light.UV,
page_idx: 0,
},
],
processParam: {
scenario: Scenario.FULL_AUTH,
resultTypeOutput: [
// actual results
Result.STATUS,
Result.AUTHENTICITY,
Result.TEXT,
Result.IMAGES,
Result.DOCUMENT_TYPE,
Result.DOCUMENT_TYPE_CANDIDATES,
Result.IMAGE_QUALITY,
// legacy results
Result.MRZ_TEXT,
Result.VISUAL_TEXT,
Result.BARCODE_TEXT,
Result.RFID_TEXT,
Result.VISUAL_GRAPHICS,
Result.BARCODE_GRAPHICS,
Result.RFID_GRAPHICS,
Result.LEXICAL_ANALYSIS,
],
const request = {
images: [
{
ImageData: white_page_0,
light: Light.WHITE,
page_idx: 0,
},
};

const response = await api.process(request);

const requestJson = JSON.stringify(request);
const responseJson = response.json();

const docOverallStatus = response.status.overallStatus;
const docOpticalTextStatus = response.status.detailsOptical.text;

// text fields example
const docNumberField = response.text.getField(DOCUMENT_NUMBER);
const docNumberFieldByName = response.text.getFieldByName('Document Number');

const docNumberVisual = docNumberField.getValue(Source.VISUAL);
const docNumberMrz = docNumberField.getValue(Source.MRZ);
const docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL);
const docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ);
const docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL);

const docAuthenticity = response.authenticity();

const docIrB900 = docAuthenticity.irB900Checks();
const docIrB900BlankChecks = docIrB900.checksByElement(SecurityFeatureType.BLANK);

const docImagePattern = docAuthenticity.imagePatternChecks();
const docImagePatternBlankChecks = docImagePattern.checksByElement(SecurityFeatureType.BLANK);

// images example
const documentImage = response.images.getField(DOCUMENT_FRONT).getValue();
const portraitField = response.images.getField(PORTRAIT);
const portraitFromVisual = portraitField.getValue(Source.VISUAL);
fs.appendFileSync('portrait.jpg', Buffer.from(portraitFromVisual));
fs.appendFileSync('document-image.jpg', Buffer.from(documentImage));

const docImageQuality = response.imageQualityChecks();

console.log('-----------------------------------------------------------------');
console.log(` Web API version: ${serverInfo.version}`);
console.log('-----------------------------------------------------------------');
console.log(` Document Overall Status: ${docOverallStatus}`);
console.log(` Document Number Visual: ${docNumberVisual}`);
console.log(` Document Number MRZ: ${docNumberMrz}`);
console.log(`Validity Of Document Number Visual: ${docNumberVisualValidity}`);
console.log(` Validity Of Document Number MRZ: ${docNumberMrzValidity}`);
console.log(` MRZ-Visual values comparison: ${docNumberMrzVisualMatching}`);
console.log('-----------------------------------------------------------------');

// how to get low lvl individual results
const lexResult = response.lowLvlResponse.resultByType(Result.LEXICAL_ANALYSIS);
})();
],
processParam: {
scenario: Scenario.FULL_PROCESS,
alreadyCropped: true,
},
};

const response = await api.process(request);

// images example
const documentImage = response.images.getField(GraphicFieldType.DOCUMENT_FRONT).getValue();
const portraitField = response.images.getField(GraphicFieldType.PORTRAIT);
const portraitFromVisual = portraitField.getValue(Source.VISUAL);
fs.appendFileSync('portrait.jpg', Buffer.from(portraitFromVisual));
fs.appendFileSync('document-image.jpg', Buffer.from(documentImage));

console.log('---------------------------------------------------------------');
console.log(`Document name: ${response.documentType().DocumentName}`);
console.log('---------------------------------------------------------------');

response.text.fieldList.forEach((field) => {
console.log(`\n[${field.fieldName}]`);
field.valueList.forEach(({ source, value }) => {
console.log(` - Source: ${source}`);
console.log(` Value : ${value}`);
});
console.log('---------------------------------------------------------------');
});
console.log(` -Web API version: ${serverInfo.version}-`);
Loading
Loading