|
1 | 1 | import * as fs from 'fs'; |
2 | 2 | import { |
3 | 3 | DocumentReaderApi, |
4 | | - Result, |
5 | 4 | Scenario, |
6 | 5 | Source, |
7 | | - TextFieldType, |
8 | 6 | GraphicFieldType, |
9 | 7 | Light, |
10 | | - SecurityFeatureType, |
11 | 8 | } from '@regulaforensics/document-reader-webclient'; |
12 | 9 | import axios from 'axios'; |
13 | 10 | import qs from 'qs'; |
14 | 11 |
|
15 | 12 | const GATEWAY_BASE_URL = 'http://localhost:8080'; |
16 | 13 |
|
17 | | -const { PORTRAIT, DOCUMENT_FRONT } = GraphicFieldType; |
18 | | -const { DOCUMENT_NUMBER } = TextFieldType; |
19 | | - |
20 | 14 | async function get_authorization_token() { |
21 | 15 | let data = qs.stringify({ |
22 | 16 | grant_type: 'password', |
@@ -45,116 +39,71 @@ async function get_authorization_token() { |
45 | 39 | }); |
46 | 40 | } |
47 | 41 |
|
48 | | -(async () => { |
49 | | - const apiBasePath = process.env.API_BASE_PATH || 'http://localhost:4333'; |
50 | | - let license = process.env.TEST_LICENSE; // optional, used here only for smoke test purposes |
51 | | - |
52 | | - if (fs.existsSync('regula.license')) { |
53 | | - license = fs.readFileSync('regula.license'); |
54 | | - } |
55 | | - const token = await get_authorization_token(); |
| 42 | +const apiBasePath = process.env.API_BASE_PATH || 'http://localhost:4333'; |
| 43 | +let license = process.env.TEST_LICENSE; |
56 | 44 |
|
57 | | - const api = new DocumentReaderApi({ |
58 | | - basePath: apiBasePath, |
59 | | - baseOptions: { headers: { Authorization: `Bearer ${token}` } }, |
60 | | - }); |
61 | | - |
62 | | - api.setLicense(license); |
63 | | - |
64 | | - const serverInfo = await api.ping(); |
65 | | - |
66 | | - const white_page_0 = fs.readFileSync('WHITE.jpg').buffer; |
67 | | - const ir_page_0 = fs.readFileSync('IR.jpg').buffer; |
68 | | - const uv_page_0 = fs.readFileSync('UV.jpg').buffer; |
69 | | - |
70 | | - const request = { |
71 | | - images: [ |
72 | | - { |
73 | | - ImageData: white_page_0, |
74 | | - light: Light.WHITE, |
75 | | - page_idx: 0, |
76 | | - }, |
77 | | - { |
78 | | - ImageData: ir_page_0, |
79 | | - light: Light.IR, |
80 | | - page_idx: 0, |
81 | | - }, |
82 | | - { |
83 | | - ImageData: uv_page_0, |
84 | | - light: Light.UV, |
85 | | - page_idx: 0, |
86 | | - }, |
87 | | - ], |
88 | | - processParam: { |
89 | | - scenario: Scenario.FULL_AUTH, |
90 | | - resultTypeOutput: [ |
91 | | - // actual results |
92 | | - Result.STATUS, |
93 | | - Result.AUTHENTICITY, |
94 | | - Result.TEXT, |
95 | | - Result.IMAGES, |
96 | | - Result.DOCUMENT_TYPE, |
97 | | - Result.DOCUMENT_TYPE_CANDIDATES, |
98 | | - Result.IMAGE_QUALITY, |
99 | | - // legacy results |
100 | | - Result.MRZ_TEXT, |
101 | | - Result.VISUAL_TEXT, |
102 | | - Result.BARCODE_TEXT, |
103 | | - Result.RFID_TEXT, |
104 | | - Result.VISUAL_GRAPHICS, |
105 | | - Result.BARCODE_GRAPHICS, |
106 | | - Result.RFID_GRAPHICS, |
107 | | - Result.LEXICAL_ANALYSIS, |
108 | | - ], |
109 | | - }, |
110 | | - }; |
| 45 | +if (fs.existsSync('regula.license')) { |
| 46 | + license = fs.readFileSync('regula.license'); |
| 47 | +} |
| 48 | +const token = await get_authorization_token(); |
111 | 49 |
|
112 | | - const response = await api.process(request); |
| 50 | +const api = new DocumentReaderApi({ |
| 51 | + basePath: apiBasePath, |
| 52 | + baseOptions: { headers: { Authorization: `Bearer ${token}` } }, |
| 53 | +}); |
113 | 54 |
|
114 | | - const requestJson = JSON.stringify(request); |
115 | | - const responseJson = response.json(); |
| 55 | +// Uncomment the line below if you want to transfer the license with each request |
| 56 | +// api.setLicense(license); |
116 | 57 |
|
117 | | - const docOverallStatus = response.status.overallStatus; |
118 | | - const docOpticalTextStatus = response.status.detailsOptical.text; |
| 58 | +const serverInfo = await api.ping(); |
119 | 59 |
|
120 | | - // text fields example |
121 | | - const docNumberField = response.text.getField(DOCUMENT_NUMBER); |
122 | | - const docNumberFieldByName = response.text.getFieldByName('Document Number'); |
| 60 | +const white_page_0 = fs.readFileSync('WHITE.jpg').buffer; |
123 | 61 |
|
124 | | - const docNumberVisual = docNumberField.getValue(Source.VISUAL); |
125 | | - const docNumberMrz = docNumberField.getValue(Source.MRZ); |
126 | | - const docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL); |
127 | | - const docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ); |
128 | | - const docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL); |
| 62 | +const request = { |
| 63 | + images: [ |
| 64 | + { |
| 65 | + ImageData: white_page_0, |
| 66 | + light: Light.WHITE, |
| 67 | + page_idx: 0, |
| 68 | + }, |
| 69 | + ], |
| 70 | + processParam: { |
| 71 | + scenario: Scenario.FULL_PROCESS, |
| 72 | + alreadyCropped: true, |
| 73 | + }, |
| 74 | +}; |
129 | 75 |
|
130 | | - const docAuthenticity = response.authenticity(); |
| 76 | +const response = await api.process(request); |
131 | 77 |
|
132 | | - const docIrB900 = docAuthenticity.irB900Checks(); |
133 | | - const docIrB900BlankChecks = docIrB900.checksByElement(SecurityFeatureType.BLANK); |
| 78 | +// images example |
| 79 | +const documentImage = response.images.getField(GraphicFieldType.DOCUMENT_FRONT).getValue(); |
| 80 | +const portraitField = response.images.getField(GraphicFieldType.PORTRAIT); |
134 | 81 |
|
135 | | - const docImagePattern = docAuthenticity.imagePatternChecks(); |
136 | | - const docImagePatternBlankChecks = docImagePattern.checksByElement(SecurityFeatureType.BLANK); |
| 82 | +if (documentImage) { |
| 83 | + fs.appendFileSync('document-image.jpg', Buffer.from(documentImage)); |
| 84 | +} |
137 | 85 |
|
138 | | - // images example |
139 | | - const documentImage = response.images.getField(DOCUMENT_FRONT).getValue(); |
140 | | - const portraitField = response.images.getField(PORTRAIT); |
| 86 | +if (portraitField) { |
141 | 87 | const portraitFromVisual = portraitField.getValue(Source.VISUAL); |
142 | 88 | fs.appendFileSync('portrait.jpg', Buffer.from(portraitFromVisual)); |
143 | | - fs.appendFileSync('document-image.jpg', Buffer.from(documentImage)); |
| 89 | +} |
| 90 | + |
| 91 | +console.log('---------------------------------------------------------------'); |
| 92 | +console.log(`Document name: ${response.documentType()?.DocumentName || 'UNKNOWN'}`); |
| 93 | +console.log('---------------------------------------------------------------'); |
| 94 | + |
| 95 | +if (response.text) { |
| 96 | + response.text.fieldList.forEach((field) => { |
| 97 | + console.log(`\n[${field.fieldName}]`); |
| 98 | + field.valueList.forEach(({ source, value }) => { |
| 99 | + console.log(` - Source: ${source}`); |
| 100 | + console.log(` Value : ${value}`); |
| 101 | + }); |
| 102 | + console.log('---------------------------------------------------------------'); |
| 103 | + }); |
| 104 | +} else { |
| 105 | + console.log(' NO TEXT DATA'); |
| 106 | + console.log('---------------------------------------------------------------'); |
| 107 | +} |
144 | 108 |
|
145 | | - const docImageQuality = response.imageQualityChecks(); |
146 | | - |
147 | | - console.log('-----------------------------------------------------------------'); |
148 | | - console.log(` Web API version: ${serverInfo.version}`); |
149 | | - console.log('-----------------------------------------------------------------'); |
150 | | - console.log(` Document Overall Status: ${docOverallStatus}`); |
151 | | - console.log(` Document Number Visual: ${docNumberVisual}`); |
152 | | - console.log(` Document Number MRZ: ${docNumberMrz}`); |
153 | | - console.log(`Validity Of Document Number Visual: ${docNumberVisualValidity}`); |
154 | | - console.log(` Validity Of Document Number MRZ: ${docNumberMrzValidity}`); |
155 | | - console.log(` MRZ-Visual values comparison: ${docNumberMrzVisualMatching}`); |
156 | | - console.log('-----------------------------------------------------------------'); |
157 | | - |
158 | | - // how to get low lvl individual results |
159 | | - const lexResult = response.lowLvlResponse.resultByType(Result.LEXICAL_ANALYSIS); |
160 | | -})(); |
| 109 | +console.log(` -Web API version: ${serverInfo.version}-`); |
0 commit comments