Skip to content

Commit 4d69964

Browse files
committed
Add Images
1 parent 0efd0de commit 4d69964

File tree

10 files changed

+269
-113
lines changed

10 files changed

+269
-113
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"plugin:@typescript-eslint/recommended"
1212
],
1313
"rules": {
14-
"unused-imports/no-unused-imports": "error"
14+
"unused-imports/no-unused-imports": "error",
15+
"@typescript-eslint/ban-ts-comment": "off"
1516
}
1617
}

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ wwwroot/*.js
22
node_modules
33
typings
44
dist
5-
.idea
5+
.idea
6+
/example/portraitFromVisual.jpg
7+
/example/normalizedInputImage.jpg

example/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import * as fs from 'fs';
22

3-
import {DocumentReaderApi, Scenario, Result, TextFieldType, Source} from '@regulaforensics/document-reader-client/esm';
3+
import {
4+
DocumentReaderApi,
5+
Result,
6+
Scenario,
7+
Source,
8+
TextFieldType,
9+
GraphicFieldType
10+
} from '@regulaforensics/document-reader-client/esm';
11+
12+
const {PORTRAIT} = GraphicFieldType;
413
const {DOCUMENT_NUMBER} = TextFieldType;
514

615

@@ -17,20 +26,28 @@ const {DOCUMENT_NUMBER} = TextFieldType;
1726
images: [raw_image],
1827
processParam: {
1928
scenario: Scenario.FULL_PROCESS,
20-
resultTypeOutput: [Result.STATUS, Result.TEXT, Result.LEXICAL_ANALYSIS]
29+
resultTypeOutput: [Result.RAW_IMAGE, Result.STATUS, Result.TEXT, Result.IMAGES]
2130
}
2231
})
2332

2433
const docOverallStatus = response.status.complete;
2534
const docOpticalTextStatus = response.status.detailsOptical.text;
2635

36+
// text fields example
2737
const docNumberField = response.text.getField(DOCUMENT_NUMBER);
2838
const docNumberVisual = docNumberField.getValue(Source.VISUAL)
2939
const docNumberMrz = docNumberField.getValue(Source.MRZ)
3040
const docNumberVisualValidity = docNumberField.sourceValidity(Source.VISUAL)
3141
const docNumberMrzValidity = docNumberField.sourceValidity(Source.MRZ)
3242
const docNumberMrzVisualMatching = docNumberField.crossSourceComparison(Source.MRZ, Source.VISUAL)
3343

44+
// images example
45+
const normalizedInputImage = response.images.getNormalizedInputImage()
46+
const portraitField = response.images.getField(PORTRAIT)
47+
const portraitFromVisual = portraitField.getValue(Source.VISUAL)
48+
fs.appendFileSync('portraitFromVisual.jpg', Buffer.from(portraitFromVisual));
49+
fs.appendFileSync('normalizedInputImage.jpg', Buffer.from(normalizedInputImage));
50+
3451
console.log("-----------------------------------------------------------------")
3552
console.log(` Document Overall Status: ${docOverallStatus}`)
3653
console.log(` Document Number Visual: ${docNumberVisual}`)

src/ext/document-reader.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {ProcessRequestImage as ProcessRequestImageBase} from "../models/index.js
88
import {ProcessRequest} from "./process-request.js";
99
import {ProcessRequestImage} from "./process-request-image.js";
1010

11+
// @ts-ignore
1112
import converter from "base64-arraybuffer";
1213

1314

src/ext/images.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {
2+
GraphicFieldType,
3+
Images as BaseImages,
4+
ImagesField as BaseImagesField,
5+
ImagesAvailableSource, ImagesFieldValue, Source, RawImageResult,
6+
} from "../models/index.js";
7+
8+
// @ts-ignore
9+
import converter from "base64-arraybuffer";
10+
11+
export class Images implements BaseImages {
12+
13+
availableSourceList: Array<ImagesAvailableSource>;
14+
fieldList: Array<ImagesField>;
15+
normalizedInputImages: Array<RawImageResult>;
16+
17+
constructor(origin: BaseImages, normalizedInputImages: Array<RawImageResult>) {
18+
this.normalizedInputImages = normalizedInputImages;
19+
this.availableSourceList = origin.availableSourceList
20+
this.fieldList = origin.fieldList.map(field => new ImagesField(field))
21+
}
22+
23+
public getField(type: GraphicFieldType): ImagesField | undefined {
24+
return this.fieldList.find(field => field.fieldType == type)
25+
}
26+
27+
public getNormalizedInputImage(page = 0): ArrayBuffer | undefined {
28+
const imageAsBase64 = this.normalizedInputImages.find(i=>i.page_idx==page)?.RawImageContainer?.image
29+
if(imageAsBase64) {
30+
return base64ToBuffer(imageAsBase64)
31+
}
32+
return undefined
33+
}
34+
}
35+
36+
export class ImagesField implements BaseImagesField {
37+
38+
fieldName: string;
39+
fieldType: GraphicFieldType;
40+
valueList: Array<ImagesFieldValue>;
41+
42+
constructor(origin: BaseImagesField) {
43+
this.fieldType = origin.fieldType
44+
this.fieldType = origin.fieldType
45+
this.valueList = origin.valueList
46+
}
47+
48+
public getValue(source?: Source, original = false): ArrayBuffer | undefined {
49+
let fieldValue
50+
if (!source) {
51+
// rfid -> visual -> barcode
52+
fieldValue = this.valueList.find(v => v.source == Source.RFID)
53+
|| this.valueList.find(v => v.source == Source.VISUAL)
54+
|| this.valueList.find(v => v.source == Source.BARCODE)
55+
} else {
56+
fieldValue = this.valueList.find(v => v.source == source)
57+
}
58+
59+
if (!fieldValue) {
60+
return undefined
61+
}
62+
63+
if (original) {
64+
return base64ToBuffer(fieldValue.originalValue)
65+
}
66+
return base64ToBuffer(fieldValue.value)
67+
}
68+
}
69+
70+
function base64ToBuffer(str: string): ArrayBuffer {
71+
return converter.decode(str);
72+
}

src/ext/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './process-request-image.js';
44
export * from './process-response.js';
55
export * from './result-item.js';
66
export * from './text.js';
7+
export * from './images.js';
78
export * from './text-field.js';

src/ext/process-response.ts

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,81 @@
11
import {
2-
ContainerList,
3-
ProcessingStatus,
4-
ProcessResponse,
5-
Result,
6-
ResultItem,
7-
RfidLocation, Status, StatusResult,
8-
TextResult,
9-
TransactionInfo
2+
ContainerList, ImagesResult,
3+
ProcessingStatus,
4+
ProcessResponse, RawImageResult,
5+
Result,
6+
ResultItem,
7+
RfidLocation, Status, StatusResult,
8+
TextResult,
9+
TransactionInfo
1010
} from "../models/index.js";
1111
import {Text} from "./text.js";
12+
import {Images} from "./images.js";
1213

1314

1415
export class Response {
1516

16-
// other modules:
17-
// - images
18-
// - status
19-
// - authenticity
20-
// - document
21-
status?: Status
22-
text?: Text
17+
// other future modules:
18+
// - authenticity
19+
// - document
20+
status?: Status
21+
text?: Text
22+
images?: Images
2323

24-
lowLvlResponse: LowLvlResponse
24+
lowLvlResponse: LowLvlResponse
2525

26-
constructor(original: ProcessResponse) {
26+
constructor(original: ProcessResponse) {
2727

28-
const lowLvlResponse = new LowLvlResponse(original)
29-
this.lowLvlResponse = lowLvlResponse
28+
const lowLvlResponse = new LowLvlResponse(original)
29+
this.lowLvlResponse = lowLvlResponse
3030

31-
const textResult = lowLvlResponse.textResult()
32-
if (textResult) {
33-
this.text = new Text(textResult.Text)
34-
}
35-
this.status = lowLvlResponse.statusResult()?.Status
31+
this.status = lowLvlResponse.statusResult()?.Status
32+
const textResult = lowLvlResponse.textResult()
33+
if (textResult) {
34+
this.text = new Text(textResult.Text)
3635
}
36+
const imagesResult = lowLvlResponse.imagesResult()
37+
if (imagesResult) {
38+
this.images = new Images(imagesResult.Images, lowLvlResponse.normalizedInputImages())
39+
}
40+
}
3741
}
3842

3943
export class LowLvlResponse implements ProcessResponse {
4044

41-
ContainerList: ContainerList
42-
ProcessingFinished: ProcessingStatus
43-
TransactionInfo: TransactionInfo
44-
ChipPage: RfidLocation
45+
ContainerList: ContainerList
46+
ProcessingFinished: ProcessingStatus
47+
TransactionInfo: TransactionInfo
48+
ChipPage: RfidLocation
4549

46-
constructor(original: ProcessResponse) {
47-
this.ContainerList = original.ContainerList
48-
this.ProcessingFinished = original.ProcessingFinished
49-
this.TransactionInfo = original.TransactionInfo
50-
this.ChipPage = original.ChipPage
51-
}
50+
constructor(original: ProcessResponse) {
51+
this.ContainerList = original.ContainerList
52+
this.ProcessingFinished = original.ProcessingFinished
53+
this.TransactionInfo = original.TransactionInfo
54+
this.ChipPage = original.ChipPage
55+
}
5256

53-
public textResult(): TextResult | undefined {
54-
return <TextResult>this.resultByType(Result.TEXT)
55-
}
57+
public normalizedInputImages(): Array<RawImageResult> | undefined {
58+
// @ts-ignore
59+
return this.resultsByType(Result.RAW_IMAGE)
60+
}
5661

57-
public statusResult(): StatusResult | undefined {
58-
return <StatusResult>this.resultByType(Result.STATUS)
59-
}
62+
public statusResult(): StatusResult | undefined {
63+
return <StatusResult>this.resultByType(Result.STATUS)
64+
}
6065

61-
public resultByType(type: Result): ResultItem | undefined {
62-
return this.ContainerList.List.find(container => container.result_type === type)
63-
}
66+
public textResult(): TextResult | undefined {
67+
return <TextResult>this.resultByType(Result.TEXT)
68+
}
69+
70+
public imagesResult(): ImagesResult | undefined {
71+
return <ImagesResult>this.resultByType(Result.IMAGES)
72+
}
73+
74+
public resultByType(type: Result): ResultItem | undefined {
75+
return this.ContainerList.List.find(container => container.result_type === type)
76+
}
77+
78+
public resultsByType(type: Result): Array<ResultItem> | undefined {
79+
return this.ContainerList.List.filter(container => container.result_type === type)
80+
}
6481
}

0 commit comments

Comments
 (0)