-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scanner.js
728 lines (622 loc) · 20.1 KB
/
Scanner.js
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
const EventEmitter = require('events');
const ZXing = require('./zxing')();
const Visibility = require('visibilityjs');
const StateMachine = require('fsm-as-promised');
// eslint-disable-next-line require-jsdoc
class ScanProvider {
constructor(emitter, analyzer, captureImage, scanPeriod, refractoryPeriod) {
this.scanPeriod = scanPeriod;
this.captureImage = captureImage;
this.refractoryPeriod = refractoryPeriod;
this._emitter = emitter;
this._frameCount = 0;
this._analyzer = analyzer;
this._lastResult = null;
this._active = false;
}
start() {
this._active = true;
requestAnimationFrame(() => this._scan());
}
stop() {
this._active = false;
}
scan() {
return this._analyze(false);
}
_analyze(skipDups) {
const analysis = this._analyzer.analyze();
if (!analysis) {
return null;
}
const {result, canvas} = analysis;
if (!result) {
return null;
}
if (skipDups && result === this._lastResult) {
return null;
}
clearTimeout(this.refractoryTimeout);
this.refractoryTimeout = setTimeout(() => {
this._lastResult = null;
}, this.refractoryPeriod);
// eslint-disable-next-line max-len
const image = this.captureImage ? canvas.toDataURL('image/webp', 0.8) : null;
this._lastResult = result;
const payload = {content: result};
if (image) {
payload.image = image;
}
return payload;
}
_scan() {
if (!this._active) {
return;
}
requestAnimationFrame(() => this._scan());
if (++this._frameCount !== this.scanPeriod) {
return;
} else {
this._frameCount = 0;
}
const result = this._analyze(true);
if (result) {
setTimeout(() => {
this._emitter.emit('scan', result.content, result.image || null);
}, 0);
}
}
}
class Analyzer {
constructor(video) {
this.video = video;
this.imageBuffer = null;
this.sensorLeft = null;
this.sensorTop = null;
this.sensorWidth = null;
this.sensorHeight = null;
this.canvas = document.createElement('canvas');
this.canvas.style.display = 'none';
this.canvasContext = null;
this.decodeCallback = ZXing.Runtime.addFunction(
(ptr, len, resultIndex, resultCount) => {
const result = new Uint8Array(ZXing.HEAPU8.buffer, ptr, len);
const str = String.fromCharCode.apply(null, result);
if (resultIndex === 0) {
window.zxDecodeResult = '';
}
window.zxDecodeResult += str;
});
}
analyze() {
if (!this.video.videoWidth) {
return null;
}
if (!this.imageBuffer) {
const videoWidth = this.video.videoWidth;
const videoHeight = this.video.videoHeight;
this.sensorWidth = videoWidth;
this.sensorHeight = videoHeight;
this.sensorLeft = Math.floor((videoWidth / 2) - (this.sensorWidth / 2));
this.sensorTop = Math.floor((videoHeight / 2) - (this.sensorHeight / 2));
this.canvas.width = this.sensorWidth;
this.canvas.height = this.sensorHeight;
this.canvasContext = this.canvas.getContext('2d');
this.imageBuffer = ZXing._resize(this.sensorWidth, this.sensorHeight);
return null;
}
this.canvasContext.drawImage(
this.video,
this.sensorLeft,
this.sensorTop,
this.sensorWidth,
this.sensorHeight,
);
// eslint-disable-next-line max-len
const data = this.canvasContext.getImageData(0, 0, this.sensorWidth, this.sensorHeight).data;
for (let i = 0, j = 0; i < data.length; i += 4, j++) {
const [r, g, b] = [data[i], data[i + 1], data[i + 2]];
ZXing.HEAPU8[this.imageBuffer + j] = Math.trunc((r + g + b) / 3);
}
const err = ZXing._decode_qr(this.decodeCallback);
if (err) {
return null;
}
const result = window.zxDecodeResult;
if (result != null) {
return {result: result, canvas: this.canvas};
}
return null;
}
}
export class Scanner extends EventEmitter {
constructor(opts) {
super();
this.video = this._configureVideo(opts);
this.mirror = (opts.mirror !== false);
this.backgroundScan = (opts.backgroundScan !== false);
this._continuous = (opts.continuous !== false);
this._analyzer = new Analyzer(this.video);
this._camera = null;
const captureImage = opts.captureImage || false;
const scanPeriod = opts.scanPeriod || 1;
const refractoryPeriod = opts.refractoryPeriod || (5 * 1000);
this._scanner = new ScanProvider(this, this._analyzer, captureImage, scanPeriod, refractoryPeriod);
this._fsm = this._createStateMachine();
Visibility.change((e, state) => {
if (state === 'visible') {
setTimeout(() => {
if (this._fsm.can('activate')) {
this._fsm.activate();
}
}, 0);
} else {
if (!this.backgroundScan && this._fsm.can('deactivate')) {
this._fsm.deactivate();
}
}
});
this.addListener('active', () => {
this.video.classList.remove('inactive');
this.video.classList.add('active');
});
this.addListener('inactive', () => {
this.video.classList.remove('active');
this.video.classList.add('inactive');
});
this.emit('inactive');
}
scan() {
return this._scanner.scan();
}
async start(camera = null) {
if (this._fsm.can('start')) {
await this._fsm.start(camera);
} else {
await this._fsm.stop();
await this._fsm.start(camera);
}
}
async stop() {
if (this._fsm.can('stop')) {
await this._fsm.stop();
}
}
set captureImage(capture) {
this._scanner.captureImage = capture;
}
get captureImage() {
return this._scanner.captureImage;
}
set scanPeriod(period) {
this._scanner.scanPeriod = period;
}
get scanPeriod() {
return this._scanner.scanPeriod;
}
set refractoryPeriod(period) {
this._scanner.refractoryPeriod = period;
}
get refractoryPeriod() {
return this._scanner.refractoryPeriod;
}
set continuous(continuous) {
this._continuous = continuous;
if (continuous && this._fsm.current === 'active') {
this._scanner.start();
} else {
this._scanner.stop();
}
}
get continuous() {
return this._continuous;
}
set mirror(mirror) {
this._mirror = mirror;
if (mirror) {
this.video.style.MozTransform = 'scaleX(-1)';
this.video.style.webkitTransform = 'scaleX(-1)';
this.video.style.OTransform = 'scaleX(-1)';
this.video.style.msFilter = 'FlipH';
this.video.style.filter = 'FlipH';
this.video.style.transform = 'scaleX(-1)';
} else {
this.video.style.MozTransform = null;
this.video.style.webkitTransform = null;
this.video.style.OTransform = null;
this.video.style.msFilter = null;
this.video.style.filter = null;
this.video.style.transform = null;
}
}
get mirror() {
return this._mirror;
}
async _enableScan(camera) {
this._camera = camera || this._camera;
if (!this._camera) {
throw new Error('Camera is not defined.');
}
const stream = await this._camera.start();
this.video.srcObject = stream;
if (this._continuous) {
this._scanner.start();
}
}
_disableScan() {
this.video.src = '';
if (this._scanner) {
this._scanner.stop();
}
if (this._camera) {
this._camera.stop();
}
}
_configureVideo(opts) {
if (opts.video) {
if (opts.video.tagName !== 'VIDEO') {
throw new Error('Video must be a <video> element.');
}
}
const video = opts.video || document.createElement('video');
video.setAttribute('autoplay', 'autoplay');
return video;
}
_createStateMachine() {
return StateMachine.create({
initial: 'stopped',
events: [
{
name: 'start',
from: 'stopped',
to: 'started',
},
{
name: 'stop',
from: ['started', 'active', 'inactive'],
to: 'stopped',
},
{
name: 'activate',
from: ['started', 'inactive'],
to: ['active', 'inactive'],
condition: function(options) {
if (Visibility.state() === 'visible' || this.backgroundScan) {
return 'active';
} else {
return 'inactive';
}
},
},
{
name: 'deactivate',
from: ['started', 'active'],
to: 'inactive',
},
],
callbacks: {
onenteractive: async (options) => {
await this._enableScan(options.args[0]);
this.emit('active');
},
onleaveactive: () => {
this._disableScan();
this.emit('inactive');
},
onenteredstarted: async (options) => {
await this._fsm.activate(options.args[0]);
},
},
});
}
}
// 1. Include scanner.js to your HTML document
// <script type="text/javascript" src="//cdn.asprise.com/scannerjs/scanner.js"></script>
// 2. Initiate A Scan
// To initiate a scan, you can simply call scanner.scan with proper arguments. For example, the code below initiates a scan that will output images in jpg format:
// -------------- Optional status display, depending on JQuery --------------
function displayStatus(loading, mesg, clear) {
if (loading) {
$('#info').html((clear ? '' : $('#info').html()) + '<p><img src="https://asprise.com/legacy/prd/asic_wia_ica/applet/loading.gif" style="vertical-align: middle;" hspace="8"> ' + mesg + '</p>');
} else {
$('#info').html((clear ? '' : $('#info').html()) + mesg);
}
}
// -------------- scanning related code: independent of any 3rd JavaScript library --------------
function scanAsJpg() {
displayStatus(true, 'Scanning', true);
scanner.scan(handleImages,
{
'output_settings': [{
'type': 'return-base64',
'format': 'jpg',
}], 'i18n': {'lang': getLang()},
}, true, false);
}
function scanSimple() {
displayStatus(true, 'Scanning', true);
scanner.scan(handleImages,
{
'prompt_scan_more': true,
'output_settings': [{
'type': 'return-base64',
'format': 'jpg',
}], 'i18n': {'lang': getLang()},
}, false, false);
}
function scanAsPdf() {
displayStatus(true, 'Scanning', true);
scanner.scan(handleImages,
{
'output_settings': [{
'type': 'return-base64',
'format': 'pdf',
}], 'i18n': {'lang': getLang()},
}, true, false);
}
function scanThenUpload() {
displayStatus(true, 'Scanning', true);
scanner.scan(handleUploadResponse,
{
'output_settings': [{
'type': 'upload',
'format': 'pdf',
'pdf_force_black_white': false,
'upload_target': {
'url': 'https://asprise.com/scan/applet/upload.php?action=dump',
'cookies': 'name=value; poweredBy=Asprise',
},
}], 'i18n': {'lang': getLang()},
}
, true, false);
}
/** Returns true if it is successfully or false if failed and reports error. */
function checkIfSuccessfully(successful, mesg, response) {
displayStatus(false, '', true);
if (successful && mesg != null && mesg.toLowerCase().indexOf('user cancel') >= 0) { // User cancelled.
displayStatus(false, '<pre>' + 'User cancelled.' + '</pre>', true);
return false;
} else if (!successful) {
displayStatus(false, '<pre>' + 'Failed: ' + mesg + '\n' + response + '</pre>', true);
return false;
}
return true;
}
/** Callback function to retrieve scanned images. Returns number of images retrieved. */
function handleImages(successful, mesg, response) {
if (!checkIfSuccessfully(successful, mesg, response)) {
return 0;
}
const scannedImages = scanner.getScannedImages(response, true, false);
displayStatus(false, '<pre>' + 'Scanned Successfully' + '</pre>', true);
for (let i = 0; (scannedImages instanceof Array) && i < scannedImages.length; i++) {
const img = scannedImages[i];
displayStatus(false, '\n<pre> ' + img.toString() + '</pre>', false);
appendImage(img, document.getElementById('images'));
const info = img.getInfo();
const barcodes = info == null ? null : info.barcodes;
for (let b = 0; Array.isArray(barcodes) && b < barcodes.length; b++) {
displayStatus(false, '\n<pre> Barcode: ' + barcodes[b].data + ' (' + barcodes[b].type + ')' + '</pre>', false);
}
}
return (scannedImages instanceof Array) ? scannedImages.length : 0;
}
function isEmpty(obj) {
if (!obj) {
return true;
}
if (typeof obj == 'string') {
return obj.trim().length == 0;
} else if (Array.isArray(obj)) {
return obj.length == 0;
}
return false;
}
/** Callback function to retrieve upload response */
function handleUploadResponse(successful, mesg, response) {
if (!checkIfSuccessfully(successful, mesg, response)) {
return 0;
}
const uploadResponse = scanner.getUploadResponse(response);
if (!isEmpty(uploadResponse)) {
displayStatus(false, '<h2>Upload Response from the Server Side: </h2>' + uploadResponse, true);
} else {
displayStatus(false, '<pre>' + 'Failed: ' + mesg + '\n' + response + '</pre>', true);
}
}
/** Displays general response to the page - for demo purpose only. */
function universalHandlerForDemo(successful, mesg, response) {
try {
if (!checkIfSuccessfully(successful, mesg, response)) {
return;
}
// Original images
const imgCount = handleImages(successful, mesg, response);
// Thumbnails
const thumbs = scanner.getScannedImages(response, false, true);
if (thumbs.length > 0) {
displayStatus(false, '<pre>' + 'Thumbnails acquired: ' + thumbs.length + '</pre>', false);
$('#info').append('<div id="thumbs"></div>');
for (let i = 0; i < thumbs.length; i++) {
const t = thumbs[i];
appendImage(t, document.getElementById('thumbs'), true);
}
}
const saveResponse = scanner.getSaveResponse(response);
if (saveResponse) {
displayStatus(false, '<h2>File Save Result: </h2>' + saveResponse, false);
}
const uploadResponse = scanner.getUploadResponse(response);
if (!isEmpty(uploadResponse)) {
displayStatus(false, '<h2>Upload Response from the Server Side: </h2>' + uploadResponse, false);
}
} catch (e) {
displayStatus(false, '<h2>Exception</h2><p>' +
(e == null ? e : e.toString().replace(/[\x26\x0A\<>'"]/g, function(r) {
return '&#' + r.charCodeAt(0) + ';';
})) +
'</p>', false);
}
}
/** To track all the images (thumbnails excluded) scanned so far. */
/** @type ScannedImage[] */
var imagesScanned = [];
/**
* Appends image to given dom node.
* @param scannedImage ScannedImage
* @param domParent
*/
function appendImage(scannedImage, domParent, isThumbnail) {
if (!scannedImage) {
return;
}
scanner.logToConsole('Appending scanned image: ' + scannedImage.toString());
if (!isThumbnail) {
imagesScanned.push(scannedImage);
}
if (scannedImage.mimeType == scanner.MIME_TYPE_BMP || scannedImage.mimeType == scanner.MIME_TYPE_GIF || scannedImage.mimeType == scanner.MIME_TYPE_JPEG || scannedImage.mimeType == scanner.MIME_TYPE_PNG) {
const elementImg = scanner.createDomElementFromModel(
{
'name': 'img',
'attributes': {
'class': 'scanned zoom thumb thumb-img',
'src': scannedImage.src,
'height': '100',
},
},
);
domParent.appendChild(elementImg);
// optional UI effect that allows the user to click the image to zoom.
enableZoom();
} else if (scannedImage.mimeType == scanner.MIME_TYPE_PDF) {
const elementA = scanner.createDomElementFromModel({
'name': 'a',
'attributes': {
'href': 'javascript:previewPdf(' + (imagesScanned.length - 1) + ');',
'class': 'thumb thumb-pdf',
},
});
domParent.appendChild(elementA);
}
}
function submitForm1() {
displayStatus(true, 'Submitting in progress, please standby ...', true);
if (!scanner.submitFormWithImages('form1', imagesScanned, function(xhr) {
if (xhr.readyState == 4) { // 4: request finished and response is ready
displayStatus(false, '<h2>Response from the server: </h2>' + xhr.responseText, true);
}
})) {
displayStatus(false, 'Form submission cancelled. Please scan an image first.', true);
}
}
function clearScans() {
if ((imagesScanned instanceof Array) && imagesScanned.length > 0) {
if (window.confirm('Are you sure that you want to clear scanned images?')) {
imagesScanned = [];
document.getElementById('images').innerHTML = '';
}
}
}
function getLang() {
const langSelected = $('#lang').val();
const licType = getParameterByName('license');
if (langSelected != 'en' && licType && (licType.indexOf('Lite') == 0 || licType.indexOf('Standard') == 0 || licType.indexOf('Pro') == 0)) {
window.alert('UI language selection is not supported by Lite and Standard licenses.');
return 'en';
}
return langSelected;
}
// Low level scanner access demos
function selectASource() {
displayStatus(true, 'Selecting a source ...', true);
scanner.getSource(handleLowLevelApiResponse, 'select', true, null, null, false, null, {
'i18n': {
'lang': getLang(),
},
});
}
function listSources() {
displayStatus(true, 'Lists all sources ...', true);
scanner.listSources(handleLowLevelApiResponse, false, 'all', false, false, null);
}
function getSourceCaps() {
displayStatus(true, 'Gets source capabilities ...', true);
scanner.getSource(handleLowLevelApiResponse, 'select', false, 'all', false, true, 'CAP_FEEDERENABLED: false; ICAP_UNITS: TWUN_INCHES', {
'i18n': {
'lang': getLang(),
},
});
}
function getSystemInfo() {
displayStatus(true, 'Gets system info ...', true);
scanner.callFunction(handleLowLevelApiResponse, 'asprise_scan_system_info');
}
function handleLowLevelApiResponse(successful, mesg, result) {
displayStatus(false, (successful ? 'OK' : 'ERROR') + '<pre>' + (mesg ? ' - ' + mesg : '') + '\n' + result + '</pre>', true);
}
$(function() {
if (window.scanner.hasJava()) {
displayStatus(false, 'JRE: ' + window.scanner.deployJava.getJREs(), false);
} else {
if (!window.scanner.isWindows()) {
displayStatus(false, '<p class=\'warn\'>Currently, only Windows is supported.</p>', false);
}
}
// Lite, Standard don't support UI language change
const licType = getParameterByName('license');
const langSelected = $('#lang').val();
if (langSelected != 'en' && licType && (licType.indexOf('Lite') == 0 || licType.indexOf('Standard') == 0 || licType.indexOf('Pro') == 0)) {
$('#lang').val('en');
}
});
// to prevent unintended window close when preview PDF
window.addEventListener('beforeunload', function(e) {
if ($('#images').children().length > 0) {
if (!window.confirm('Are you sure you want to exit scanning demo?')) {
e.preventDefault();
e.returnValue = '';
return;
}
}
});
// GET param
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
// 3. Handle The Scan Result Using A Callback Function
/** Processes the scan result */
function displayImagesOnPage(successful, mesg, response) {
if (!successful) { // On error
console.error('Failed: ' + mesg);
return;
}
if (successful && mesg != null && mesg.toLowerCase().indexOf('user cancel') >= 0) { // User canceled.
console.info('User canceled');
return;
}
const scannedImages = scanner.getScannedImages(response, true, false); // returns an array of ScannedImage
for (let i = 0; (scannedImages instanceof Array) && i < scannedImages.length; i++) {
const scannedImage = scannedImages[i];
processScannedImage(scannedImage);
}
}
/** Images scanned so far. */
var imagesScanned = [];
/** Processes a ScannedImage */
function processScannedImage(scannedImage) {
imagesScanned.push(scannedImage);
const elementImg = createDomElementFromModel({
'name': 'img',
'attributes': {
'class': 'scanned',
'src': scannedImage.src,
},
});
document.getElementById('images').appendChild(elementImg);
}