Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit bc07129

Browse files
Merge pull request #956 from telerik/kerezov/qr-image
Use qr-image for qr code generation
2 parents 6e4c95e + ebfaff5 commit bc07129

File tree

3 files changed

+14
-48
lines changed

3 files changed

+14
-48
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"progress-stream": "0.5.0",
5555
"properties-parser": "0.2.3",
5656
"pullstream": "https://github.com/icenium/node-pullstream/tarball/master",
57+
"qr-image": "3.2.0",
5758
"qrcode-generator": "1.0.0",
5859
"request": "2.81.0",
5960
"rimraf": "2.2.6",
@@ -74,6 +75,7 @@
7475
"@types/chai": "3.4.34",
7576
"@types/chai-as-promised": "0.0.29",
7677
"@types/node": "6.0.61",
78+
"@types/qr-image": "3.2.0",
7779
"@types/request": "0.0.42",
7880
"@types/semver": "5.3.30",
7981
"@types/source-map": "0.5.0",

services/qr.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,20 @@
1-
let qrcode = require("qrcode-generator");
1+
import { imageSync } from "qr-image";
2+
import { escape } from "querystring";
23

34
export class QrCodeGenerator implements IQrCodeGenerator {
4-
// The order is important.
5-
private static ERROR_CORRECTION_LEVEL = ["L", "M", "Q", "H"];
6-
7-
// https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders/Additional_information
8-
private static MAX_BLOCK_VERSION = 40;
9-
10-
constructor(private $staticConfig: Config.IStaticConfig) { }
11-
12-
public async generateQrCode(data: string): Promise<any> {
13-
let errorCorrectionLevel = "L";
14-
let errorCorrectionOffset = _.indexOf(QrCodeGenerator.ERROR_CORRECTION_LEVEL, errorCorrectionLevel);
15-
16-
// 4 is from the qrcode-generator source code.
17-
let maxReedSolomonBlockIndex = QrCodeGenerator.MAX_BLOCK_VERSION / 4 - errorCorrectionOffset;
18-
19-
for (let i = 1; i <= maxReedSolomonBlockIndex; ++i) {
20-
let qr = qrcode(i, errorCorrectionLevel);
21-
try {
22-
qr.addData(data);
23-
qr.make();
24-
} catch (ex) {
25-
let expected = "code length overflow.";
26-
if (ex.message && ex.message.substr(0, expected.length) === expected) {
27-
continue;
28-
} else {
29-
throw ex;
30-
}
31-
}
32-
33-
return qr;
34-
}
35-
36-
// Since the max Reed-Solomon block index was calculated before the for loop and no exception was thrown in it here the only error can be because of long project name.
37-
// Return null and expect the consumer to take caution in handling this case
38-
return null;
39-
}
5+
constructor(private $staticConfig: Config.IStaticConfig,
6+
private $logger: ILogger) { }
407

418
public async generateDataUri(data: string): Promise<string> {
42-
let qr = await this.generateQrCode(data);
43-
let dataUri: string = null;
44-
if (qr) {
45-
let cells = qr.getModuleCount();
46-
let size = this.$staticConfig.QR_SIZE;
47-
let cellSize = Math.ceil(size / (cells + 2 * 4 /* margin */));
48-
49-
let imgTag = qr.createImgTag(cellSize);
50-
dataUri = imgTag.split('src="')[1].split('"')[0];
9+
let result: string = null;
10+
try {
11+
const qrSvg = imageSync(data, { size: this.$staticConfig.QR_SIZE, type: "svg" }).toString();
12+
result = `data:image/svg+xml;utf-8,${escape(qrSvg)}`;
13+
} catch (err) {
14+
this.$logger.trace(`Failed to generate QR code for ${data}`, err);
5115
}
5216

53-
return dataUri;
17+
return result;
5418
}
5519
}
5620

static-config-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class StaticConfigBase implements Config.IStaticConfig {
1111
public ERROR_REPORT_SETTING_NAME: string = null;
1212
public APP_RESOURCES_DIR_NAME = "App_Resources";
1313
public COMMAND_HELP_FILE_NAME = 'command-help.json';
14-
public QR_SIZE = 300;
14+
public QR_SIZE = 5;
1515
public RESOURCE_DIR_PATH = __dirname;
1616
public SYS_REQUIREMENTS_LINK: string;
1717
public HTML_CLI_HELPERS_DIR: string;

0 commit comments

Comments
 (0)