Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Aug 5, 2024
1 parent a8b1b98 commit 0679fa4
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/app/mrz-reader/mrz-reader.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

textarea {
width: 720px;
height: 300px;
height: 200px;
margin: 10px auto;
padding: 20px;
border: 1px solid #ddd;
Expand Down
44 changes: 4 additions & 40 deletions src/app/mrz-reader/mrz-reader.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Component, OnInit } from '@angular/core';
import { OverlayManager } from '../overlay';
import { CapturedResult, CaptureVisionRouter, EnumCapturedResultItemType } from 'dynamsoft-barcode-reader-bundle';
import { LabelRecognizerModule, TextLineResultItem } from 'dynamsoft-label-recognizer';
import { CodeParser, CodeParserModule } from 'dynamsoft-code-parser';
import { CodeParser, CodeParserModule, ParsedResultItem } from 'dynamsoft-code-parser';
import { handleMrzParseResult } from '../utils';

@Component({
selector: 'app-mrz-reader',
Expand Down Expand Up @@ -89,7 +90,7 @@ export class MrzReaderComponent implements OnInit {
textarea.value = txts.join('') + '\n';

let parseResults = await parser?.parse(item.text);
textarea.value += JSON.stringify(this.handleMrzParseResult(parseResults)) + '\n';
textarea.value += JSON.stringify(handleMrzParseResult(parseResults!)) + '\n';

break;
}
Expand All @@ -113,43 +114,6 @@ export class MrzReaderComponent implements OnInit {
}
}

handleMrzParseResult(result: any) {
const parseResultInfo: any = {};
if (!result.exception) {
let type = result.getFieldValue("documentCode");
parseResultInfo['Document Type'] = JSON.parse(result.jsonString).CodeType;
let nation = result.getFieldValue("issuingState");
parseResultInfo['Issuing State'] = nation;
let surName = result.getFieldValue("primaryIdentifier");
parseResultInfo['Surname'] = surName;
let givenName = result.getFieldValue("secondaryIdentifier");
parseResultInfo['Given Name'] = givenName;
let passportNumber = type === "P" ? result.getFieldValue("passportNumber") : result.getFieldValue("documentNumber");
parseResultInfo['Passport Number'] = passportNumber;
let nationality = result.getFieldValue("nationality");
parseResultInfo['Nationality'] = nationality;
let gender = result.getFieldValue("sex");
parseResultInfo["Gender"] = gender;
let birthYear = result.getFieldValue("birthYear");
let birthMonth = result.getFieldValue("birthMonth");
let birthDay = result.getFieldValue("birthDay");
if (parseInt(birthYear) > (new Date().getFullYear() % 100)) {
birthYear = "19" + birthYear;
} else {
birthYear = "20" + birthYear;
}
parseResultInfo['Date of Birth (YYYY-MM-DD)'] = birthYear + "-" + birthMonth + "-" + birthDay
let expiryYear = result.getFieldValue("expiryYear");
let expiryMonth = result.getFieldValue("expiryMonth");
let expiryDay = result.getFieldValue("expiryDay");
if (parseInt(expiryYear) >= 60) {
expiryYear = "19" + expiryYear;
} else {
expiryYear = "20" + expiryYear;
}
parseResultInfo["Date of Expiry (YYYY-MM-DD)"] = expiryYear + "-" + expiryMonth + "-" + expiryDay;
}
return parseResultInfo;
}


}
78 changes: 78 additions & 0 deletions src/app/mrz-scanner/mrz-scanner.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
.imageview {
max-width: 720px;
max-height: 720px;
width: 720px;
height: 720px;
background-color: #eaeaea;
position: relative;
}

.imageview img {
display: block;
width: 100%;
height: 100%;
object-fit: contain
}

.overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: contain
}

textarea {
width: 720px;
height: 200px;
margin: 10px auto;
padding: 20px;
border: 1px solid #ddd;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}


.container {
margin: 0 auto;
border: 1px solid #ddd;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.row {
display: flex;
justify-content: space-between;
align-items: center;
}

.loading-indicator {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
}

.spinner {
width: 50px;
height: 50px;
border: 5px solid #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
background: linear-gradient(to right, #FF0000 0%, #FF8000 17%, #FFFF00 33%, #00FF00 50%, #00FFFF 67%, #0080FF 83%, #0000FF 100%);
}

@keyframes spin {
from {
transform: rotate(0deg);
}

to {
transform: rotate(360deg);
}
}
44 changes: 44 additions & 0 deletions src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This function is used to handle the result of the MRZ parsing.
* @param result The result of the MRZ parsing.
* @returns A JSON object that contains the parsed information.
*/

import { ParsedResultItem } from "dynamsoft-code-parser";

export function handleMrzParseResult(result: ParsedResultItem): any {
const parseResultInfo: any = {};
let type = result.getFieldValue("documentCode");
parseResultInfo['Document Type'] = JSON.parse(result.jsonString).CodeType;
let nation = result.getFieldValue("issuingState");
parseResultInfo['Issuing State'] = nation;
let surName = result.getFieldValue("primaryIdentifier");
parseResultInfo['Surname'] = surName;
let givenName = result.getFieldValue("secondaryIdentifier");
parseResultInfo['Given Name'] = givenName;
let passportNumber = type === "P" ? result.getFieldValue("passportNumber") : result.getFieldValue("documentNumber");
parseResultInfo['Passport Number'] = passportNumber;
let nationality = result.getFieldValue("nationality");
parseResultInfo['Nationality'] = nationality;
let gender = result.getFieldValue("sex");
parseResultInfo["Gender"] = gender;
let birthYear = result.getFieldValue("birthYear");
let birthMonth = result.getFieldValue("birthMonth");
let birthDay = result.getFieldValue("birthDay");
if (parseInt(birthYear) > (new Date().getFullYear() % 100)) {
birthYear = "19" + birthYear;
} else {
birthYear = "20" + birthYear;
}
parseResultInfo['Date of Birth (YYYY-MM-DD)'] = birthYear + "-" + birthMonth + "-" + birthDay
let expiryYear = result.getFieldValue("expiryYear");
let expiryMonth = result.getFieldValue("expiryMonth");
let expiryDay = result.getFieldValue("expiryDay");
if (parseInt(expiryYear) >= 60) {
expiryYear = "19" + expiryYear;
} else {
expiryYear = "20" + expiryYear;
}
parseResultInfo["Date of Expiry (YYYY-MM-DD)"] = expiryYear + "-" + expiryMonth + "-" + expiryDay;
return parseResultInfo;
}

0 comments on commit 0679fa4

Please sign in to comment.