@@ -7,9 +7,7 @@ const pDataLoad = createPendingPromise();
77/** LICENSE ALERT - README
88 * To use the library, you need to first specify a license key using the API "initLicense" as shown below.
99 */
10- Dynamsoft . License . LicenseManager . initLicense (
11- "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAwLTEwMzAwNjk2NyIsIm1haW5TZXJ2ZXJVUkwiOiJodHRwczovL21sdHMuZHluYW1zb2Z0LmNvbS8iLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMCIsInNlc3Npb25QYXNzd29yZCI6IkVUSHZVNlNPV3F3ZiIsInN0YW5kYnlTZXJ2ZXJVUkwiOiJodHRwczovL3NsdHMuZHluYW1zb2Z0LmNvbS8iLCJjaGVja0NvZGUiOjM5OTMzODU2Nn0="
12- ) ;
10+ Dynamsoft . License . LicenseManager . initLicense ( "" ) ;
1311/**
1412 * You can visit https://www.dynamsoft.com/customer/license/trialLicense/?product=mrz&utm_source=samples&package=js to get your own trial license good for 30 days.
1513 * For more information, see https://www.dynamsoft.com/capture-vision/docs/web/programming/javascript/user-guide/mrz-scanner.html#specify-the-license or contact [email protected] . @@ -99,31 +97,43 @@ let init = (async () => {
9997
10098 /* Defines the result receiver for the solution.*/
10199 const resultReceiver = new Dynamsoft . CVR . CapturedResultReceiver ( ) ;
102- resultReceiver . onCapturedResultReceived = ( result ) => {
103- const recognizedResults = result . textLineResultItems ;
104- const parsedResults = result . parsedResultItems ;
105- const originalImage = result . items ?. [ 0 ] ?. imageData ;
100+ resultReceiver . onCapturedResultReceived = handleCapturedResult ;
106101
107- if ( recognizedResults ?. length ) {
108- // Play sound feedback if enabled
109- isSoundOn ? Dynamsoft . DCE . Feedback . beep ( ) : null ;
102+ await cvRouter . addResultReceiver ( resultReceiver ) ;
103+ } ) ( ) ;
104+
105+ export const handleCapturedResult = ( result , uploadedImage = null ) => {
106+ console . log ( result ) ;
107+ const recognizedResults = result . textLineResultItems ;
108+ const parsedResults = result . parsedResultItems ;
109+ const originalImage = result . items ?. [ 0 ] ?. imageData ;
110+
111+ if ( recognizedResults ?. length ) {
112+ // Play sound feedback if enabled
113+ isSoundOn ? Dynamsoft . DCE . Feedback . beep ( ) : null ;
110114
111- // Display image
112- scannedImage . textContent = "" ;
113- scannedImage . append ( originalImage . toCanvas ( ) ) ;
115+ parsedResultArea . innerText = "" ;
114116
115- const parseSuccess = displayResults ( recognizedResults [ 0 ] ?. text , parsedResults ?. [ 0 ] ) ;
117+ // Add MRZ Text to Result
118+ const mrzElement = resultToHTMLElement ( "MRZ String" , recognizedResults [ 0 ] ?. text ) ;
119+ mrzElement . classList . add ( "code" ) ;
120+ parsedResultArea . appendChild ( mrzElement ) ;
116121
117- if ( ! parseSuccess ) {
118- alert ( `Failed to parse the content.` ) ;
119- parsedResultArea . style . justifyContent = "flex-start" ;
120- }
122+ const parseSuccess = displayResults ( recognizedResults [ 0 ] ?. text , parsedResults ?. [ 0 ] ) ;
121123
122- dispose ( ) ;
124+ if ( ! parseSuccess ) {
125+ alert ( `Failed to parse the content.` ) ;
126+ parsedResultArea . style . justifyContent = "flex-start" ;
123127 }
124- } ;
125- await cvRouter . addResultReceiver ( resultReceiver ) ;
126- } ) ( ) ;
128+ displayImage ( uploadedImage || originalImage ) ;
129+
130+ dispose ( ) ;
131+ } else if ( uploadedImage ) {
132+ parsedResultArea . innerText = "No results found" ;
133+ displayImage ( uploadedImage ) ;
134+ dispose ( ) ;
135+ }
136+ } ;
127137
128138const displayResults = ( recognizedText , parsedResult ) => {
129139 parsedResultArea . innerText = "" ;
@@ -145,12 +155,33 @@ const displayResults = (recognizedText, parsedResult) => {
145155 return false ;
146156} ;
147157
158+ function displayImage ( image ) {
159+ scannedImage . textContent = "" ;
160+
161+ if ( image . type ?. startsWith ( "image/" ) ) {
162+ const img = document . createElement ( "img" ) ;
163+ const imageUrl = URL . createObjectURL ( image ) ;
164+
165+ img . src = imageUrl ;
166+ img . className = "uploaded-image" ;
167+
168+ img . onload = ( ) => {
169+ URL . revokeObjectURL ( imageUrl ) ;
170+
171+ scannedImage . append ( img ) ;
172+ } ;
173+ } else if ( image . toCanvas ) {
174+ scannedImage . append ( image . toCanvas ( ) ) ;
175+ }
176+ }
177+
148178function dispose ( ) {
149179 resultContainer . style . display = "flex" ; // Show result container
150180 cameraListContainer . style . display = "none" ; // hide header menu windows
151181 informationListContainer . style . display = "none" ;
152182 scanModeContainer . style . display = "none" ; // hide scan mode buttons
153183
184+ cameraEnhancer . close ( ) ;
154185 cvRouter . stopCapturing ( ) ;
155186 cameraView . clearAllInnerDrawingItems ( ) ;
156187}
0 commit comments