-
Notifications
You must be signed in to change notification settings - Fork 1
/
read-image.html
242 lines (214 loc) · 9.44 KB
/
read-image.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="Read Labels from images in no time with Dynamsoft Label Recognizer. With this online demo, you can choose a file to scan the label on them.">
<meta name="keywords" content="recognize label from still images">
<title>Dynamsoft Label Recognizer Sample - Recognize an Image</title>
<script src="https://cdn.jsdelivr.net/npm/keillion-dynamsoft-label-recognizer@0.20211119151032.0/dist/dlr.js"></script>
</head>
<body>
<h1 style="font-size: 1.5em;">Recognize Label from Images</h1>
<input id="ipt-file" type="file" style="margin-bottom: 2vh;" accept="image/png,image/jpeg,image/bmp,image/gif">
<input type="text" id="result" title="Double click to clear!" readonly="true" class="latest-result" placeholder="The Last Recognize Label">
<div id="UIElement">
<img id="imgToRecognize" alt="Image to recognize" hidden /><br />
<span id='lib-load' style='font-size:x-large' hidden>Loading Library...</span>
<span id='reading' style='font-size:x-large;max-height: 10%;' hidden>Reading Labels...</span>
</div>
<div>
<span style="float:left">All Results:</span><br />
<div id="results"></div>
</div>
<script>
Dynamsoft.DLR.LabelRecognizer.engineResourcePath = "https://cdn.jsdelivr.net/npm/keillion-dynamsoft-label-recognizer@0.20211119151032.0/dist/";
let pRecognizer = null;
window.onload = async function() {
try {
Dynamsoft.DLR.LabelRecognizer.initLicense("t0068MgAAAKUZULwM1SshTkeSYEV5LPo0cnYXkSYBlpGkb0XcP1NTLvP+//NMWCbQfuyxGn1hbLfqF/bV7FuoMzNPJljQqW0=");
await Dynamsoft.DLR.LabelRecognizer.loadWasm();
} catch (ex) {
alert(ex.message);
throw ex;
}
};
document.getElementById('ipt-file').addEventListener('change', async function() {
try {
document.getElementById("imgToRecognize").hidden = true;
document.getElementById('lib-load').hidden = false;
let recognizer = await (pRecognizer = pRecognizer || Dynamsoft.DLR.LabelRecognizer.createInstance({
runtimeSettings: "passportMrz"
}));
document.getElementById('lib-load').hidden = true;
document.getElementById('reading').hidden = false;
for (let i = 0; i < this.files.length; ++i) {
// In our case, there will only be one file here (no 'multiple' attribute)
let file = this.files[i];
let time = ((new Date()).getTime()).toString();
if (FileReader) {
var fr = new FileReader();
fr.onload = function() {
document.getElementById("imgToRecognize").src = fr.result;
document.getElementById("imgToRecognize").hidden = false;
}
fr.readAsDataURL(file);
}
// Recognize the file and show results
let results = await recognizer.recognize(file);
let newElements = [];
newElements.push(createASpan(file.name + ": ", "bigger"));
newElements.push(document.createElement('br'));
newElements.push(document.createElement('br'));
if (results.length === 0) {
newElements.push(createASpan("No Label Found!"));
newElements.push(document.createElement('br'));
}
for (let result of results) {
let str1 = result.lineResults[0].text;
let str2 = result.lineResults[1].text;
let nation = str1.substring(2, 5);
if(nation[nation.length - 1] == '<') {
nation = nation.substr(0, 2);
}
console.log("Nationality: " + nation);
str1 = str1.substring(5);
var pos = str1.indexOf("<<");
let surName = str1.substring(0, pos);
console.log("Surname: " + surName);
str1 = str1.substring(surName.length + 2);
pos = str1.indexOf("<");
let givenName = str1.substring(0, pos);
str1 = str1.substring(givenName.length + 1);
pos = str1.indexOf("<<<");
givenName = givenName + " " + str1.substring(0, pos);
console.log("Given Name: " + givenName);
let passportNumber = str2.substring(0, 9);
console.log("Passport Number: " + passportNumber);
let issueCountry = str2.substr(10, 3);
if(issueCountry[issueCountry.length - 1] == '<'){
issueCountry = issueCountry.substr(0, 2);
}
console.log("Issuing Country or Organization: " + issueCountry);
let birth = str2.substr(13, 6);
var date = new Date();
currentYear = date.getFullYear();
if(birth.substr(0, 2) > (currentYear%100)){
birth = "19" + birth;
}else {
birth = "20" + birth;
}
birth = birth.slice(0, 4) + "-" + birth.slice(4, 6) + "-" + birth.slice(6);
console.log("Date of Birth: " + birth);
let gender = str2[20];
console.log("Gender: " + gender);
let expiry = str2.substr(21, 6);
if(expiry.substr(0, 2) > (currentYear%100)){
expiry = "19" + expiry;
}else {
expiry = "20" + expiry;
}
expiry = expiry.slice(0, 4) + "-" + expiry.slice(4, 6) + "-" + expiry.slice(6);
console.log("Date of Expiry: " + expiry);
for (let lineResult of result.lineResults) {
document.getElementById('result').value = "Last recognize: " + lineResult.text;
newElements.push(createASpan(lineResult.text, "resultText"));
newElements.push(document.createElement('br'));
if (lineResult.text.indexOf("Attention(exceptionCode") != -1) {
newElements.push(createASpan(" Error: " + lineResult.exception.message));
newElements.push(document.createElement('br'));
}
}
}
newElements.push(document.createElement('hr'));
for (let span of newElements) {
document.getElementById('results').appendChild(span);
}
document.getElementById('results').scrollTop = document.getElementById('results').scrollHeight;
}
document.getElementById('reading').hidden = true;
} catch (ex) {
alert(ex.message);
throw ex;
}
this.value = '';
});
document.getElementById('result').addEventListener('dblclick', async() => {
document.getElementById('result').value = "";
});
function createASpan(txt, className) {
let newSPAN = document.createElement("span");
newSPAN.textContent = txt;
if (className)
newSPAN.className = className;
return newSPAN;
}
</script>
</body>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
overflow: hidden;
width: 100vw;
height: 90vh;
color: #455A64;
margin: 0;
}
button {
font-size: 1.5rem;
margin-bottom: 2vh;
}
span {
font-size: 0.8rem;
}
.latest-result {
display: block;
margin: 0;
padding: 0.4rem 0.8rem;
color: inherit;
width: 80vw;
border: none;
font-size: 1rem;
border-radius: 0.2rem;
text-align: center;
}
.latest-result::placeholder {
color: #B0BEC5;
}
.latest-result:focus {
outline: none;
box-shadow: 0.1rem 0.4rem 0.8rem #5e35b1;
}
#results {
border: 1px dashed grey;
overflow: auto;
width: 80vw;
padding: 2vmin;
margin-bottom: 3vh;
height: 15vh;
}
.resultText {
color: #cE5E04
}
.bigger {
font-size: large;
margin-bottom: 2%;
}
#UIElement {
margin: 2vmin auto;
text-align: center;
font-size: medium;
height: 40vh;
width: 80vw;
}
#UIElement img {
max-width: 100%;
max-height: 90%;
border: solid 1px gray;
}
</style>
</html>