提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* @fileoverview |
|
3 |
* - Using the 'QRCode for Javascript library' |
|
4 |
* - Fixed dataset of 'QRCode for Javascript library' for support full-spec. |
|
5 |
* - this library has no dependencies. |
|
6 |
* |
|
7 |
* @author davidshimjs |
|
8 |
* @see <a href="http://www.d-project.com/" target="_blank">http://www.d-project.com/</a> |
|
9 |
* @see <a href="http://jeromeetienne.github.com/jquery-qrcode/" target="_blank">http://jeromeetienne.github.com/jquery-qrcode/</a> |
|
10 |
*/ |
|
11 |
layui.define(function (exports) { |
|
12 |
var QRCode; |
|
13 |
|
|
14 |
function QR8bitByte(data) { |
|
15 |
this.mode = QRMode.MODE_8BIT_BYTE; |
|
16 |
this.data = data; |
|
17 |
this.parsedData = []; |
|
18 |
|
|
19 |
// Added to support UTF-8 Characters |
|
20 |
for (var i = 0, l = this.data.length; i < l; i++) { |
|
21 |
var byteArray = []; |
|
22 |
var code = this.data.charCodeAt(i); |
|
23 |
|
|
24 |
if (code > 0x10000) { |
|
25 |
byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18); |
|
26 |
byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12); |
|
27 |
byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6); |
|
28 |
byteArray[3] = 0x80 | (code & 0x3F); |
|
29 |
} else if (code > 0x800) { |
|
30 |
byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12); |
|
31 |
byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6); |
|
32 |
byteArray[2] = 0x80 | (code & 0x3F); |
|
33 |
} else if (code > 0x80) { |
|
34 |
byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6); |
|
35 |
byteArray[1] = 0x80 | (code & 0x3F); |
|
36 |
} else { |
|
37 |
byteArray[0] = code; |
|
38 |
} |
|
39 |
|
|
40 |
this.parsedData.push(byteArray); |
|
41 |
} |
|
42 |
|
|
43 |
this.parsedData = Array.prototype.concat.apply([], this.parsedData); |
|
44 |
|
|
45 |
if (this.parsedData.length != this.data.length) { |
|
46 |
this.parsedData.unshift(191); |
|
47 |
this.parsedData.unshift(187); |
|
48 |
this.parsedData.unshift(239); |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
QR8bitByte.prototype = { |
|
53 |
getLength: function (buffer) { |
|
54 |
return this.parsedData.length; |
|
55 |
}, |
|
56 |
write: function (buffer) { |
|
57 |
for (var i = 0, l = this.parsedData.length; i < l; i++) { |
|
58 |
buffer.put(this.parsedData[i], 8); |
|
59 |
} |
|
60 |
} |
|
61 |
}; |
|
62 |
|
|
63 |
function QRCodeModel(typeNumber, errorCorrectLevel) { |
|
64 |
this.typeNumber = typeNumber; |
|
65 |
this.errorCorrectLevel = errorCorrectLevel; |
|
66 |
this.modules = null; |
|
67 |
this.moduleCount = 0; |
|
68 |
this.dataCache = null; |
|
69 |
this.dataList = []; |
|
70 |
} |
|
71 |
|
|
72 |
QRCodeModel.prototype = { |
|
73 |
addData: function (data) { |
|
74 |
var newData = new QR8bitByte(data); |
|
75 |
this.dataList.push(newData); |
|
76 |
this.dataCache = null; |
|
77 |
}, isDark: function (row, col) { |
|
78 |
if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) { |
|
79 |
throw new Error(row + "," + col); |
|
80 |
} |
|
81 |
return this.modules[row][col]; |
|
82 |
}, getModuleCount: function () { |
|
83 |
return this.moduleCount; |
|
84 |
}, make: function () { |
|
85 |
this.makeImpl(false, this.getBestMaskPattern()); |
|
86 |
}, makeImpl: function (test, maskPattern) { |
|
87 |
this.moduleCount = this.typeNumber * 4 + 17; |
|
88 |
this.modules = new Array(this.moduleCount); |
|
89 |
for (var row = 0; row < this.moduleCount; row++) { |
|
90 |
this.modules[row] = new Array(this.moduleCount); |
|
91 |
for (var col = 0; col < this.moduleCount; col++) { |
|
92 |
this.modules[row][col] = null; |
|
93 |
} |
|
94 |
} |
|
95 |
this.setupPositionProbePattern(0, 0); |
|
96 |
this.setupPositionProbePattern(this.moduleCount - 7, 0); |
|
97 |
this.setupPositionProbePattern(0, this.moduleCount - 7); |
|
98 |
this.setupPositionAdjustPattern(); |
|
99 |
this.setupTimingPattern(); |
|
100 |
this.setupTypeInfo(test, maskPattern); |
|
101 |
if (this.typeNumber >= 7) { |
|
102 |
this.setupTypeNumber(test); |
|
103 |
} |
|
104 |
if (this.dataCache == null) { |
|
105 |
this.dataCache = QRCodeModel.createData(this.typeNumber, this.errorCorrectLevel, this.dataList); |
|
106 |
} |
|
107 |
this.mapData(this.dataCache, maskPattern); |
|
108 |
}, setupPositionProbePattern: function (row, col) { |
|
109 |
for (var r = -1; r <= 7; r++) { |
|
110 |
if (row + r <= -1 || this.moduleCount <= row + r) continue; |
|
111 |
for (var c = -1; c <= 7; c++) { |
|
112 |
if (col + c <= -1 || this.moduleCount <= col + c) continue; |
|
113 |
if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r == 6)) || (2 <= r && r <= 4 && 2 <= c && c <= 4)) { |
|
114 |
this.modules[row + r][col + c] = true; |
|
115 |
} else { |
|
116 |
this.modules[row + r][col + c] = false; |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
}, getBestMaskPattern: function () { |
|
121 |
var minLostPoint = 0; |
|
122 |
var pattern = 0; |
|
123 |
for (var i = 0; i < 8; i++) { |
|
124 |
this.makeImpl(true, i); |
|
125 |
var lostPoint = QRUtil.getLostPoint(this); |
|
126 |
if (i == 0 || minLostPoint > lostPoint) { |
|
127 |
minLostPoint = lostPoint; |
|
128 |
pattern = i; |
|
129 |
} |
|
130 |
} |
|
131 |
return pattern; |
|
132 |
}, createMovieClip: function (target_mc, instance_name, depth) { |
|
133 |
var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth); |
|
134 |
var cs = 1; |
|
135 |
this.make(); |
|
136 |
for (var row = 0; row < this.modules.length; row++) { |
|
137 |
var y = row * cs; |
|
138 |
for (var col = 0; col < this.modules[row].length; col++) { |
|
139 |
var x = col * cs; |
|
140 |
var dark = this.modules[row][col]; |
|
141 |
if (dark) { |
|
142 |
qr_mc.beginFill(0, 100); |
|
143 |
qr_mc.moveTo(x, y); |
|
144 |
qr_mc.lineTo(x + cs, y); |
|
145 |
qr_mc.lineTo(x + cs, y + cs); |
|
146 |
qr_mc.lineTo(x, y + cs); |
|
147 |
qr_mc.endFill(); |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
return qr_mc; |
|
152 |
}, setupTimingPattern: function () { |
|
153 |
for (var r = 8; r < this.moduleCount - 8; r++) { |
|
154 |
if (this.modules[r][6] != null) { |
|
155 |
continue; |
|
156 |
} |
|
157 |
this.modules[r][6] = (r % 2 == 0); |
|
158 |
} |
|
159 |
for (var c = 8; c < this.moduleCount - 8; c++) { |
|
160 |
if (this.modules[6][c] != null) { |
|
161 |
continue; |
|
162 |
} |
|
163 |
this.modules[6][c] = (c % 2 == 0); |
|
164 |
} |
|
165 |
}, setupPositionAdjustPattern: function () { |
|
166 |
var pos = QRUtil.getPatternPosition(this.typeNumber); |
|
167 |
for (var i = 0; i < pos.length; i++) { |
|
168 |
for (var j = 0; j < pos.length; j++) { |
|
169 |
var row = pos[i]; |
|
170 |
var col = pos[j]; |
|
171 |
if (this.modules[row][col] != null) { |
|
172 |
continue; |
|
173 |
} |
|
174 |
for (var r = -2; r <= 2; r++) { |
|
175 |
for (var c = -2; c <= 2; c++) { |
|
176 |
if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) { |
|
177 |
this.modules[row + r][col + c] = true; |
|
178 |
} else { |
|
179 |
this.modules[row + r][col + c] = false; |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
}, setupTypeNumber: function (test) { |
|
186 |
var bits = QRUtil.getBCHTypeNumber(this.typeNumber); |
|
187 |
for (var i = 0; i < 18; i++) { |
|
188 |
var mod = (!test && ((bits >> i) & 1) == 1); |
|
189 |
this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod; |
|
190 |
} |
|
191 |
for (var i = 0; i < 18; i++) { |
|
192 |
var mod = (!test && ((bits >> i) & 1) == 1); |
|
193 |
this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod; |
|
194 |
} |
|
195 |
}, setupTypeInfo: function (test, maskPattern) { |
|
196 |
var data = (this.errorCorrectLevel << 3) | maskPattern; |
|
197 |
var bits = QRUtil.getBCHTypeInfo(data); |
|
198 |
for (var i = 0; i < 15; i++) { |
|
199 |
var mod = (!test && ((bits >> i) & 1) == 1); |
|
200 |
if (i < 6) { |
|
201 |
this.modules[i][8] = mod; |
|
202 |
} else if (i < 8) { |
|
203 |
this.modules[i + 1][8] = mod; |
|
204 |
} else { |
|
205 |
this.modules[this.moduleCount - 15 + i][8] = mod; |
|
206 |
} |
|
207 |
} |
|
208 |
for (var i = 0; i < 15; i++) { |
|
209 |
var mod = (!test && ((bits >> i) & 1) == 1); |
|
210 |
if (i < 8) { |
|
211 |
this.modules[8][this.moduleCount - i - 1] = mod; |
|
212 |
} else if (i < 9) { |
|
213 |
this.modules[8][15 - i - 1 + 1] = mod; |
|
214 |
} else { |
|
215 |
this.modules[8][15 - i - 1] = mod; |
|
216 |
} |
|
217 |
} |
|
218 |
this.modules[this.moduleCount - 8][8] = (!test); |
|
219 |
}, mapData: function (data, maskPattern) { |
|
220 |
var inc = -1; |
|
221 |
var row = this.moduleCount - 1; |
|
222 |
var bitIndex = 7; |
|
223 |
var byteIndex = 0; |
|
224 |
for (var col = this.moduleCount - 1; col > 0; col -= 2) { |
|
225 |
if (col == 6) col--; |
|
226 |
while (true) { |
|
227 |
for (var c = 0; c < 2; c++) { |
|
228 |
if (this.modules[row][col - c] == null) { |
|
229 |
var dark = false; |
|
230 |
if (byteIndex < data.length) { |
|
231 |
dark = (((data[byteIndex] >>> bitIndex) & 1) == 1); |
|
232 |
} |
|
233 |
var mask = QRUtil.getMask(maskPattern, row, col - c); |
|
234 |
if (mask) { |
|
235 |
dark = !dark; |
|
236 |
} |
|
237 |
this.modules[row][col - c] = dark; |
|
238 |
bitIndex--; |
|
239 |
if (bitIndex == -1) { |
|
240 |
byteIndex++; |
|
241 |
bitIndex = 7; |
|
242 |
} |
|
243 |
} |
|
244 |
} |
|
245 |
row += inc; |
|
246 |
if (row < 0 || this.moduleCount <= row) { |
|
247 |
row -= inc; |
|
248 |
inc = -inc; |
|
249 |
break; |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
} |
|
254 |
}; |
|
255 |
QRCodeModel.PAD0 = 0xEC; |
|
256 |
QRCodeModel.PAD1 = 0x11; |
|
257 |
QRCodeModel.createData = function (typeNumber, errorCorrectLevel, dataList) { |
|
258 |
var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel); |
|
259 |
var buffer = new QRBitBuffer(); |
|
260 |
for (var i = 0; i < dataList.length; i++) { |
|
261 |
var data = dataList[i]; |
|
262 |
buffer.put(data.mode, 4); |
|
263 |
buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber)); |
|
264 |
data.write(buffer); |
|
265 |
} |
|
266 |
var totalDataCount = 0; |
|
267 |
for (var i = 0; i < rsBlocks.length; i++) { |
|
268 |
totalDataCount += rsBlocks[i].dataCount; |
|
269 |
} |
|
270 |
if (buffer.getLengthInBits() > totalDataCount * 8) { |
|
271 |
throw new Error("code length overflow. (" |
|
272 |
+ buffer.getLengthInBits() |
|
273 |
+ ">" |
|
274 |
+ totalDataCount * 8 |
|
275 |
+ ")"); |
|
276 |
} |
|
277 |
if (buffer.getLengthInBits() + 4 <= totalDataCount * 8) { |
|
278 |
buffer.put(0, 4); |
|
279 |
} |
|
280 |
while (buffer.getLengthInBits() % 8 != 0) { |
|
281 |
buffer.putBit(false); |
|
282 |
} |
|
283 |
while (true) { |
|
284 |
if (buffer.getLengthInBits() >= totalDataCount * 8) { |
|
285 |
break; |
|
286 |
} |
|
287 |
buffer.put(QRCodeModel.PAD0, 8); |
|
288 |
if (buffer.getLengthInBits() >= totalDataCount * 8) { |
|
289 |
break; |
|
290 |
} |
|
291 |
buffer.put(QRCodeModel.PAD1, 8); |
|
292 |
} |
|
293 |
return QRCodeModel.createBytes(buffer, rsBlocks); |
|
294 |
}; |
|
295 |
QRCodeModel.createBytes = function (buffer, rsBlocks) { |
|
296 |
var offset = 0; |
|
297 |
var maxDcCount = 0; |
|
298 |
var maxEcCount = 0; |
|
299 |
var dcdata = new Array(rsBlocks.length); |
|
300 |
var ecdata = new Array(rsBlocks.length); |
|
301 |
for (var r = 0; r < rsBlocks.length; r++) { |
|
302 |
var dcCount = rsBlocks[r].dataCount; |
|
303 |
var ecCount = rsBlocks[r].totalCount - dcCount; |
|
304 |
maxDcCount = Math.max(maxDcCount, dcCount); |
|
305 |
maxEcCount = Math.max(maxEcCount, ecCount); |
|
306 |
dcdata[r] = new Array(dcCount); |
|
307 |
for (var i = 0; i < dcdata[r].length; i++) { |
|
308 |
dcdata[r][i] = 0xff & buffer.buffer[i + offset]; |
|
309 |
} |
|
310 |
offset += dcCount; |
|
311 |
var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount); |
|
312 |
var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1); |
|
313 |
var modPoly = rawPoly.mod(rsPoly); |
|
314 |
ecdata[r] = new Array(rsPoly.getLength() - 1); |
|
315 |
for (var i = 0; i < ecdata[r].length; i++) { |
|
316 |
var modIndex = i + modPoly.getLength() - ecdata[r].length; |
|
317 |
ecdata[r][i] = (modIndex >= 0) ? modPoly.get(modIndex) : 0; |
|
318 |
} |
|
319 |
} |
|
320 |
var totalCodeCount = 0; |
|
321 |
for (var i = 0; i < rsBlocks.length; i++) { |
|
322 |
totalCodeCount += rsBlocks[i].totalCount; |
|
323 |
} |
|
324 |
var data = new Array(totalCodeCount); |
|
325 |
var index = 0; |
|
326 |
for (var i = 0; i < maxDcCount; i++) { |
|
327 |
for (var r = 0; r < rsBlocks.length; r++) { |
|
328 |
if (i < dcdata[r].length) { |
|
329 |
data[index++] = dcdata[r][i]; |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 |
for (var i = 0; i < maxEcCount; i++) { |
|
334 |
for (var r = 0; r < rsBlocks.length; r++) { |
|
335 |
if (i < ecdata[r].length) { |
|
336 |
data[index++] = ecdata[r][i]; |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
return data; |
|
341 |
}; |
|
342 |
var QRMode = {MODE_NUMBER: 1 << 0, MODE_ALPHA_NUM: 1 << 1, MODE_8BIT_BYTE: 1 << 2, MODE_KANJI: 1 << 3}; |
|
343 |
var QRErrorCorrectLevel = {L: 1, M: 0, Q: 3, H: 2}; |
|
344 |
var QRMaskPattern = { |
|
345 |
PATTERN000: 0, |
|
346 |
PATTERN001: 1, |
|
347 |
PATTERN010: 2, |
|
348 |
PATTERN011: 3, |
|
349 |
PATTERN100: 4, |
|
350 |
PATTERN101: 5, |
|
351 |
PATTERN110: 6, |
|
352 |
PATTERN111: 7 |
|
353 |
}; |
|
354 |
var QRUtil = { |
|
355 |
PATTERN_POSITION_TABLE: [[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50], [6, 30, 54], [6, 32, 58], [6, 34, 62], [6, 26, 46, 66], [6, 26, 48, 70], [6, 26, 50, 74], [6, 30, 54, 78], [6, 30, 56, 82], [6, 30, 58, 86], [6, 34, 62, 90], [6, 28, 50, 72, 94], [6, 26, 50, 74, 98], [6, 30, 54, 78, 102], [6, 28, 54, 80, 106], [6, 32, 58, 84, 110], [6, 30, 58, 86, 114], [6, 34, 62, 90, 118], [6, 26, 50, 74, 98, 122], [6, 30, 54, 78, 102, 126], [6, 26, 52, 78, 104, 130], [6, 30, 56, 82, 108, 134], [6, 34, 60, 86, 112, 138], [6, 30, 58, 86, 114, 142], [6, 34, 62, 90, 118, 146], [6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154], [6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162], [6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170]], |
|
356 |
G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0), |
|
357 |
G18: (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0), |
|
358 |
G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1), |
|
359 |
getBCHTypeInfo: function (data) { |
|
360 |
var d = data << 10; |
|
361 |
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) { |
|
362 |
d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15))); |
|
363 |
} |
|
364 |
return ((data << 10) | d) ^ QRUtil.G15_MASK; |
|
365 |
}, |
|
366 |
getBCHTypeNumber: function (data) { |
|
367 |
var d = data << 12; |
|
368 |
while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) { |
|
369 |
d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18))); |
|
370 |
} |
|
371 |
return (data << 12) | d; |
|
372 |
}, |
|
373 |
getBCHDigit: function (data) { |
|
374 |
var digit = 0; |
|
375 |
while (data != 0) { |
|
376 |
digit++; |
|
377 |
data >>>= 1; |
|
378 |
} |
|
379 |
return digit; |
|
380 |
}, |
|
381 |
getPatternPosition: function (typeNumber) { |
|
382 |
return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1]; |
|
383 |
}, |
|
384 |
getMask: function (maskPattern, i, j) { |
|
385 |
switch (maskPattern) { |
|
386 |
case QRMaskPattern.PATTERN000: |
|
387 |
return (i + j) % 2 == 0; |
|
388 |
case QRMaskPattern.PATTERN001: |
|
389 |
return i % 2 == 0; |
|
390 |
case QRMaskPattern.PATTERN010: |
|
391 |
return j % 3 == 0; |
|
392 |
case QRMaskPattern.PATTERN011: |
|
393 |
return (i + j) % 3 == 0; |
|
394 |
case QRMaskPattern.PATTERN100: |
|
395 |
return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0; |
|
396 |
case QRMaskPattern.PATTERN101: |
|
397 |
return (i * j) % 2 + (i * j) % 3 == 0; |
|
398 |
case QRMaskPattern.PATTERN110: |
|
399 |
return ((i * j) % 2 + (i * j) % 3) % 2 == 0; |
|
400 |
case QRMaskPattern.PATTERN111: |
|
401 |
return ((i * j) % 3 + (i + j) % 2) % 2 == 0; |
|
402 |
default: |
|
403 |
throw new Error("bad maskPattern:" + maskPattern); |
|
404 |
} |
|
405 |
}, |
|
406 |
getErrorCorrectPolynomial: function (errorCorrectLength) { |
|
407 |
var a = new QRPolynomial([1], 0); |
|
408 |
for (var i = 0; i < errorCorrectLength; i++) { |
|
409 |
a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0)); |
|
410 |
} |
|
411 |
return a; |
|
412 |
}, |
|
413 |
getLengthInBits: function (mode, type) { |
|
414 |
if (1 <= type && type < 10) { |
|
415 |
switch (mode) { |
|
416 |
case QRMode.MODE_NUMBER: |
|
417 |
return 10; |
|
418 |
case QRMode.MODE_ALPHA_NUM: |
|
419 |
return 9; |
|
420 |
case QRMode.MODE_8BIT_BYTE: |
|
421 |
return 8; |
|
422 |
case QRMode.MODE_KANJI: |
|
423 |
return 8; |
|
424 |
default: |
|
425 |
throw new Error("mode:" + mode); |
|
426 |
} |
|
427 |
} else if (type < 27) { |
|
428 |
switch (mode) { |
|
429 |
case QRMode.MODE_NUMBER: |
|
430 |
return 12; |
|
431 |
case QRMode.MODE_ALPHA_NUM: |
|
432 |
return 11; |
|
433 |
case QRMode.MODE_8BIT_BYTE: |
|
434 |
return 16; |
|
435 |
case QRMode.MODE_KANJI: |
|
436 |
return 10; |
|
437 |
default: |
|
438 |
throw new Error("mode:" + mode); |
|
439 |
} |
|
440 |
} else if (type < 41) { |
|
441 |
switch (mode) { |
|
442 |
case QRMode.MODE_NUMBER: |
|
443 |
return 14; |
|
444 |
case QRMode.MODE_ALPHA_NUM: |
|
445 |
return 13; |
|
446 |
case QRMode.MODE_8BIT_BYTE: |
|
447 |
return 16; |
|
448 |
case QRMode.MODE_KANJI: |
|
449 |
return 12; |
|
450 |
default: |
|
451 |
throw new Error("mode:" + mode); |
|
452 |
} |
|
453 |
} else { |
|
454 |
throw new Error("type:" + type); |
|
455 |
} |
|
456 |
}, |
|
457 |
getLostPoint: function (qrCode) { |
|
458 |
var moduleCount = qrCode.getModuleCount(); |
|
459 |
var lostPoint = 0; |
|
460 |
for (var row = 0; row < moduleCount; row++) { |
|
461 |
for (var col = 0; col < moduleCount; col++) { |
|
462 |
var sameCount = 0; |
|
463 |
var dark = qrCode.isDark(row, col); |
|
464 |
for (var r = -1; r <= 1; r++) { |
|
465 |
if (row + r < 0 || moduleCount <= row + r) { |
|
466 |
continue; |
|
467 |
} |
|
468 |
for (var c = -1; c <= 1; c++) { |
|
469 |
if (col + c < 0 || moduleCount <= col + c) { |
|
470 |
continue; |
|
471 |
} |
|
472 |
if (r == 0 && c == 0) { |
|
473 |
continue; |
|
474 |
} |
|
475 |
if (dark == qrCode.isDark(row + r, col + c)) { |
|
476 |
sameCount++; |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
if (sameCount > 5) { |
|
481 |
lostPoint += (3 + sameCount - 5); |
|
482 |
} |
|
483 |
} |
|
484 |
} |
|
485 |
for (var row = 0; row < moduleCount - 1; row++) { |
|
486 |
for (var col = 0; col < moduleCount - 1; col++) { |
|
487 |
var count = 0; |
|
488 |
if (qrCode.isDark(row, col)) count++; |
|
489 |
if (qrCode.isDark(row + 1, col)) count++; |
|
490 |
if (qrCode.isDark(row, col + 1)) count++; |
|
491 |
if (qrCode.isDark(row + 1, col + 1)) count++; |
|
492 |
if (count == 0 || count == 4) { |
|
493 |
lostPoint += 3; |
|
494 |
} |
|
495 |
} |
|
496 |
} |
|
497 |
for (var row = 0; row < moduleCount; row++) { |
|
498 |
for (var col = 0; col < moduleCount - 6; col++) { |
|
499 |
if (qrCode.isDark(row, col) && !qrCode.isDark(row, col + 1) && qrCode.isDark(row, col + 2) && qrCode.isDark(row, col + 3) && qrCode.isDark(row, col + 4) && !qrCode.isDark(row, col + 5) && qrCode.isDark(row, col + 6)) { |
|
500 |
lostPoint += 40; |
|
501 |
} |
|
502 |
} |
|
503 |
} |
|
504 |
for (var col = 0; col < moduleCount; col++) { |
|
505 |
for (var row = 0; row < moduleCount - 6; row++) { |
|
506 |
if (qrCode.isDark(row, col) && !qrCode.isDark(row + 1, col) && qrCode.isDark(row + 2, col) && qrCode.isDark(row + 3, col) && qrCode.isDark(row + 4, col) && !qrCode.isDark(row + 5, col) && qrCode.isDark(row + 6, col)) { |
|
507 |
lostPoint += 40; |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
var darkCount = 0; |
|
512 |
for (var col = 0; col < moduleCount; col++) { |
|
513 |
for (var row = 0; row < moduleCount; row++) { |
|
514 |
if (qrCode.isDark(row, col)) { |
|
515 |
darkCount++; |
|
516 |
} |
|
517 |
} |
|
518 |
} |
|
519 |
var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5; |
|
520 |
lostPoint += ratio * 10; |
|
521 |
return lostPoint; |
|
522 |
} |
|
523 |
}; |
|
524 |
var QRMath = { |
|
525 |
glog: function (n) { |
|
526 |
if (n < 1) { |
|
527 |
throw new Error("glog(" + n + ")"); |
|
528 |
} |
|
529 |
return QRMath.LOG_TABLE[n]; |
|
530 |
}, gexp: function (n) { |
|
531 |
while (n < 0) { |
|
532 |
n += 255; |
|
533 |
} |
|
534 |
while (n >= 256) { |
|
535 |
n -= 255; |
|
536 |
} |
|
537 |
return QRMath.EXP_TABLE[n]; |
|
538 |
}, EXP_TABLE: new Array(256), LOG_TABLE: new Array(256) |
|
539 |
}; |
|
540 |
for (var i = 0; i < 8; i++) { |
|
541 |
QRMath.EXP_TABLE[i] = 1 << i; |
|
542 |
} |
|
543 |
for (var i = 8; i < 256; i++) { |
|
544 |
QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8]; |
|
545 |
} |
|
546 |
for (var i = 0; i < 255; i++) { |
|
547 |
QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i; |
|
548 |
} |
|
549 |
|
|
550 |
function QRPolynomial(num, shift) { |
|
551 |
if (num.length == undefined) { |
|
552 |
throw new Error(num.length + "/" + shift); |
|
553 |
} |
|
554 |
var offset = 0; |
|
555 |
while (offset < num.length && num[offset] == 0) { |
|
556 |
offset++; |
|
557 |
} |
|
558 |
this.num = new Array(num.length - offset + shift); |
|
559 |
for (var i = 0; i < num.length - offset; i++) { |
|
560 |
this.num[i] = num[i + offset]; |
|
561 |
} |
|
562 |
} |
|
563 |
|
|
564 |
QRPolynomial.prototype = { |
|
565 |
get: function (index) { |
|
566 |
return this.num[index]; |
|
567 |
}, getLength: function () { |
|
568 |
return this.num.length; |
|
569 |
}, multiply: function (e) { |
|
570 |
var num = new Array(this.getLength() + e.getLength() - 1); |
|
571 |
for (var i = 0; i < this.getLength(); i++) { |
|
572 |
for (var j = 0; j < e.getLength(); j++) { |
|
573 |
num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j))); |
|
574 |
} |
|
575 |
} |
|
576 |
return new QRPolynomial(num, 0); |
|
577 |
}, mod: function (e) { |
|
578 |
if (this.getLength() - e.getLength() < 0) { |
|
579 |
return this; |
|
580 |
} |
|
581 |
var ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0)); |
|
582 |
var num = new Array(this.getLength()); |
|
583 |
for (var i = 0; i < this.getLength(); i++) { |
|
584 |
num[i] = this.get(i); |
|
585 |
} |
|
586 |
for (var i = 0; i < e.getLength(); i++) { |
|
587 |
num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio); |
|
588 |
} |
|
589 |
return new QRPolynomial(num, 0).mod(e); |
|
590 |
} |
|
591 |
}; |
|
592 |
|
|
593 |
function QRRSBlock(totalCount, dataCount) { |
|
594 |
this.totalCount = totalCount; |
|
595 |
this.dataCount = dataCount; |
|
596 |
} |
|
597 |
|
|
598 |
QRRSBlock.RS_BLOCK_TABLE = [[1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9], [1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16], [1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13], [1, 100, 80], [2, 50, 32], [2, 50, 24], [4, 25, 9], [1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12], [2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15], [2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14], [2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15], [2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13], [2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16], [4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13], [2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15], [4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12], [3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13], [5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12], [5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16], [1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15], [5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15], [3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14], [3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16], [4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17], [2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13], [4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16], [6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17], [8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16], [10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17], [8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16], [3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16], [7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16], [5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16], [13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16], [17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16], [17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16], [13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17], [12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16], [6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16], [17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16], [4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16], [20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16], [19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16]]; |
|
599 |
QRRSBlock.getRSBlocks = function (typeNumber, errorCorrectLevel) { |
|
600 |
var rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel); |
|
601 |
if (rsBlock == undefined) { |
|
602 |
throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + errorCorrectLevel); |
|
603 |
} |
|
604 |
var length = rsBlock.length / 3; |
|
605 |
var list = []; |
|
606 |
for (var i = 0; i < length; i++) { |
|
607 |
var count = rsBlock[i * 3 + 0]; |
|
608 |
var totalCount = rsBlock[i * 3 + 1]; |
|
609 |
var dataCount = rsBlock[i * 3 + 2]; |
|
610 |
for (var j = 0; j < count; j++) { |
|
611 |
list.push(new QRRSBlock(totalCount, dataCount)); |
|
612 |
} |
|
613 |
} |
|
614 |
return list; |
|
615 |
}; |
|
616 |
QRRSBlock.getRsBlockTable = function (typeNumber, errorCorrectLevel) { |
|
617 |
switch (errorCorrectLevel) { |
|
618 |
case QRErrorCorrectLevel.L: |
|
619 |
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0]; |
|
620 |
case QRErrorCorrectLevel.M: |
|
621 |
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1]; |
|
622 |
case QRErrorCorrectLevel.Q: |
|
623 |
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2]; |
|
624 |
case QRErrorCorrectLevel.H: |
|
625 |
return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3]; |
|
626 |
default: |
|
627 |
return undefined; |
|
628 |
} |
|
629 |
}; |
|
630 |
|
|
631 |
function QRBitBuffer() { |
|
632 |
this.buffer = []; |
|
633 |
this.length = 0; |
|
634 |
} |
|
635 |
|
|
636 |
QRBitBuffer.prototype = { |
|
637 |
get: function (index) { |
|
638 |
var bufIndex = Math.floor(index / 8); |
|
639 |
return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) == 1; |
|
640 |
}, put: function (num, length) { |
|
641 |
for (var i = 0; i < length; i++) { |
|
642 |
this.putBit(((num >>> (length - i - 1)) & 1) == 1); |
|
643 |
} |
|
644 |
}, getLengthInBits: function () { |
|
645 |
return this.length; |
|
646 |
}, putBit: function (bit) { |
|
647 |
var bufIndex = Math.floor(this.length / 8); |
|
648 |
if (this.buffer.length <= bufIndex) { |
|
649 |
this.buffer.push(0); |
|
650 |
} |
|
651 |
if (bit) { |
|
652 |
this.buffer[bufIndex] |= (0x80 >>> (this.length % 8)); |
|
653 |
} |
|
654 |
this.length++; |
|
655 |
} |
|
656 |
}; |
|
657 |
var QRCodeLimitLength = [[17, 14, 11, 7], [32, 26, 20, 14], [53, 42, 32, 24], [78, 62, 46, 34], [106, 84, 60, 44], [134, 106, 74, 58], [154, 122, 86, 64], [192, 152, 108, 84], [230, 180, 130, 98], [271, 213, 151, 119], [321, 251, 177, 137], [367, 287, 203, 155], [425, 331, 241, 177], [458, 362, 258, 194], [520, 412, 292, 220], [586, 450, 322, 250], [644, 504, 364, 280], [718, 560, 394, 310], [792, 624, 442, 338], [858, 666, 482, 382], [929, 711, 509, 403], [1003, 779, 565, 439], [1091, 857, 611, 461], [1171, 911, 661, 511], [1273, 997, 715, 535], [1367, 1059, 751, 593], [1465, 1125, 805, 625], [1528, 1190, 868, 658], [1628, 1264, 908, 698], [1732, 1370, 982, 742], [1840, 1452, 1030, 790], [1952, 1538, 1112, 842], [2068, 1628, 1168, 898], [2188, 1722, 1228, 958], [2303, 1809, 1283, 983], [2431, 1911, 1351, 1051], [2563, 1989, 1423, 1093], [2699, 2099, 1499, 1139], [2809, 2213, 1579, 1219], [2953, 2331, 1663, 1273]]; |
|
658 |
|
|
659 |
function _isSupportCanvas() { |
|
660 |
return typeof CanvasRenderingContext2D != "undefined"; |
|
661 |
} |
|
662 |
|
|
663 |
// android 2.x doesn't support Data-URI spec |
|
664 |
function _getAndroid() { |
|
665 |
var android = false; |
|
666 |
var sAgent = navigator.userAgent; |
|
667 |
|
|
668 |
if (/android/i.test(sAgent)) { // android |
|
669 |
android = true; |
|
670 |
var aMat = sAgent.toString().match(/android ([0-9]\.[0-9])/i); |
|
671 |
|
|
672 |
if (aMat && aMat[1]) { |
|
673 |
android = parseFloat(aMat[1]); |
|
674 |
} |
|
675 |
} |
|
676 |
|
|
677 |
return android; |
|
678 |
} |
|
679 |
|
|
680 |
var svgDrawer = (function () { |
|
681 |
|
|
682 |
var Drawing = function (el, htOption) { |
|
683 |
this._el = el; |
|
684 |
this._htOption = htOption; |
|
685 |
}; |
|
686 |
|
|
687 |
Drawing.prototype.draw = function (oQRCode) { |
|
688 |
var _htOption = this._htOption; |
|
689 |
var _el = this._el; |
|
690 |
var nCount = oQRCode.getModuleCount(); |
|
691 |
var nWidth = Math.floor(_htOption.width / nCount); |
|
692 |
var nHeight = Math.floor(_htOption.height / nCount); |
|
693 |
|
|
694 |
this.clear(); |
|
695 |
|
|
696 |
function makeSVG(tag, attrs) { |
|
697 |
var el = document.createElementNS('http://www.w3.org/2000/svg', tag); |
|
698 |
for (var k in attrs) |
|
699 |
if (attrs.hasOwnProperty(k)) el.setAttribute(k, attrs[k]); |
|
700 |
return el; |
|
701 |
} |
|
702 |
|
|
703 |
var svg = makeSVG("svg", { |
|
704 |
'viewBox': '0 0 ' + String(nCount) + " " + String(nCount), |
|
705 |
'width': '100%', |
|
706 |
'height': '100%', |
|
707 |
'fill': _htOption.colorLight |
|
708 |
}); |
|
709 |
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink"); |
|
710 |
_el.appendChild(svg); |
|
711 |
|
|
712 |
svg.appendChild(makeSVG("rect", {"fill": _htOption.colorLight, "width": "100%", "height": "100%"})); |
|
713 |
svg.appendChild(makeSVG("rect", { |
|
714 |
"fill": _htOption.colorDark, |
|
715 |
"width": "1", |
|
716 |
"height": "1", |
|
717 |
"id": "template" |
|
718 |
})); |
|
719 |
|
|
720 |
for (var row = 0; row < nCount; row++) { |
|
721 |
for (var col = 0; col < nCount; col++) { |
|
722 |
if (oQRCode.isDark(row, col)) { |
|
723 |
var child = makeSVG("use", {"x": String(col), "y": String(row)}); |
|
724 |
child.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#template") |
|
725 |
svg.appendChild(child); |
|
726 |
} |
|
727 |
} |
|
728 |
} |
|
729 |
}; |
|
730 |
Drawing.prototype.clear = function () { |
|
731 |
while (this._el.hasChildNodes()) |
|
732 |
this._el.removeChild(this._el.lastChild); |
|
733 |
}; |
|
734 |
return Drawing; |
|
735 |
})(); |
|
736 |
|
|
737 |
var useSVG = document.documentElement.tagName.toLowerCase() === "svg"; |
|
738 |
|
|
739 |
// Drawing in DOM by using Table tag |
|
740 |
var Drawing = useSVG ? svgDrawer : !_isSupportCanvas() ? (function () { |
|
741 |
var Drawing = function (el, htOption) { |
|
742 |
this._el = el; |
|
743 |
this._htOption = htOption; |
|
744 |
}; |
|
745 |
|
|
746 |
/** |
|
747 |
* Draw the QRCode |
|
748 |
* |
|
749 |
* @param {QRCode} oQRCode |
|
750 |
*/ |
|
751 |
Drawing.prototype.draw = function (oQRCode) { |
|
752 |
var _htOption = this._htOption; |
|
753 |
var _el = this._el; |
|
754 |
var nCount = oQRCode.getModuleCount(); |
|
755 |
var nWidth = Math.floor(_htOption.width / nCount); |
|
756 |
var nHeight = Math.floor(_htOption.height / nCount); |
|
757 |
var aHTML = ['<table style="border:0;border-collapse:collapse;">']; |
|
758 |
|
|
759 |
for (var row = 0; row < nCount; row++) { |
|
760 |
aHTML.push('<tr>'); |
|
761 |
|
|
762 |
for (var col = 0; col < nCount; col++) { |
|
763 |
aHTML.push('<td style="border:0;border-collapse:collapse;padding:0;margin:0;width:' + nWidth + 'px;height:' + nHeight + 'px;background-color:' + (oQRCode.isDark(row, col) ? _htOption.colorDark : _htOption.colorLight) + ';"></td>'); |
|
764 |
} |
|
765 |
|
|
766 |
aHTML.push('</tr>'); |
|
767 |
} |
|
768 |
|
|
769 |
aHTML.push('</table>'); |
|
770 |
_el.innerHTML = aHTML.join(''); |
|
771 |
|
|
772 |
// Fix the margin values as real size. |
|
773 |
var elTable = _el.childNodes[0]; |
|
774 |
var nLeftMarginTable = (_htOption.width - elTable.offsetWidth) / 2; |
|
775 |
var nTopMarginTable = (_htOption.height - elTable.offsetHeight) / 2; |
|
776 |
|
|
777 |
if (nLeftMarginTable > 0 && nTopMarginTable > 0) { |
|
778 |
elTable.style.margin = nTopMarginTable + "px " + nLeftMarginTable + "px"; |
|
779 |
} |
|
780 |
}; |
|
781 |
|
|
782 |
/** |
|
783 |
* Clear the QRCode |
|
784 |
*/ |
|
785 |
Drawing.prototype.clear = function () { |
|
786 |
this._el.innerHTML = ''; |
|
787 |
}; |
|
788 |
|
|
789 |
return Drawing; |
|
790 |
})() : (function () { // Drawing in Canvas |
|
791 |
function _onMakeImage() { |
|
792 |
this._elImage.src = this._elCanvas.toDataURL("image/png"); |
|
793 |
this._elImage.style.display = "block"; |
|
794 |
this._elCanvas.style.display = "none"; |
|
795 |
} |
|
796 |
|
|
797 |
// Android 2.1 bug workaround |
|
798 |
// http://code.google.com/p/android/issues/detail?id=5141 |
|
799 |
if (this._android && this._android <= 2.1) { |
|
800 |
var factor = 1 / window.devicePixelRatio; |
|
801 |
var drawImage = CanvasRenderingContext2D.prototype.drawImage; |
|
802 |
CanvasRenderingContext2D.prototype.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) { |
|
803 |
if (("nodeName" in image) && /img/i.test(image.nodeName)) { |
|
804 |
for (var i = arguments.length - 1; i >= 1; i--) { |
|
805 |
arguments[i] = arguments[i] * factor; |
|
806 |
} |
|
807 |
} else if (typeof dw == "undefined") { |
|
808 |
arguments[1] *= factor; |
|
809 |
arguments[2] *= factor; |
|
810 |
arguments[3] *= factor; |
|
811 |
arguments[4] *= factor; |
|
812 |
} |
|
813 |
|
|
814 |
drawImage.apply(this, arguments); |
|
815 |
}; |
|
816 |
} |
|
817 |
|
|
818 |
/** |
|
819 |
* Check whether the user's browser supports Data URI or not |
|
820 |
* |
|
821 |
* @private |
|
822 |
* @param {Function} fSuccess Occurs if it supports Data URI |
|
823 |
* @param {Function} fFail Occurs if it doesn't support Data URI |
|
824 |
*/ |
|
825 |
function _safeSetDataURI(fSuccess, fFail) { |
|
826 |
var self = this; |
|
827 |
self._fFail = fFail; |
|
828 |
self._fSuccess = fSuccess; |
|
829 |
|
|
830 |
// Check it just once |
|
831 |
if (self._bSupportDataURI === null) { |
|
832 |
var el = document.createElement("img"); |
|
833 |
var fOnError = function () { |
|
834 |
self._bSupportDataURI = false; |
|
835 |
|
|
836 |
if (self._fFail) { |
|
837 |
self._fFail.call(self); |
|
838 |
} |
|
839 |
}; |
|
840 |
var fOnSuccess = function () { |
|
841 |
self._bSupportDataURI = true; |
|
842 |
|
|
843 |
if (self._fSuccess) { |
|
844 |
self._fSuccess.call(self); |
|
845 |
} |
|
846 |
}; |
|
847 |
|
|
848 |
el.onabort = fOnError; |
|
849 |
el.onerror = fOnError; |
|
850 |
el.onload = fOnSuccess; |
|
851 |
el.src = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // the Image contains 1px data. |
|
852 |
return; |
|
853 |
} else if (self._bSupportDataURI === true && self._fSuccess) { |
|
854 |
self._fSuccess.call(self); |
|
855 |
} else if (self._bSupportDataURI === false && self._fFail) { |
|
856 |
self._fFail.call(self); |
|
857 |
} |
|
858 |
}; |
|
859 |
|
|
860 |
/** |
|
861 |
* Drawing QRCode by using canvas |
|
862 |
* |
|
863 |
* @constructor |
|
864 |
* @param {HTMLElement} el |
|
865 |
* @param {Object} htOption QRCode Options |
|
866 |
*/ |
|
867 |
var Drawing = function (el, htOption) { |
|
868 |
this._bIsPainted = false; |
|
869 |
this._android = _getAndroid(); |
|
870 |
|
|
871 |
this._htOption = htOption; |
|
872 |
this._elCanvas = document.createElement("canvas"); |
|
873 |
this._elCanvas.width = htOption.width; |
|
874 |
this._elCanvas.height = htOption.height; |
|
875 |
el.appendChild(this._elCanvas); |
|
876 |
this._el = el; |
|
877 |
this._oContext = this._elCanvas.getContext("2d"); |
|
878 |
this._bIsPainted = false; |
|
879 |
this._elImage = document.createElement("img"); |
|
880 |
this._elImage.alt = "Scan me!"; |
|
881 |
this._elImage.style.display = "none"; |
|
882 |
this._el.appendChild(this._elImage); |
|
883 |
this._bSupportDataURI = null; |
|
884 |
}; |
|
885 |
|
|
886 |
/** |
|
887 |
* Draw the QRCode |
|
888 |
* |
|
889 |
* @param {QRCode} oQRCode |
|
890 |
*/ |
|
891 |
Drawing.prototype.draw = function (oQRCode) { |
|
892 |
var _elImage = this._elImage; |
|
893 |
var _oContext = this._oContext; |
|
894 |
var _htOption = this._htOption; |
|
895 |
|
|
896 |
var nCount = oQRCode.getModuleCount(); |
|
897 |
var nWidth = _htOption.width / nCount; |
|
898 |
var nHeight = _htOption.height / nCount; |
|
899 |
var nRoundedWidth = Math.round(nWidth); |
|
900 |
var nRoundedHeight = Math.round(nHeight); |
|
901 |
|
|
902 |
_elImage.style.display = "none"; |
|
903 |
this.clear(); |
|
904 |
|
|
905 |
for (var row = 0; row < nCount; row++) { |
|
906 |
for (var col = 0; col < nCount; col++) { |
|
907 |
var bIsDark = oQRCode.isDark(row, col); |
|
908 |
var nLeft = col * nWidth; |
|
909 |
var nTop = row * nHeight; |
|
910 |
_oContext.strokeStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight; |
|
911 |
_oContext.lineWidth = 1; |
|
912 |
_oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight; |
|
913 |
_oContext.fillRect(nLeft, nTop, nWidth, nHeight); |
|
914 |
|
|
915 |
// 안티 앨리어싱 방지 처리 |
|
916 |
_oContext.strokeRect( |
|
917 |
Math.floor(nLeft) + 0.5, |
|
918 |
Math.floor(nTop) + 0.5, |
|
919 |
nRoundedWidth, |
|
920 |
nRoundedHeight |
|
921 |
); |
|
922 |
|
|
923 |
_oContext.strokeRect( |
|
924 |
Math.ceil(nLeft) - 0.5, |
|
925 |
Math.ceil(nTop) - 0.5, |
|
926 |
nRoundedWidth, |
|
927 |
nRoundedHeight |
|
928 |
); |
|
929 |
} |
|
930 |
} |
|
931 |
|
|
932 |
this._bIsPainted = true; |
|
933 |
}; |
|
934 |
|
|
935 |
/** |
|
936 |
* Make the image from Canvas if the browser supports Data URI. |
|
937 |
*/ |
|
938 |
Drawing.prototype.makeImage = function () { |
|
939 |
if (this._bIsPainted) { |
|
940 |
_safeSetDataURI.call(this, _onMakeImage); |
|
941 |
} |
|
942 |
}; |
|
943 |
|
|
944 |
/** |
|
945 |
* Return whether the QRCode is painted or not |
|
946 |
* |
|
947 |
* @return {Boolean} |
|
948 |
*/ |
|
949 |
Drawing.prototype.isPainted = function () { |
|
950 |
return this._bIsPainted; |
|
951 |
}; |
|
952 |
|
|
953 |
/** |
|
954 |
* Clear the QRCode |
|
955 |
*/ |
|
956 |
Drawing.prototype.clear = function () { |
|
957 |
this._oContext.clearRect(0, 0, this._elCanvas.width, this._elCanvas.height); |
|
958 |
this._bIsPainted = false; |
|
959 |
}; |
|
960 |
|
|
961 |
/** |
|
962 |
* @private |
|
963 |
* @param {Number} nNumber |
|
964 |
*/ |
|
965 |
Drawing.prototype.round = function (nNumber) { |
|
966 |
if (!nNumber) { |
|
967 |
return nNumber; |
|
968 |
} |
|
969 |
|
|
970 |
return Math.floor(nNumber * 1000) / 1000; |
|
971 |
}; |
|
972 |
|
|
973 |
return Drawing; |
|
974 |
})(); |
|
975 |
|
|
976 |
/** |
|
977 |
* Get the type by string length |
|
978 |
* |
|
979 |
* @private |
|
980 |
* @param {String} sText |
|
981 |
* @param {Number} nCorrectLevel |
|
982 |
* @return {Number} type |
|
983 |
*/ |
|
984 |
function _getTypeNumber(sText, nCorrectLevel) { |
|
985 |
var nType = 1; |
|
986 |
var length = _getUTF8Length(sText); |
|
987 |
|
|
988 |
for (var i = 0, len = QRCodeLimitLength.length; i <= len; i++) { |
|
989 |
var nLimit = 0; |
|
990 |
|
|
991 |
switch (nCorrectLevel) { |
|
992 |
case QRErrorCorrectLevel.L : |
|
993 |
nLimit = QRCodeLimitLength[i][0]; |
|
994 |
break; |
|
995 |
case QRErrorCorrectLevel.M : |
|
996 |
nLimit = QRCodeLimitLength[i][1]; |
|
997 |
break; |
|
998 |
case QRErrorCorrectLevel.Q : |
|
999 |
nLimit = QRCodeLimitLength[i][2]; |
|
1000 |
break; |
|
1001 |
case QRErrorCorrectLevel.H : |
|
1002 |
nLimit = QRCodeLimitLength[i][3]; |
|
1003 |
break; |
|
1004 |
} |
|
1005 |
|
|
1006 |
if (length <= nLimit) { |
|
1007 |
break; |
|
1008 |
} else { |
|
1009 |
nType++; |
|
1010 |
} |
|
1011 |
} |
|
1012 |
|
|
1013 |
if (nType > QRCodeLimitLength.length) { |
|
1014 |
throw new Error("Too long data"); |
|
1015 |
} |
|
1016 |
|
|
1017 |
return nType; |
|
1018 |
} |
|
1019 |
|
|
1020 |
function _getUTF8Length(sText) { |
|
1021 |
var replacedText = encodeURI(sText).toString().replace(/\%[0-9a-fA-F]{2}/g, 'a'); |
|
1022 |
return replacedText.length + (replacedText.length != sText ? 3 : 0); |
|
1023 |
} |
|
1024 |
|
|
1025 |
/** |
|
1026 |
* @class QRCode |
|
1027 |
* @constructor |
|
1028 |
* @example |
|
1029 |
* new QRCode(document.getElementById("test"), "http://jindo.dev.naver.com/collie"); |
|
1030 |
* |
|
1031 |
* @example |
|
1032 |
* var oQRCode = new QRCode("test", { |
|
1033 |
* text : "http://naver.com", |
|
1034 |
* width : 128, |
|
1035 |
* height : 128 |
|
1036 |
* }); |
|
1037 |
* |
|
1038 |
* oQRCode.clear(); // Clear the QRCode. |
|
1039 |
* oQRCode.makeCode("http://map.naver.com"); // Re-create the QRCode. |
|
1040 |
* |
|
1041 |
* @param {HTMLElement|String} el target element or 'id' attribute of element. |
|
1042 |
* @param {Object|String} vOption |
|
1043 |
* @param {String} vOption.text QRCode link data |
|
1044 |
* @param {Number} [vOption.width=256] |
|
1045 |
* @param {Number} [vOption.height=256] |
|
1046 |
* @param {String} [vOption.colorDark="#000000"] |
|
1047 |
* @param {String} [vOption.colorLight="#ffffff"] |
|
1048 |
* @param {QRCode.CorrectLevel} [vOption.correctLevel=QRCode.CorrectLevel.H] [L|M|Q|H] |
|
1049 |
*/ |
|
1050 |
QRCode = function (el, vOption) { |
|
1051 |
this._htOption = { |
|
1052 |
width: 256, |
|
1053 |
height: 256, |
|
1054 |
typeNumber: 4, |
|
1055 |
colorDark: "#000000", |
|
1056 |
colorLight: "#ffffff", |
|
1057 |
correctLevel: QRErrorCorrectLevel.H |
|
1058 |
}; |
|
1059 |
|
|
1060 |
if (typeof vOption === 'string') { |
|
1061 |
vOption = { |
|
1062 |
text: vOption |
|
1063 |
}; |
|
1064 |
} |
|
1065 |
|
|
1066 |
// Overwrites options |
|
1067 |
if (vOption) { |
|
1068 |
for (var i in vOption) { |
|
1069 |
this._htOption[i] = vOption[i]; |
|
1070 |
} |
|
1071 |
} |
|
1072 |
|
|
1073 |
if (typeof el == "string") { |
|
1074 |
el = document.getElementById(el); |
|
1075 |
} |
|
1076 |
|
|
1077 |
if (this._htOption.useSVG) { |
|
1078 |
Drawing = svgDrawer; |
|
1079 |
} |
|
1080 |
|
|
1081 |
this._android = _getAndroid(); |
|
1082 |
this._el = el; |
|
1083 |
this._oQRCode = null; |
|
1084 |
this._oDrawing = new Drawing(this._el, this._htOption); |
|
1085 |
|
|
1086 |
if (this._htOption.text) { |
|
1087 |
this.makeCode(this._htOption.text); |
|
1088 |
} |
|
1089 |
}; |
|
1090 |
|
|
1091 |
/** |
|
1092 |
* Make the QRCode |
|
1093 |
* |
|
1094 |
* @param {String} sText link data |
|
1095 |
*/ |
|
1096 |
QRCode.prototype.makeCode = function (sText) { |
|
1097 |
this._oQRCode = new QRCodeModel(_getTypeNumber(sText, this._htOption.correctLevel), this._htOption.correctLevel); |
|
1098 |
this._oQRCode.addData(sText); |
|
1099 |
this._oQRCode.make(); |
|
1100 |
this._el.title = sText; |
|
1101 |
this._oDrawing.draw(this._oQRCode); |
|
1102 |
this.makeImage(); |
|
1103 |
}; |
|
1104 |
|
|
1105 |
/** |
|
1106 |
* Make the Image from Canvas element |
|
1107 |
* - It occurs automatically |
|
1108 |
* - Android below 3 doesn't support Data-URI spec. |
|
1109 |
* |
|
1110 |
* @private |
|
1111 |
*/ |
|
1112 |
QRCode.prototype.makeImage = function () { |
|
1113 |
if (typeof this._oDrawing.makeImage == "function" && (!this._android || this._android >= 3)) { |
|
1114 |
this._oDrawing.makeImage(); |
|
1115 |
} |
|
1116 |
}; |
|
1117 |
|
|
1118 |
/** |
|
1119 |
* Clear the QRCode |
|
1120 |
*/ |
|
1121 |
QRCode.prototype.clear = function () { |
|
1122 |
this._oDrawing.clear(); |
|
1123 |
}; |
|
1124 |
|
|
1125 |
/** |
|
1126 |
* @name QRCode.CorrectLevel |
|
1127 |
*/ |
|
1128 |
QRCode.CorrectLevel = QRErrorCorrectLevel; |
|
1129 |
|
|
1130 |
exports('QRCode', QRCode); |
|
1131 |
}); |