-
Notifications
You must be signed in to change notification settings - Fork 10
/
QRCode.ts
222 lines (199 loc) · 6.57 KB
/
QRCode.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
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
/**
* (C) Symbol Contributors 2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createCanvas } from 'canvas';
import * as QRCodeCanvas from 'qrcode';
import {
from as observableFrom,
Observable,
} from 'rxjs';
// internal dependencies
import {
CorrectionLevel,
QRCodeDataSchema,
QRCodeInterface,
QRCodeSettings,
QRCodeStreamType,
QRCodeType,
} from "../index";
import {INetworkType} from "./sdk/INetworkType";
abstract class QRCode implements QRCodeInterface {
/**
* Construct a QR Code instance out of its base64
* representation and type.
*
* @param type {QRCodeType}
* @param base64 {string}
*/
constructor(/**
* The QR Code type.
* @var {QRCodeType}
*/
public readonly type: QRCodeType,
/**
* The network ID.
* @var {number}
*/
public readonly networkType: INetworkType,
/**
* The network generation hash.
* @var {string}
*/
public readonly generationHash: string,
/**
* Whether the data is encrypted
* @var {boolean}
*/
public readonly encrypted: boolean = false,
/**
* The base64 representation of the QR Code content.
* @var {string}
*/
public readonly base64?: string
) {
}
/// region Abstract Methods
/**
* The `getSchema()` method should return an instance
* of a sub-class of QRCodeDataSchema which describes
* the QR Code data.
*
* @return {QRCodeDataSchema}
*/
public abstract getSchema(): QRCodeDataSchema;
/**
* The `getTypeNumber()` method should return the
* version number for QR codes of the underlying class.
*
* @return {number}
*/
public abstract getTypeNumber(): number;
/// end-region Abstract Methods
/**
* The `getCorrectionLevel()` method should return the
* QR Code correction level.
*
* Sub-classes may overload this method to provide with
* a different correction level.
*
* @return {number}
*/
public getCorrectionLevel(): CorrectionLevel {
return 'M';
}
/**
* The `toJSON()` method should return the JSON
* representation of the QR Code content.
*
* @return {string}
*/
public toJSON(): string {
// get the QR Code Data Schema
const schema = this.getSchema();
// create the JSON object for this QR Code
const json = schema.toObject(this);
// format to JSON
return JSON.stringify(json);
}
/**
* Generate QRcode image Base64.
*
* The returned string can be put in the `src` attribute
* of a `<img />` tag directly in HTML. The produced image
* will be a PNG.
*
* @param {QRCodeSettings} settings (Optional) Settings for generation
* @return {Observable<string>} Return image data in Base64.
*/
public toBase64(
settings: QRCodeSettings = new QRCodeSettings(),
): Observable<string> {
// get JSON representation
const json = this.toJSON();
// get base64 representation
return observableFrom(QRCodeCanvas.toDataURL(json, {
errorCorrectionLevel: settings.correctionLevel,
// do-not-set-'width'
// do-not-set-'version'
}) as Promise<string>);
}
/**
* Generate QRCode as a string. This permits to display SVG
* format, Terminal format or utf-8 format.
*
* @see https://www.npmjs.com/package/qrcode
* @param {QRCodeSettings} settings (Optional) Settings for generation
* @param {QRCodeTextType} streamType (Optional) The QR Code text type, defaults to "terminal"
* @return {Observable<string>}
*/
public toString(
settings: QRCodeSettings = new QRCodeSettings(),
streamType: QRCodeStreamType = QRCodeStreamType.Terminal,
): Observable<string> {
// get JSON representation
const json = this.toJSON();
// build the QR Code
return observableFrom(QRCodeCanvas.toString(json, {
errorCorrectionLevel: settings.correctionLevel,
width: settings.widthPixel,
type: streamType,
// do-not-set-'version'
}) as Promise<string>);
}
/**
* Generate QRCode and return object.
*
* @see https://www.npmjs.com/package/qrcode
* @param {QRCodeSettings} settings (Optional) Settings for generation
* @return {Observable<string>}
*/
public toObject(
settings: QRCodeSettings = new QRCodeSettings(),
): Observable<any> {
// get JSON representation
const json = this.toJSON();
// build the QR Code
return observableFrom([QRCodeCanvas.create(json, {
errorCorrectionLevel: settings.correctionLevel,
// do-not-set-'width'
// do-not-set-'version'
})]);
}
/**
* Generate QRCode to be printed on a `node-canvas`. This
* is compatible with the browser and node.
*
* @see https://www.npmjs.com/package/qrcode
* @see https://www.npmjs.com/package/canvas
* @param {QRCodeSettings} settings (Optional) Settings for generation
* @return {Observable<string>}
*/
public toCanvas(
settings: QRCodeSettings = new QRCodeSettings(),
): Observable<string> {
// get JSON representation
const json = this.toJSON();
// create canvas
const canvas = createCanvas(250, 250);
const context = canvas.getContext('2d');
// build the QR Code
return observableFrom(QRCodeCanvas.toCanvas(canvas, json, {
errorCorrectionLevel: settings.correctionLevel,
width: settings.widthPixel,
// do-not-set-'version'
}) as Promise<string>);
}
}
export {QRCode};