Skip to content

Commit f91d352

Browse files
committed
Add image load error handler.
Add image load error handler.
1 parent 7f15c1a commit f91d352

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

index.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
/**
32
* EasyQRCodeJS-NodeJS
43
*
54
* NodeJS QRCode generator. Can save image or svg to file, get standard base64 image data url text or get SVG serialized text. Cross-browser QRCode generator for pure javascript. Support Dot style, Logo, Background image, Colorful, Title etc. settings. support binary mode.(Running without DOM on server side)
65
*
7-
* Version 4.2.3
6+
* Version 4.2.4
87
*
98
* @author [ [email protected] ]
109
*
@@ -112,7 +111,9 @@ QRCodeModel.prototype = {
112111
throw new Error(row + "," + col);
113112
}
114113

115-
var block = this.modules[row][col]; // [isDark(ture/false), EyeOuterOrInner(O/I), Position(TL/TR/BL/A) ]
114+
var block = this.modules[row][
115+
col
116+
]; // [isDark(ture/false), EyeOuterOrInner(O/I), Position(TL/TR/BL/A) ]
116117

117118
if (block[1]) {
118119
var type = 'P' + block[1] + '_' + block[2]; //PO_TL, PI_TL, PO_TR, PI_TR, PO_BL, PI_BL
@@ -140,7 +141,9 @@ QRCodeModel.prototype = {
140141
for (var row = 0; row < this.moduleCount; row++) {
141142
this.modules[row] = new Array(this.moduleCount);
142143
for (var col = 0; col < this.moduleCount; col++) {
143-
this.modules[row][col] = []; // [isDark(ture/false), EyeOuterOrInner(O/I), Position(TL/TR/BL) ]
144+
this.modules[row][
145+
col
146+
] = []; // [isDark(ture/false), EyeOuterOrInner(O/I), Position(TL/TR/BL) ]
144147
}
145148
}
146149
this.setupPositionProbePattern(0, 0, 'TL'); // TopLeft, TL
@@ -162,7 +165,8 @@ QRCodeModel.prototype = {
162165
if (row + r <= -1 || this.moduleCount <= row + r) continue;
163166
for (var c = -1; c <= 7; c++) {
164167
if (col + c <= -1 || this.moduleCount <= col + c) continue;
165-
if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r == 6)) || (2 <=
168+
if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r == 6)) || (
169+
2 <=
166170
r && r <= 4 &&
167171
2 <= c && c <= 4)) {
168172
this.modules[row + r][col + c][0] = true;
@@ -624,7 +628,8 @@ var QRUtil = {
624628
if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col + 2) &&
625629
qrCode.isDark(
626630
row,
627-
col + 3) && qrCode.isDark(row, col + 4) && !qrCode.isDark(row, col + 5) && qrCode.isDark(
631+
col + 3) && qrCode.isDark(row, col + 4) && !qrCode.isDark(row, col + 5) && qrCode
632+
.isDark(
628633
row, col + 6)) {
629634
lostPoint += 40;
630635
}
@@ -635,7 +640,8 @@ var QRUtil = {
635640
if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2, col) &&
636641
qrCode.isDark(
637642
row +
638-
3, col) && qrCode.isDark(row + 4, col) && !qrCode.isDark(row + 5, col) && qrCode.isDark(
643+
3, col) && qrCode.isDark(row + 4, col) && !qrCode.isDark(row + 5, col) && qrCode
644+
.isDark(
639645
row + 6, col)) {
640646
lostPoint += 40;
641647
}
@@ -677,8 +683,9 @@ for (var i = 0; i < 8; i++) {
677683
QRMath.EXP_TABLE[i] = 1 << i;
678684
}
679685
for (var i = 8; i < 256; i++) {
680-
QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[
681-
i - 8];
686+
QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath
687+
.EXP_TABLE[
688+
i - 8];
682689
}
683690
for (var i = 0; i < 255; i++) {
684691
QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
@@ -1163,10 +1170,12 @@ Drawing.prototype.draw = function(oQRCode) {
11631170

11641171
_oContext.fillRect(0, 0, t._canvas.width, _htOption.quietZone);
11651172
// left
1166-
_oContext.fillRect(0, _htOption.quietZone, _htOption.quietZone, t._canvas.height - _htOption.quietZone *
1173+
_oContext.fillRect(0, _htOption.quietZone, _htOption.quietZone, t._canvas.height - _htOption
1174+
.quietZone *
11671175
2);
11681176
// right
1169-
_oContext.fillRect(t._canvas.width - _htOption.quietZone, _htOption.quietZone, _htOption.quietZone, t._canvas
1177+
_oContext.fillRect(t._canvas.width - _htOption.quietZone, _htOption.quietZone, _htOption.quietZone, t
1178+
._canvas
11701179
.height - _htOption.quietZone * 2);
11711180
// bottom
11721181
_oContext.fillRect(0, t._canvas.height - _htOption.quietZone, t._canvas.width, _htOption.quietZone);
@@ -1187,7 +1196,7 @@ Drawing.prototype.draw = function(oQRCode) {
11871196

11881197
drawQrcode.call(t, oQRCode);
11891198
}
1190-
bgImg.onerror = function(e){
1199+
bgImg.onerror = function(e) {
11911200
t.reject(e);
11921201
}
11931202
bgImg.src = _htOption.backgroundImage;
@@ -1204,7 +1213,8 @@ Drawing.prototype.draw = function(oQRCode) {
12041213

12051214
var bIsDark = oQRCode.isDark(row, col);
12061215

1207-
var eye = oQRCode.getEye(row, col); // { isDark: true/false, type: PO_TL, PI_TL, PO_TR, PI_TR, PO_BL, PI_BL };
1216+
var eye = oQRCode.getEye(row,
1217+
col); // { isDark: true/false, type: PO_TL, PI_TL, PO_TR, PI_TR, PO_BL, PI_BL };
12081218

12091219
var nowDotScale = _htOption.dotScale;
12101220

@@ -1305,14 +1315,16 @@ Drawing.prototype.draw = function(oQRCode) {
13051315

13061316
if (_htOption.backgroundImage) {
13071317

1308-
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop +
1318+
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight +
1319+
nTop +
13091320
nHeight * (1 -
13101321
nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
13111322

13121323
} else {
13131324

13141325

1315-
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight + nTop +
1326+
_oContext.fillRect(nLeft + nWidth * (1 - nowDotScale) / 2, _htOption.titleHeight +
1327+
nTop +
13161328
nHeight * (1 -
13171329
nowDotScale) / 2, nWidth * nowDotScale, nHeight * nowDotScale);
13181330

@@ -1334,18 +1346,20 @@ Drawing.prototype.draw = function(oQRCode) {
13341346
_oContext.font = _htOption.titleFont;
13351347
_oContext.fillStyle = _htOption.titleColor;
13361348
_oContext.textAlign = 'center';
1337-
_oContext.fillText(_htOption.title, t._canvas.width / 2, this._htOption.quietZone + this._htOption.titleTop);
1349+
_oContext.fillText(_htOption.title, t._canvas.width / 2, this._htOption.quietZone + this._htOption
1350+
.titleTop);
13381351
}
13391352

13401353
if (_htOption.subTitle) {
13411354
_oContext.font = _htOption.subTitleFont;
13421355
_oContext.fillStyle = _htOption.subTitleColor;
1343-
_oContext.fillText(_htOption.subTitle, t._canvas.width / 2, this._htOption.quietZone + this._htOption.subTitleTop);
1356+
_oContext.fillText(_htOption.subTitle, t._canvas.width / 2, this._htOption.quietZone + this._htOption
1357+
.subTitleTop);
13441358
}
13451359

13461360
if (_htOption.logo) {
13471361
var img = new Image();
1348-
img.src = _htOption.logo;
1362+
13491363
var _this = this;
13501364

13511365
function generateLogoImg(img) {
@@ -1398,22 +1412,19 @@ Drawing.prototype.draw = function(oQRCode) {
13981412
_this._bIsPainted = true;
13991413
_this.makeImage();
14001414
}
1401-
1402-
1403-
14041415
img.onload = function() {
14051416
generateLogoImg(img);
14061417
}
1407-
14081418
img.onerror = function(e) {
1409-
console.error(e)
1410-
}
1411-
1412-
if (img.complete) {
1413-
img.onload = null;
1414-
generateLogoImg(img);
1415-
return;
1419+
// console.error(e);
1420+
t.reject(e);
14161421
}
1422+
img.src = _htOption.logo;
1423+
// if (img.complete) {
1424+
// img.onload = null;
1425+
// generateLogoImg(img);
1426+
// return;
1427+
// }
14171428

14181429
} else {
14191430
drawQuietZoneColor();
@@ -1471,8 +1482,6 @@ Drawing.prototype.makeImage = function() {
14711482

14721483
} else if (makeOptions.makeType == 'URL') {
14731484

1474-
1475-
14761485
if (this._htOption.onRenderingStart) {
14771486
this._htOption.onRenderingStart(this._htOption);
14781487
}
@@ -1618,7 +1627,8 @@ function QRCode(vOption) {
16181627
console.warn("Image format '" + this._htOption.format + "' is invalidate, reset to 'PNG'")
16191628
this._htOption.format = 'PNG';
16201629
}
1621-
if (this._htOption.format == 'PNG' && (this._htOption.compressionLevel < 0 || this._htOption.compressionLevel > 9)) {
1630+
if (this._htOption.format == 'PNG' && (this._htOption.compressionLevel < 0 || this._htOption.compressionLevel >
1631+
9)) {
16221632
console.warn(this._htOption.compressionLevel +
16231633
" is invalidate, PNG compressionLevel must between 0 and 9, now reset to 6. ")
16241634
this._htOption.compressionLevel = 1;
@@ -1799,4 +1809,4 @@ QRCode.prototype.toSVGText = function() {
17991809
*/
18001810
QRCode.CorrectLevel = QRErrorCorrectLevel;
18011811

1802-
module.exports = QRCode;
1812+
module.exports = QRCode;

0 commit comments

Comments
 (0)