forked from mebjas/html5-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zxing-html5-qrcode-decoder.ts
155 lines (141 loc) · 5.74 KB
/
zxing-html5-qrcode-decoder.ts
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
/**
* @fileoverview
* {@interface QrcodeDecoder} wrapper around ZXing library.
*
* @author mebjas <[email protected]>
*
* ZXing library forked from https://github.com/zxing-js/library.
*
* The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
* http://www.denso-wave.com/qrcode/faqpatent-e.html
*/
import * as ZXing from "../third_party/zxing-js.umd";
import {
QrcodeResult,
QrcodeResultDebugData,
QrcodeResultFormat,
Html5QrcodeSupportedFormats,
Logger,
QrcodeDecoderAsync
} from "./core";
/**
* ZXing based Code decoder.
*/
export class ZXingHtml5QrcodeDecoder implements QrcodeDecoderAsync {
private readonly formatMap: Map<Html5QrcodeSupportedFormats, any>
= new Map([
[Html5QrcodeSupportedFormats.QR_CODE, ZXing.BarcodeFormat.QR_CODE ],
[Html5QrcodeSupportedFormats.AZTEC, ZXing.BarcodeFormat.AZTEC ],
[Html5QrcodeSupportedFormats.CODABAR, ZXing.BarcodeFormat.CODABAR ],
[Html5QrcodeSupportedFormats.CODE_39, ZXing.BarcodeFormat.CODE_39 ],
[Html5QrcodeSupportedFormats.CODE_93, ZXing.BarcodeFormat.CODE_93 ],
[
Html5QrcodeSupportedFormats.CODE_128,
ZXing.BarcodeFormat.CODE_128 ],
[
Html5QrcodeSupportedFormats.DATA_MATRIX,
ZXing.BarcodeFormat.DATA_MATRIX ],
[
Html5QrcodeSupportedFormats.MAXICODE,
ZXing.BarcodeFormat.MAXICODE ],
[Html5QrcodeSupportedFormats.ITF, ZXing.BarcodeFormat.ITF ],
[Html5QrcodeSupportedFormats.EAN_13, ZXing.BarcodeFormat.EAN_13 ],
[Html5QrcodeSupportedFormats.EAN_8, ZXing.BarcodeFormat.EAN_8 ],
[Html5QrcodeSupportedFormats.PDF_417, ZXing.BarcodeFormat.PDF_417 ],
[Html5QrcodeSupportedFormats.RSS_14, ZXing.BarcodeFormat.RSS_14 ],
[
Html5QrcodeSupportedFormats.RSS_EXPANDED,
ZXing.BarcodeFormat.RSS_EXPANDED ],
[Html5QrcodeSupportedFormats.UPC_A, ZXing.BarcodeFormat.UPC_A ],
[Html5QrcodeSupportedFormats.UPC_E, ZXing.BarcodeFormat.UPC_E ],
[
Html5QrcodeSupportedFormats.UPC_EAN_EXTENSION,
ZXing.BarcodeFormat.UPC_EAN_EXTENSION ]
]);
private readonly reverseFormatMap: Map<any, Html5QrcodeSupportedFormats>
= this.createReverseFormatMap();
private hints: Map<any, any>;
private verbose: boolean;
private logger: Logger;
public constructor(
requestedFormats: Array<Html5QrcodeSupportedFormats>,
verbose: boolean,
logger: Logger) {
if (!ZXing) {
throw "Use html5qrcode.min.js without edit, ZXing not found.";
}
this.verbose = verbose;
this.logger = logger;
const formats = this.createZXingFormats(requestedFormats);
const hints = new Map();
hints.set(ZXing.DecodeHintType.POSSIBLE_FORMATS, formats);
// TODO(minhazav): Make this configurable by developers.
hints.set(ZXing.DecodeHintType.TRY_HARDER, false);
this.hints = hints;
}
decodeAsync(canvas: HTMLCanvasElement): Promise<QrcodeResult> {
return new Promise((resolve, reject) => {
try {
resolve(this.decode(canvas));
} catch (error) {
reject(error);
}
});
}
private decode(canvas: HTMLCanvasElement): QrcodeResult {
// Note: Earlier we used to instantiate the zxingDecoder once as state
// of this class and use it for each scans. There seems to be some
// stateful bug in ZXing library around RSS_14 likely due to
// https://github.com/zxing-js/library/issues/211.
// Recreating a new instance per scan doesn't lead to performance issues
// and temporarily mitigates this issue.
// TODO(mebjas): Properly fix this issue in ZXing library.
const zxingDecoder = new ZXing.MultiFormatReader(
this.verbose, this.hints);
const luminanceSource
= new ZXing.HTMLCanvasElementLuminanceSource(canvas);
const binaryBitmap
= new ZXing.BinaryBitmap(
new ZXing.HybridBinarizer(luminanceSource));
let result = zxingDecoder.decode(binaryBitmap);
return {
text: result.text,
format: QrcodeResultFormat.create(
this.toHtml5QrcodeSupportedFormats(result.format)),
debugData: this.createDebugData()
};
}
private createReverseFormatMap(): Map<any, Html5QrcodeSupportedFormats> {
let result = new Map();
this.formatMap.forEach(
(value: any, key: Html5QrcodeSupportedFormats, _) => {
result.set(value, key);
});
return result;
}
private toHtml5QrcodeSupportedFormats(zxingFormat: any)
: Html5QrcodeSupportedFormats {
if (!this.reverseFormatMap.has(zxingFormat)) {
throw `reverseFormatMap doesn't have ${zxingFormat}`;
}
return this.reverseFormatMap.get(zxingFormat)!;
}
private createZXingFormats(
requestedFormats: Array<Html5QrcodeSupportedFormats>):
Array<any> {
let zxingFormats = [];
for (const requestedFormat of requestedFormats) {
if (this.formatMap.has(requestedFormat)) {
zxingFormats.push(
this.formatMap.get(requestedFormat));
} else {
this.logger.logError(`${requestedFormat} is not supported by`
+ "ZXingHtml5QrcodeShim");
}
}
return zxingFormats;
}
private createDebugData(): QrcodeResultDebugData {
return { decoderName: "zxing-js" };
}
}