-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
9,328 additions
and
294 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
#videoview { | ||
position: absolute; | ||
width: 40vw; | ||
height: 40vh; | ||
} | ||
|
||
#videoContainer { | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
z-index: 1; | ||
} | ||
|
||
#overlay { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
z-index: 2; | ||
object-fit: contain; | ||
} | ||
|
||
max-width: 720px; | ||
max-height: 720px; | ||
width: 720px; | ||
height: 720px; | ||
background-color: #eaeaea; | ||
position: relative; | ||
} | ||
|
||
#videoContainer { | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
z-index: 1; | ||
} | ||
|
||
#overlay { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
z-index: 2; | ||
object-fit: contain; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
<p>mrz-scanner works!</p> | ||
<div id="loading-indicator" class="loading-indicator" *ngIf="!isLoaded"> | ||
<div class="spinner"></div> | ||
</div> | ||
|
||
<div class="select"> | ||
<label for="videoSource">Video source: </label> | ||
<select id="videoSource" (change)="openCamera()"></select> | ||
</div> | ||
|
||
<div class="container"> | ||
<div id="videoview"> | ||
<div class="dce-video-container" id="videoContainer"></div> | ||
<canvas id="overlay"></canvas> | ||
</div> | ||
|
||
</div> | ||
|
||
|
||
<div> | ||
<textarea id="result"></textarea> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,162 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { OverlayManager } from '../overlay'; | ||
import { CameraEnhancer, CameraView, CapturedResult, CaptureVisionRouter, EnumCapturedResultItemType, Resolution } from 'dynamsoft-barcode-reader-bundle'; | ||
import { CodeParser, CodeParserModule } from 'dynamsoft-code-parser'; | ||
import { LabelRecognizerModule, TextLineResultItem } from 'dynamsoft-label-recognizer'; | ||
import { handleMrzParseResult } from '../utils'; | ||
|
||
const componentDestroyedErrorMsg = 'VideoCapture Component Destroyed'; | ||
|
||
@Component({ | ||
selector: 'app-mrz-scanner', | ||
templateUrl: './mrz-scanner.component.html', | ||
styleUrls: ['./mrz-scanner.component.css'] | ||
}) | ||
export class MrzScannerComponent implements OnInit { | ||
isLoaded = false; | ||
overlay?: HTMLCanvasElement; | ||
context?: CanvasRenderingContext2D; | ||
cameraInfo: any = {}; | ||
videoSelect: HTMLSelectElement | undefined; | ||
overlayManager: OverlayManager; | ||
cvr?: CaptureVisionRouter; | ||
cameraEnhancer?: CameraEnhancer; | ||
parser?: CodeParser; | ||
isDestroyed = false; | ||
|
||
constructor() { } | ||
constructor() { this.overlayManager = new OverlayManager(); } | ||
|
||
ngOnInit(): void { | ||
this.videoSelect = document.querySelector('select#videoSource') as HTMLSelectElement; | ||
this.overlayManager.initOverlay(document.getElementById('overlay') as HTMLCanvasElement); | ||
(async () => { | ||
await CodeParserModule.loadSpec("MRTD_TD1_ID"); | ||
await CodeParserModule.loadSpec("MRTD_TD2_FRENCH_ID"); | ||
await CodeParserModule.loadSpec("MRTD_TD2_ID"); | ||
await CodeParserModule.loadSpec("MRTD_TD2_VISA"); | ||
await CodeParserModule.loadSpec("MRTD_TD3_PASSPORT"); | ||
await CodeParserModule.loadSpec("MRTD_TD3_VISA"); | ||
|
||
await LabelRecognizerModule.loadRecognitionData("MRZ"); | ||
this.cvr = await CaptureVisionRouter.createInstance(); | ||
this.parser = await CodeParser.createInstance(); | ||
let ret = await this.cvr.initSettings('/assets/template.json'); | ||
console.log(ret); | ||
await this.initBarcodeScanner(); | ||
})(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.isDestroyed = true; | ||
try { | ||
this.cvr?.dispose(); | ||
this.cameraEnhancer?.dispose(); | ||
} catch (_) { } | ||
} | ||
|
||
updateResolution(): void { | ||
if (this.cameraEnhancer && this.overlayManager) { | ||
let resolution: Resolution = this.cameraEnhancer.getResolution(); | ||
this.overlayManager.updateOverlay(resolution.width, resolution.height); | ||
} | ||
} | ||
|
||
async initBarcodeScanner(): Promise<void> { | ||
const cameraView: CameraView = await CameraView.createInstance(); | ||
|
||
this.cameraEnhancer = await CameraEnhancer.createInstance(cameraView); | ||
|
||
let uiElement = document.getElementById('videoContainer'); | ||
if (uiElement) { | ||
uiElement.append(cameraView.getUIElement()); | ||
|
||
cameraView.getUIElement().shadowRoot?.querySelector('.dce-sel-camera')?.setAttribute('style', 'display: none'); | ||
cameraView.getUIElement().shadowRoot?.querySelector('.dce-sel-resolution')?.setAttribute('style', 'display: none'); | ||
|
||
let cameras = await this.cameraEnhancer.getAllCameras(); | ||
this.listCameras(cameras); | ||
|
||
if (this.isDestroyed) { | ||
throw Error(componentDestroyedErrorMsg); | ||
} | ||
this.cvr?.setInput(this.cameraEnhancer); | ||
|
||
// Define a callback for results. | ||
this.cvr?.addResultReceiver({ | ||
onCapturedResultReceived: async (result: CapturedResult) => { | ||
this.overlayManager.clearOverlay(); | ||
let txts: any = []; | ||
let resultElement = document.getElementById('result'); | ||
try { | ||
let localization; | ||
let items = result.items | ||
if (items.length > 0) { | ||
for (var i = 0; i < items.length; ++i) { | ||
|
||
if (items[i].type !== EnumCapturedResultItemType.CRIT_TEXT_LINE) { | ||
continue; | ||
} | ||
|
||
let item = items[i] as TextLineResultItem; | ||
|
||
txts.push(item.text); | ||
localization = item.location; | ||
this.overlayManager.drawOverlay( | ||
localization, | ||
'' | ||
); | ||
|
||
let parseResults = await this.parser?.parse(item.text); | ||
if (resultElement) { | ||
resultElement.innerHTML = JSON.stringify(handleMrzParseResult(parseResults!)) + '\n'; | ||
} | ||
|
||
break; | ||
} | ||
|
||
} | ||
} catch (e) { | ||
alert(e); | ||
} | ||
}, | ||
}); | ||
|
||
this.cameraEnhancer.on('played', () => { | ||
this.updateResolution(); | ||
}); | ||
await this.openCamera(); | ||
if (this.isDestroyed) { | ||
throw Error(componentDestroyedErrorMsg); | ||
} | ||
await this.cvr?.startCapturing('ReadMRZ'); | ||
if (this.isDestroyed) { | ||
throw Error(componentDestroyedErrorMsg); | ||
} | ||
|
||
this.isLoaded = true; | ||
} | ||
} | ||
|
||
async openCamera(): Promise<void> { | ||
this.overlayManager.clearOverlay(); | ||
if (this.videoSelect) { | ||
let deviceId = this.videoSelect.value; | ||
if (this.cameraEnhancer) { | ||
await this.cameraEnhancer.selectCamera(deviceId); | ||
await this.cameraEnhancer.open(); | ||
} | ||
} | ||
|
||
} | ||
|
||
listCameras(deviceInfos: any): void { | ||
for (var i = 0; i < deviceInfos.length; ++i) { | ||
var deviceInfo = deviceInfos[i]; | ||
var option = document.createElement('option'); | ||
option.value = deviceInfo.deviceId; | ||
option.text = deviceInfo.label; | ||
this.cameraInfo[deviceInfo.deviceId] = deviceInfo; | ||
if (this.videoSelect) this.videoSelect.appendChild(option); | ||
} | ||
} | ||
} |