qrcode.js
1 /*from tccdn minify at 2014-6-4 14:59:43,file:/cn/c/c/qrcode.js*/ 2 /** 3 * @fileoverview 4 * - Using the 'QRCode for Javascript library' 5 * - Fixed dataset of 'QRCode for Javascript library' for support full-spec. 6 * - this library has no dependencies. 7 * 8 * @author davidshimjs 9 * @see <a href="http://www.d-project.com/" target="_blank">http://www.d-project.com/</a> 10 * @see <a href="http://jeromeetienne.github.com/jquery-qrcode/" target="_blank">http://jeromeetienne.github.com/jquery-qrcode/</a> 11 */ 12 var QRCode; 13 14 (function () { 15 //--------------------------------------------------------------------- 16 // QRCode for JavaScript 17 // 18 // Copyright (c) 2009 Kazuhiko Arase 19 // 20 // URL: http://www.d-project.com/ 21 // 22 // Licensed under the MIT license: 23 // http://www.opensource.org/licenses/mit-license.php 24 // 25 // The word "QR Code" is registered trademark of 26 // DENSO WAVE INCORPORATED 27 // http://www.denso-wave.com/qrcode/faqpatent-e.html 28 // 29 //--------------------------------------------------------------------- 30 function QR8bitByte(data) { 31 this.mode = QRMode.MODE_8BIT_BYTE; 32 this.data = data; 33 this.parsedData = []; 34 35 // Added to support UTF-8 Characters 36 for (var i = 0, l = this.data.length; i < l; i++) { 37 var byteArray = []; 38 var code = this.data.charCodeAt(i); 39 40 if (code > 0x10000) { 41 byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18); 42 byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12); 43 byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6); 44 byteArray[3] = 0x80 | (code & 0x3F); 45 } else if (code > 0x800) { 46 byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12); 47 byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6); 48 byteArray[2] = 0x80 | (code & 0x3F); 49 } else if (code > 0x80) { 50 byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6); 51 byteArray[1] = 0x80 | (code & 0x3F); 52 } else { 53 byteArray[0] = code; 54 } 55 56 this.parsedData.push(byteArray); 57 } 58 59 this.parsedData = Array.prototype.concat.apply([], this.parsedData); 60 61 if (this.parsedData.length != this.data.length) { 62 this.parsedData.unshift(191); 63 this.parsedData.unshift(187); 64 this.parsedData.unshift(239); 65 } 66 } 67 68 QR8bitByte.prototype = { 69 getLength: function (buffer) { 70 return this.parsedData.length; 71 }, 72 write: function (buffer) { 73 for (var i = 0, l = this.parsedData.length; i < l; i++) { 74 buffer.put(this.parsedData[i], 8); 75 } 76 } 77 }; 78 79 function QRCodeModel(typeNumber, errorCorrectLevel) { 80 this.typeNumber = typeNumber; 81 this.errorCorrectLevel = errorCorrectLevel; 82 this.modules = null; 83 this.moduleCount = 0; 84 this.dataCache = null; 85 this.dataList = []; 86 } 87 88 function QRPolynomial(num, shift) { 89 if (num.length == undefined) throw new Error(num.length + "/" + shift); 90 var offset = 0; 91 while (offset < num.length && num[offset] == 0) offset++; 92 this.num = new Array(num.length - offset + shift); 93 for (var i = 0; i < num.length - offset; i++) this.num[i] = num[i + offset]; 94 } 95 96 function QRRSBlock(totalCount, dataCount) { 97 this.totalCount = totalCount, this.dataCount = dataCount; 98 } 99 100 function QRBitBuffer() { 101 this.buffer = [], this.length = 0; 102 } 103 104 QRCodeModel.prototype = { 105 "addData": function(data) { 106 var newData = new QR8bitByte(data); 107 this.dataList.push(newData), this.dataCache = null; 108 }, 109 "isDark": function(row, col) { 110 if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) throw new Error(row + "," + col); 111 return this.modules[row][col]; 112 }, 113 "getModuleCount": function() { 114 return this.moduleCount; 115 }, 116 "make": function() { 117 this.makeImpl(!1, this.getBestMaskPattern()); 118 }, 119 "makeImpl": function(test, maskPattern) { 120 this.moduleCount = this.typeNumber * 4 + 17, this.modules = new Array(this.moduleCount); 121 for (var row = 0; row < this.moduleCount; row++) { 122 this.modules[row] = new Array(this.moduleCount); 123 for (var col = 0; col < this.moduleCount; col++) this.modules[row][col] = null; 124 } 125 this.setupPositionProbePattern(0, 0), 126 this.setupPositionProbePattern(this.moduleCount - 7, 0), 127 this.setupPositionProbePattern(0, this.moduleCount - 7), 128 this.setupPositionAdjustPattern(), this.setupTimingPattern(), 129 this.setupTypeInfo(test, maskPattern), 130 this.typeNumber >= 7 && this.setupTypeNumber(test), 131 this.dataCache == null && (this.dataCache = QRCodeModel.createData(this.typeNumber, this.errorCorrectLevel, this.dataList)), this.mapData(this.dataCache, maskPattern); 132 }, 133 "setupPositionProbePattern": function(row, col) { 134 for (var r = -1; r <= 7; r++) { 135 if (row + r <= -1 || this.moduleCount <= row + r) continue; 136 for (var c = -1; c <= 7; c++) { 137 if (col + c <= -1 || this.moduleCount <= col + c) continue; 138 r <= 6 && (c == 0 || c == 6) || 0 <= c && c <= 6 && (r == 0 || r == 6) || 2 <= r && r <= 4 && 2 <= c && c <= 4 ? this.modules[row + r][col + c] = !0 : this.modules[row + r][col + c] = !1; 139 } 140 } 141 }, 142 "getBestMaskPattern": function() { 143 var minLostPoint = 0, pattern = 0; 144 for (var i = 0; i < 8; i++) { 145 this.makeImpl(!0, i); 146 var lostPoint = QRUtil.getLostPoint(this); 147 if (i == 0 || minLostPoint > lostPoint) minLostPoint = lostPoint, pattern = i; 148 } 149 return pattern; 150 }, 151 "createMovieClip": function(target_mc, instance_name, depth) { 152 var qr_mc = target_mc.createEmptyMovieClip(instance_name, depth), cs = 1; 153 this.make(); 154 for (var row = 0; row < this.modules.length; row++) { 155 var y = row * cs; 156 for (var col = 0; col < this.modules[row].length; col++) { 157 var x = col * cs, dark = this.modules[row][col]; 158 dark && (qr_mc.beginFill(0, 100), qr_mc.moveTo(x, y), qr_mc.lineTo(x + cs, y), qr_mc.lineTo(x + cs, y + cs), qr_mc.lineTo(x, y + cs), qr_mc.endFill()); 159 } 160 } 161 return qr_mc; 162 }, 163 "setupTimingPattern": function() { 164 for (var r = 8; r < this.moduleCount - 8; r++) { 165 if (this.modules[r][6] != null) continue; 166 this.modules[r][6] = r % 2 == 0; 167 } 168 for (var c = 8; c < this.moduleCount - 8; c++) { 169 if (this.modules[6][c] != null) continue; 170 this.modules[6][c] = c % 2 == 0; 171 } 172 }, 173 "setupPositionAdjustPattern": function() { 174 var pos = QRUtil.getPatternPosition(this.typeNumber); 175 for (var i = 0; i < pos.length; i++) for (var j = 0; j < pos.length; j++) { 176 var row = pos[i], col = pos[j]; 177 if (this.modules[row][col] != null) continue; 178 for (var r = -2; r <= 2; r++) for (var c = -2; c <= 2; c++) r == -2 || r == 2 || c == -2 || c == 2 || r == 0 && c == 0 ? this.modules[row + r][col + c] = !0 : this.modules[row + r][col + c] = !1; 179 } 180 }, 181 "setupTypeNumber": function(test) { 182 var bits = QRUtil.getBCHTypeNumber(this.typeNumber); 183 for (var i = 0; i < 18; i++) { 184 var mod = !test && (bits >> i & 1) == 1; 185 this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod; 186 } 187 for (var i = 0; i < 18; i++) { 188 var mod = !test && (bits >> i & 1) == 1; 189 this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod; 190 } 191 }, 192 "setupTypeInfo": function(test, maskPattern) { 193 var data = this.errorCorrectLevel << 3 | maskPattern, bits = QRUtil.getBCHTypeInfo(data); 194 for (var i = 0; i < 15; i++) { 195 var mod = !test && (bits >> i & 1) == 1; 196 i < 6 ? this.modules[i][8] = mod : i < 8 ? this.modules[i + 1][8] = mod : this.modules[this.moduleCount - 15 + i][8] = mod; 197 } 198 for (var i = 0; i < 15; i++) { 199 var mod = !test && (bits >> i & 1) == 1; 200 i < 8 ? this.modules[8][this.moduleCount - i - 1] = mod : i < 9 ? this.modules[8][15 - i - 1 + 1] = mod : this.modules[8][15 - i - 1] = mod; 201 } 202 this.modules[this.moduleCount - 8][8] = !test; 203 }, 204 "mapData": function(data, maskPattern) { 205 var inc = -1, row = this.moduleCount - 1, bitIndex = 7, byteIndex = 0; 206 for (var col = this.moduleCount - 1; col > 0; col -= 2) { 207 col == 6 && col--; 208 for (;;) { 209 for (var c = 0; c < 2; c++) if (this.modules[row][col - c] == null) { 210 var dark = !1; 211 byteIndex < data.length && (dark = (data[byteIndex] >>> bitIndex & 1) == 1); 212 var mask = QRUtil.getMask(maskPattern, row, col - c); 213 mask && (dark = !dark), this.modules[row][col - c] = dark, bitIndex--, bitIndex == -1 && (byteIndex++, bitIndex = 7); 214 } 215 row += inc; 216 if (row < 0 || this.moduleCount <= row) { 217 row -= inc, inc = -inc; 218 break; 219 } 220 } 221 } 222 } 223 }, QRCodeModel.PAD0 = 236, QRCodeModel.PAD1 = 17, QRCodeModel.createData = function(typeNumber, errorCorrectLevel, dataList) { 224 var rsBlocks = QRRSBlock.getRSBlocks(typeNumber, errorCorrectLevel), buffer = new QRBitBuffer; 225 for (var i = 0; i < dataList.length; i++) { 226 var data = dataList[i]; 227 buffer.put(data.mode, 4), buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, typeNumber)), data.write(buffer); 228 } 229 var totalDataCount = 0; 230 for (var i = 0; i < rsBlocks.length; i++) totalDataCount += rsBlocks[i].dataCount; 231 if (buffer.getLengthInBits() > totalDataCount * 8) throw new Error("code length overflow. (" + buffer.getLengthInBits() + ">" + totalDataCount * 8 + ")"); 232 buffer.getLengthInBits() + 4 <= totalDataCount * 8 && buffer.put(0, 4); 233 while (buffer.getLengthInBits() % 8 != 0) buffer.putBit(!1); 234 for (;;) { 235 if (buffer.getLengthInBits() >= totalDataCount * 8) break; 236 buffer.put(QRCodeModel.PAD0, 8); 237 if (buffer.getLengthInBits() >= totalDataCount * 8) break; 238 buffer.put(QRCodeModel.PAD1, 8); 239 } 240 return QRCodeModel.createBytes(buffer, rsBlocks); 241 }, QRCodeModel.createBytes = function(buffer, rsBlocks) { 242 var offset = 0, maxDcCount = 0, maxEcCount = 0, dcdata = new Array(rsBlocks.length), ecdata = new Array(rsBlocks.length); 243 for (var r = 0; r < rsBlocks.length; r++) { 244 var dcCount = rsBlocks[r].dataCount, ecCount = rsBlocks[r].totalCount - dcCount; 245 maxDcCount = Math.max(maxDcCount, dcCount), maxEcCount = Math.max(maxEcCount, ecCount), dcdata[r] = new Array(dcCount); 246 for (var i = 0; i < dcdata[r].length; i++) dcdata[r][i] = 255 & buffer.buffer[i + offset]; 247 offset += dcCount; 248 var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount), rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1), modPoly = rawPoly.mod(rsPoly); 249 ecdata[r] = new Array(rsPoly.getLength() - 1); 250 for (var i = 0; i < ecdata[r].length; i++) { 251 var modIndex = i + modPoly.getLength() - ecdata[r].length; 252 ecdata[r][i] = modIndex >= 0 ? modPoly.get(modIndex) : 0; 253 } 254 } 255 var totalCodeCount = 0; 256 for (var i = 0; i < rsBlocks.length; i++) totalCodeCount += rsBlocks[i].totalCount; 257 var data = new Array(totalCodeCount), index = 0; 258 for (var i = 0; i < maxDcCount; i++) for (var r = 0; r < rsBlocks.length; r++) i < dcdata[r].length && (data[index++] = dcdata[r][i]); 259 for (var i = 0; i < maxEcCount; i++) for (var r = 0; r < rsBlocks.length; r++) i < ecdata[r].length && (data[index++] = ecdata[r][i]); 260 return data; 261 }; 262 263 var QRMode = { 264 "MODE_NUMBER": 1, 265 "MODE_ALPHA_NUM": 2, 266 "MODE_8BIT_BYTE": 4, 267 "MODE_KANJI": 8 268 }, QRErrorCorrectLevel = { 269 "L": 1, 270 "M": 0, 271 "Q": 3, 272 "H": 2 273 }, QRMaskPattern = { 274 "PATTERN000": 0, 275 "PATTERN001": 1, 276 "PATTERN010": 2, 277 "PATTERN011": 3, 278 "PATTERN100": 4, 279 "PATTERN101": 5, 280 "PATTERN110": 6, 281 "PATTERN111": 7 282 }, QRUtil = { 283 "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 ] ], 284 "G15": 1335, 285 "G18": 7973, 286 "G15_MASK": 21522, 287 "getBCHTypeInfo": function(data) { 288 var d = data << 10; 289 while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) d ^= QRUtil.G15 << QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15); 290 return (data << 10 | d) ^ QRUtil.G15_MASK; 291 }, 292 "getBCHTypeNumber": function(data) { 293 var d = data << 12; 294 while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) d ^= QRUtil.G18 << QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18); 295 return data << 12 | d; 296 }, 297 "getBCHDigit": function(data) { 298 var digit = 0; 299 while (data != 0) digit++, data >>>= 1; 300 return digit; 301 }, 302 "getPatternPosition": function(typeNumber) { 303 return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1]; 304 }, 305 "getMask": function(maskPattern, i, j) { 306 switch (maskPattern) { 307 case QRMaskPattern.PATTERN000: 308 return (i + j) % 2 == 0; 309 case QRMaskPattern.PATTERN001: 310 return i % 2 == 0; 311 case QRMaskPattern.PATTERN010: 312 return j % 3 == 0; 313 case QRMaskPattern.PATTERN011: 314 return (i + j) % 3 == 0; 315 case QRMaskPattern.PATTERN100: 316 return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0; 317 case QRMaskPattern.PATTERN101: 318 return i * j % 2 + i * j % 3 == 0; 319 case QRMaskPattern.PATTERN110: 320 return (i * j % 2 + i * j % 3) % 2 == 0; 321 case QRMaskPattern.PATTERN111: 322 return (i * j % 3 + (i + j) % 2) % 2 == 0; 323 default: 324 throw new Error("bad maskPattern:" + maskPattern); 325 } 326 }, 327 "getErrorCorrectPolynomial": function(errorCorrectLength) { 328 var a = new QRPolynomial([ 1 ], 0); 329 for (var i = 0; i < errorCorrectLength; i++) a = a.multiply(new QRPolynomial([ 1, QRMath.gexp(i) ], 0)); 330 return a; 331 }, 332 "getLengthInBits": function(mode, type) { 333 if (1 <= type && type < 10) switch (mode) { 334 case QRMode.MODE_NUMBER: 335 return 10; 336 case QRMode.MODE_ALPHA_NUM: 337 return 9; 338 case QRMode.MODE_8BIT_BYTE: 339 return 8; 340 case QRMode.MODE_KANJI: 341 return 8; 342 default: 343 throw new Error("mode:" + mode); 344 } else if (type < 27) switch (mode) { 345 case QRMode.MODE_NUMBER: 346 return 12; 347 case QRMode.MODE_ALPHA_NUM: 348 return 11; 349 case QRMode.MODE_8BIT_BYTE: 350 return 16; 351 case QRMode.MODE_KANJI: 352 return 10; 353 default: 354 throw new Error("mode:" + mode); 355 } else { 356 if (!(type < 41)) throw new Error("type:" + type); 357 switch (mode) { 358 case QRMode.MODE_NUMBER: 359 return 14; 360 case QRMode.MODE_ALPHA_NUM: 361 return 13; 362 case QRMode.MODE_8BIT_BYTE: 363 return 16; 364 case QRMode.MODE_KANJI: 365 return 12; 366 default: 367 throw new Error("mode:" + mode); 368 } 369 } 370 }, 371 "getLostPoint": function(qrCode) { 372 var moduleCount = qrCode.getModuleCount(), lostPoint = 0; 373 for (var row = 0; row < moduleCount; row++) for (var col = 0; col < moduleCount; col++) { 374 var sameCount = 0, dark = qrCode.isDark(row, col); 375 for (var r = -1; r <= 1; r++) { 376 if (row + r < 0 || moduleCount <= row + r) continue; 377 for (var c = -1; c <= 1; c++) { 378 if (col + c < 0 || moduleCount <= col + c) continue; 379 if (r == 0 && c == 0) continue; 380 dark == qrCode.isDark(row + r, col + c) && sameCount++; 381 } 382 } 383 sameCount > 5 && (lostPoint += 3 + sameCount - 5); 384 } 385 for (var row = 0; row < moduleCount - 1; row++) for (var col = 0; col < moduleCount - 1; col++) { 386 var count = 0; 387 qrCode.isDark(row, col) && count++, qrCode.isDark(row + 1, col) && count++, qrCode.isDark(row, col + 1) && count++, qrCode.isDark(row + 1, col + 1) && count++; 388 if (count == 0 || count == 4) lostPoint += 3; 389 } 390 for (var row = 0; row < moduleCount; row++) for (var col = 0; col < moduleCount - 6; col++) 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) && (lostPoint += 40); 391 for (var col = 0; col < moduleCount; col++) for (var row = 0; row < moduleCount - 6; row++) 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) && (lostPoint += 40); 392 var darkCount = 0; 393 for (var col = 0; col < moduleCount; col++) for (var row = 0; row < moduleCount; row++) qrCode.isDark(row, col) && darkCount++; 394 var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5; 395 return lostPoint += ratio * 10, lostPoint; 396 } 397 }, QRMath = { 398 "glog": function(n) { 399 if (n < 1) throw new Error("glog(" + n + ")"); 400 return QRMath.LOG_TABLE[n]; 401 }, 402 "gexp": function(n) { 403 while (n < 0) n += 255; 404 while (n >= 256) n -= 255; 405 return QRMath.EXP_TABLE[n]; 406 }, 407 "EXP_TABLE": new Array(256), 408 "LOG_TABLE": new Array(256) 409 }; 410 411 for (var i = 0; i < 8; i++) QRMath.EXP_TABLE[i] = 1 << i; 412 413 for (var i = 8; i < 256; i++) QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8]; 414 415 for (var i = 0; i < 255; i++) QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i; 416 417 QRPolynomial.prototype = { 418 "get": function(index) { 419 return this.num[index]; 420 }, 421 "getLength": function() { 422 return this.num.length; 423 }, 424 "multiply": function(e) { 425 var num = new Array(this.getLength() + e.getLength() - 1); 426 for (var i = 0; i < this.getLength(); i++) for (var j = 0; j < e.getLength(); j++) num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j))); 427 return new QRPolynomial(num, 0); 428 }, 429 "mod": function(e) { 430 if (this.getLength() - e.getLength() < 0) return this; 431 var ratio = QRMath.glog(this.get(0)) - QRMath.glog(e.get(0)), num = new Array(this.getLength()); 432 for (var i = 0; i < this.getLength(); i++) num[i] = this.get(i); 433 for (var i = 0; i < e.getLength(); i++) num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio); 434 return (new QRPolynomial(num, 0)).mod(e); 435 } 436 }, 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 ] ], QRRSBlock.getRSBlocks = function(typeNumber, errorCorrectLevel) { 437 var rsBlock = QRRSBlock.getRsBlockTable(typeNumber, errorCorrectLevel); 438 if (rsBlock == undefined) throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + errorCorrectLevel); 439 var length = rsBlock.length / 3, list = []; 440 for (var i = 0; i < length; i++) { 441 var count = rsBlock[i * 3 + 0], totalCount = rsBlock[i * 3 + 1], dataCount = rsBlock[i * 3 + 2]; 442 for (var j = 0; j < count; j++) list.push(new QRRSBlock(totalCount, dataCount)); 443 } 444 return list; 445 }, QRRSBlock.getRsBlockTable = function(typeNumber, errorCorrectLevel) { 446 switch (errorCorrectLevel) { 447 case QRErrorCorrectLevel.L: 448 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0]; 449 case QRErrorCorrectLevel.M: 450 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1]; 451 case QRErrorCorrectLevel.Q: 452 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2]; 453 case QRErrorCorrectLevel.H: 454 return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3]; 455 default: 456 return undefined; 457 } 458 }, QRBitBuffer.prototype = { 459 "get": function(index) { 460 var bufIndex = Math.floor(index / 8); 461 return (this.buffer[bufIndex] >>> 7 - index % 8 & 1) == 1; 462 }, 463 "put": function(num, length) { 464 for (var i = 0; i < length; i++) this.putBit((num >>> length - i - 1 & 1) == 1); 465 }, 466 "getLengthInBits": function() { 467 return this.length; 468 }, 469 "putBit": function(bit) { 470 var bufIndex = Math.floor(this.length / 8); 471 this.buffer.length <= bufIndex && this.buffer.push(0), bit && (this.buffer[bufIndex] |= 128 >>> this.length % 8), this.length++; 472 } 473 }; 474 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]]; 475 476 function _isSupportCanvas() { 477 return typeof CanvasRenderingContext2D != "undefined"; 478 } 479 480 // android 2.x doesn't support Data-URI spec 481 function _getAndroid() { 482 var android = false; 483 var sAgent = navigator.userAgent; 484 485 if (/android/i.test(sAgent)) { // android 486 android = true; 487 aMat = sAgent.toString().match(/android ([0-9].[0-9])/i); 488 489 if (aMat && aMat[1]) { 490 android = parseFloat(aMat[1]); 491 } 492 } 493 494 return android; 495 } 496 497 var svgDrawer = (function() { 498 499 var Drawing = function (el, htOption) { 500 this._el = el; 501 this._htOption = htOption; 502 }; 503 504 Drawing.prototype.draw = function (oQRCode) { 505 var _htOption = this._htOption; 506 var _el = this._el; 507 var nCount = oQRCode.getModuleCount(); 508 var nWidth = Math.floor(_htOption.width / nCount); 509 var nHeight = Math.floor(_htOption.height / nCount); 510 511 this.clear(); 512 513 function makeSVG(tag, attrs) { 514 var el = document.createElementNS('http://www.w3.org/2000/svg', tag); 515 for (var k in attrs) 516 if (attrs.hasOwnProperty(k)) el.setAttribute(k, attrs[k]); 517 return el; 518 } 519 520 var svg = makeSVG("svg" , {'viewBox': '0 0 ' + String(nCount) + " " + String(nCount), 'width': '100%', 'height': '100%', 'fill': _htOption.colorLight}); 521 svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink"); 522 _el.appendChild(svg); 523 524 svg.appendChild(makeSVG("rect", {"fill": _htOption.colorDark, "width": "1", "height": "1", "id": "template"})); 525 526 for (var row = 0; row < nCount; row++) { 527 for (var col = 0; col < nCount; col++) { 528 if (oQRCode.isDark(row, col)) { 529 var child = makeSVG("use", {"x": String(row), "y": String(col)}); 530 child.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#template") 531 svg.appendChild(child); 532 } 533 } 534 } 535 }; 536 Drawing.prototype.clear = function () { 537 while (this._el.hasChildNodes()) 538 this._el.removeChild(this._el.lastChild); 539 }; 540 return Drawing; 541 })(); 542 543 var useSVG = document.documentElement.tagName.toLowerCase() === "svg"; 544 545 // Drawing in DOM by using Table tag 546 var Drawing = useSVG ? svgDrawer : !_isSupportCanvas() ? (function () { 547 var Drawing = function (el, htOption) { 548 this._el = el; 549 this._htOption = htOption; 550 }; 551 552 /** 553 * Draw the QRCode 554 * 555 * @param {QRCode} oQRCode 556 */ 557 Drawing.prototype.draw = function (oQRCode) { 558 var _htOption = this._htOption; 559 var _el = this._el; 560 var nCount = oQRCode.getModuleCount(); 561 var nWidth = Math.floor(_htOption.width / nCount); 562 var nHeight = Math.floor(_htOption.height / nCount); 563 var aHTML = ['<table style="border:0;border-collapse:collapse;">']; 564 565 for (var row = 0; row < nCount; row++) { 566 aHTML.push('<tr>'); 567 568 for (var col = 0; col < nCount; col++) { 569 aHTML.push('<td style="border:0;border-collapse:collapse;padding:0;margin:0;' + nWidth + 'px;height:' + nHeight + 'px;background-color:' + (oQRCode.isDark(row, col) ? _htOption.colorDark : _htOption.colorLight) + ';"></td>'); 570 } 571 572 aHTML.push('</tr>'); 573 } 574 575 aHTML.push('</table>'); 576 _el.innerHTML = aHTML.join(''); 577 578 // Fix the margin values as real size. 579 var elTable = _el.childNodes[0]; 580 var nLeftMarginTable = (_htOption.width - elTable.offsetWidth) / 2; 581 var nTopMarginTable = (_htOption.height - elTable.offsetHeight) / 2; 582 if (nLeftMarginTable > 0 && nTopMarginTable > 0) { 583 elTable.style.margin = nTopMarginTable + "px " + nLeftMarginTable + "px"; 584 } 585 }; 586 587 /** 588 * Clear the QRCode 589 */ 590 Drawing.prototype.clear = function () { 591 this._el.innerHTML = ''; 592 }; 593 594 return Drawing; 595 })() : (function () { // Drawing in Canvas 596 function _onMakeImage() { 597 this._elImage.src = this._elCanvas.toDataURL("image/png"); 598 this._elImage.style.display = "block"; 599 this._elCanvas.style.display = "none"; 600 } 601 602 // Android 2.1 bug workaround 603 // http://code.google.com/p/android/issues/detail?id=5141 604 if (this._android && this._android <= 2.1) { 605 var factor = 1 / window.devicePixelRatio; 606 var drawImage = CanvasRenderingContext2D.prototype.drawImage; 607 CanvasRenderingContext2D.prototype.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) { 608 if (("nodeName" in image) && /img/i.test(image.nodeName)) { 609 for (var i = arguments.length - 1; i >= 1; i--) { 610 arguments[i] = arguments[i] * factor; 611 } 612 } else if (typeof dw == "undefined") { 613 arguments[1] *= factor; 614 arguments[2] *= factor; 615 arguments[3] *= factor; 616 arguments[4] *= factor; 617 } 618 619 drawImage.apply(this, arguments); 620 }; 621 } 622 623 /** 624 * Check whether the user's browser supports Data URI or not 625 * 626 * @private 627 * @param {Function} fSuccess Occurs if it supports Data URI 628 * @param {Function} fFail Occurs if it doesn't support Data URI 629 */ 630 function _safeSetDataURI(fSuccess, fFail) { 631 var self = this; 632 self._fFail = fFail; 633 self._fSuccess = fSuccess; 634 635 // Check it just once 636 if (self._bSupportDataURI === null) { 637 var el = document.createElement("img"); 638 var fOnError = function() { 639 self._bSupportDataURI = false; 640 641 if (self._fFail) { 642 _fFail.call(self); 643 } 644 }; 645 var fOnSuccess = function() { 646 self._bSupportDataURI = true; 647 648 if (self._fSuccess) { 649 self._fSuccess.call(self); 650 } 651 }; 652 653 el.onabort = fOnError; 654 el.onerror = fOnError; 655 el.onload = fOnSuccess; 656 el.src = "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; // the Image contains 1px data. 657 return; 658 } else if (self._bSupportDataURI === true && self._fSuccess) { 659 self._fSuccess.call(self); 660 } else if (self._bSupportDataURI === false && self._fFail) { 661 self._fFail.call(self); 662 } 663 }; 664 665 /** 666 * Drawing QRCode by using canvas 667 * 668 * @constructor 669 * @param {HTMLElement} el 670 * @param {Object} htOption QRCode Options 671 */ 672 var Drawing = function (el, htOption) { 673 this._bIsPainted = false; 674 this._android = _getAndroid(); 675 676 this._htOption = htOption; 677 this._elCanvas = document.createElement("canvas"); 678 this._elCanvas.width = htOption.width; 679 this._elCanvas.height = htOption.height; 680 el.appendChild(this._elCanvas); 681 this._el = el; 682 this._oContext = this._elCanvas.getContext("2d"); 683 this._bIsPainted = false; 684 this._elImage = document.createElement("img"); 685 this._elImage.alt = "Scan me!"; 686 this._elImage.style.display = "none"; 687 this._el.appendChild(this._elImage); 688 this._bSupportDataURI = null; 689 }; 690 691 /** 692 * Draw the QRCode 693 * 694 * @param {QRCode} oQRCode 695 */ 696 Drawing.prototype.draw = function (oQRCode) { 697 var _elImage = this._elImage; 698 var _oContext = this._oContext; 699 var _htOption = this._htOption; 700 701 var nCount = oQRCode.getModuleCount(); 702 var nWidth = _htOption.width / nCount; 703 var nHeight = _htOption.height / nCount; 704 var nRoundedWidth = Math.round(nWidth); 705 var nRoundedHeight = Math.round(nHeight); 706 707 _elImage.style.display = "none"; 708 this.clear(); 709 710 for (var row = 0; row < nCount; row++) { 711 for (var col = 0; col < nCount; col++) { 712 var bIsDark = oQRCode.isDark(row, col); 713 var nLeft = col * nWidth; 714 var nTop = row * nHeight; 715 _oContext.strokeStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight; 716 _oContext.lineWidth = 1; 717 _oContext.fillStyle = bIsDark ? _htOption.colorDark : _htOption.colorLight; 718 _oContext.fillRect(nLeft, nTop, nWidth, nHeight); 719 720 // 안티 앨리어싱 방지 처리 721 _oContext.strokeRect( 722 Math.floor(nLeft) + 0.5, 723 Math.floor(nTop) + 0.5, 724 nRoundedWidth, 725 nRoundedHeight 726 ); 727 728 _oContext.strokeRect( 729 Math.ceil(nLeft) - 0.5, 730 Math.ceil(nTop) - 0.5, 731 nRoundedWidth, 732 nRoundedHeight 733 ); 734 } 735 } 736 737 this._bIsPainted = true; 738 }; 739 740 /** 741 * Make the image from Canvas if the browser supports Data URI. 742 */ 743 Drawing.prototype.makeImage = function () { 744 if (this._bIsPainted) { 745 _safeSetDataURI.call(this, _onMakeImage); 746 } 747 }; 748 749 /** 750 * Return whether the QRCode is painted or not 751 * 752 * @return {Boolean} 753 */ 754 Drawing.prototype.isPainted = function () { 755 return this._bIsPainted; 756 }; 757 758 /** 759 * Clear the QRCode 760 */ 761 Drawing.prototype.clear = function () { 762 this._oContext.clearRect(0, 0, this._elCanvas.width, this._elCanvas.height); 763 this._bIsPainted = false; 764 }; 765 766 /** 767 * @private 768 * @param {Number} nNumber 769 */ 770 Drawing.prototype.round = function (nNumber) { 771 if (!nNumber) { 772 return nNumber; 773 } 774 775 return Math.floor(nNumber * 1000) / 1000; 776 }; 777 778 return Drawing; 779 })(); 780 781 /** 782 * Get the type by string length 783 * 784 * @private 785 * @param {String} sText 786 * @param {Number} nCorrectLevel 787 * @return {Number} type 788 */ 789 function _getTypeNumber(sText, nCorrectLevel) { 790 var nType = 1; 791 var length = _getUTF8Length(sText); 792 793 for (var i = 0, len = QRCodeLimitLength.length; i <= len; i++) { 794 var nLimit = 0; 795 796 switch (nCorrectLevel) { 797 case QRErrorCorrectLevel.L : 798 nLimit = QRCodeLimitLength[i][0]; 799 break; 800 case QRErrorCorrectLevel.M : 801 nLimit = QRCodeLimitLength[i][1]; 802 break; 803 case QRErrorCorrectLevel.Q : 804 nLimit = QRCodeLimitLength[i][2]; 805 break; 806 case QRErrorCorrectLevel.H : 807 nLimit = QRCodeLimitLength[i][3]; 808 break; 809 } 810 811 if (length <= nLimit) { 812 break; 813 } else { 814 nType++; 815 } 816 } 817 818 if (nType > QRCodeLimitLength.length) { 819 throw new Error("Too long data"); 820 } 821 822 return nType; 823 } 824 825 function _getUTF8Length(sText) { 826 var replacedText = encodeURI(sText).toString().replace(/\%[0-9a-fA-F]{2}/g, 'a'); 827 return replacedText.length + (replacedText.length != sText ? 3 : 0); 828 } 829 830 /** 831 * @class QRCode 832 * @constructor 833 * @example 834 * new QRCode(document.getElementById("test"), "http://jindo.dev.naver.com/collie"); 835 * 836 * @example 837 * var oQRCode = new QRCode("test", { 838 * text : "http://naver.com", 839 * width : 128, 840 * height : 128 841 * }); 842 * 843 * oQRCode.clear(); // Clear the QRCode. 844 * oQRCode.makeCode("http://map.naver.com"); // Re-create the QRCode. 845 * 846 * @param {HTMLElement|String} el target element or 'id' attribute of element. 847 * @param {Object|String} vOption 848 * @param {String} vOption.text QRCode link data 849 * @param {Number} [vOption.width=256] 850 * @param {Number} [vOption.height=256] 851 * @param {String} [vOption.colorDark="#000000"] 852 * @param {String} [vOption.colorLight="#ffffff"] 853 * @param {QRCode.CorrectLevel} [vOption.correctLevel=QRCode.CorrectLevel.H] [L|M|Q|H] 854 */ 855 QRCode = function (el, vOption) { 856 this._htOption = { 857 width : 256, 858 height : 256, 859 typeNumber : 4, 860 colorDark : "#000000", 861 colorLight : "#ffffff", 862 correctLevel : QRErrorCorrectLevel.H 863 }; 864 865 if (typeof vOption === 'string') { 866 vOption = { 867 text : vOption 868 }; 869 } 870 871 // Overwrites options 872 if (vOption) { 873 for (var i in vOption) { 874 this._htOption[i] = vOption[i]; 875 } 876 } 877 878 if (typeof el == "string") { 879 el = document.getElementById(el); 880 } 881 882 this._android = _getAndroid(); 883 this._el = el; 884 this._oQRCode = null; 885 this._oDrawing = new Drawing(this._el, this._htOption); 886 887 if (this._htOption.text) { 888 this.makeCode(this._htOption.text); 889 } 890 }; 891 892 /** 893 * Make the QRCode 894 * 895 * @param {String} sText link data 896 */ 897 QRCode.prototype.makeCode = function (sText) { 898 this._oQRCode = new QRCodeModel(_getTypeNumber(sText, this._htOption.correctLevel), this._htOption.correctLevel); 899 this._oQRCode.addData(sText); 900 this._oQRCode.make(); 901 this._el.title = sText; 902 this._oDrawing.draw(this._oQRCode); 903 this.makeImage(); 904 }; 905 906 /** 907 * Make the Image from Canvas element 908 * - It occurs automatically 909 * - Android below 3 doesn't support Data-URI spec. 910 * 911 * @private 912 */ 913 QRCode.prototype.makeImage = function () { 914 if (typeof this._oDrawing.makeImage == "function" && (!this._android || this._android >= 3)) { 915 this._oDrawing.makeImage(); 916 } 917 }; 918 919 /** 920 * Clear the QRCode 921 */ 922 QRCode.prototype.clear = function () { 923 this._oDrawing.clear(); 924 }; 925 926 /** 927 * @name QRCode.CorrectLevel 928 */ 929 QRCode.CorrectLevel = QRErrorCorrectLevel; 930 })();
html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title></title> 5 <meta charset="utf-8"/> 6 <script src="qrcode.js"></script> 7 <style> 8 #qrcode{ 9 /*text-align: center;*/ 10 /*display: table-cell;*/ 11 /* 96px;*/ 12 /*height: 96px;*/ 13 /*vertical-align:middle;*/ 14 /*position: relative;*/ 15 } 16 </style> 17 </head> 18 <body> 19 <div id="qrcode"> 20 </div> 21 22 <input type="text" id="getval"/> <button id="send">点击更换验证码</button> 23 <script> 24 window.onload =function(){ 25 var qrcode = new QRCode(document.getElementById("qrcode"), { 26 width : 96,//设置宽高 27 height : 96 28 }); 29 // qrcode.makeCode("http://www.baidu.com"); 30 document.getElementById("send").onclick =function(){ 31 qrcode.makeCode(document.getElementById("getval").value); 32 } 33 } 34 35 36 </script> 37 </body> 38 </html>
jquery.qrcode
html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style media="screen"> </style> </head> <body> <div id="qrDiv"> </div> <div id='box'> </div> </body> <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.min.js"></script> <script src="./jquery.qrcode.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> $("#qrDiv").qrcode({ 120, //宽度 height:120, //高度 text: "http://www.baidu.com/" //任意内容 }); //从 canvas 提取图片 image function convertCanvasToImage(canvas) { //新Image对象,可以理解为DOM var image = new Image(); // canvas.toDataURL 返回的是一串Base64编码的URL,当然,浏览器自己肯定支持 // 指定格式 PNG image.src = canvas.toDataURL("image/png"); return image; } //获取网页中的canvas对象 var mycanvas1=document.getElementsByTagName('canvas')[0]; //将转换后的img标签插入到html中 var img=convertCanvasToImage(mycanvas1); $('#box').append(img);//imagQrDiv表示你要插入的容器id </script> </html>
jquery.qrcode.min.js
(function(r){r.fn.qrcode=function(h){var s;function u(a){this.mode=s;this.data=a}function o(a,c){this.typeNumber=a;this.errorCorrectLevel=c;this.modules=null;this.moduleCount=0;this.dataCache=null;this.dataList=[]}function q(a,c){if(void 0==a.length)throw Error(a.length+"/"+c);for(var d=0;d<a.length&&0==a[d];)d++;this.num=Array(a.length-d+c);for(var b=0;b<a.length-d;b++)this.num[b]=a[b+d]}function p(a,c){this.totalCount=a;this.dataCount=c}function t(){this.buffer=[];this.length=0}u.prototype={getLength:function(){return this.data.length}, write:function(a){for(var c=0;c<this.data.length;c++)a.put(this.data.charCodeAt(c),8)}};o.prototype={addData:function(a){this.dataList.push(new u(a));this.dataCache=null},isDark:function(a,c){if(0>a||this.moduleCount<=a||0>c||this.moduleCount<=c)throw Error(a+","+c);return this.modules[a][c]},getModuleCount:function(){return this.moduleCount},make:function(){if(1>this.typeNumber){for(var a=1,a=1;40>a;a++){for(var c=p.getRSBlocks(a,this.errorCorrectLevel),d=new t,b=0,e=0;e<c.length;e++)b+=c[e].dataCount; for(e=0;e<this.dataList.length;e++)c=this.dataList[e],d.put(c.mode,4),d.put(c.getLength(),j.getLengthInBits(c.mode,a)),c.write(d);if(d.getLengthInBits()<=8*b)break}this.typeNumber=a}this.makeImpl(!1,this.getBestMaskPattern())},makeImpl:function(a,c){this.moduleCount=4*this.typeNumber+17;this.modules=Array(this.moduleCount);for(var d=0;d<this.moduleCount;d++){this.modules[d]=Array(this.moduleCount);for(var b=0;b<this.moduleCount;b++)this.modules[d][b]=null}this.setupPositionProbePattern(0,0);this.setupPositionProbePattern(this.moduleCount- 7,0);this.setupPositionProbePattern(0,this.moduleCount-7);this.setupPositionAdjustPattern();this.setupTimingPattern();this.setupTypeInfo(a,c);7<=this.typeNumber&&this.setupTypeNumber(a);null==this.dataCache&&(this.dataCache=o.createData(this.typeNumber,this.errorCorrectLevel,this.dataList));this.mapData(this.dataCache,c)},setupPositionProbePattern:function(a,c){for(var d=-1;7>=d;d++)if(!(-1>=a+d||this.moduleCount<=a+d))for(var b=-1;7>=b;b++)-1>=c+b||this.moduleCount<=c+b||(this.modules[a+d][c+b]= 0<=d&&6>=d&&(0==b||6==b)||0<=b&&6>=b&&(0==d||6==d)||2<=d&&4>=d&&2<=b&&4>=b?!0:!1)},getBestMaskPattern:function(){for(var a=0,c=0,d=0;8>d;d++){this.makeImpl(!0,d);var b=j.getLostPoint(this);if(0==d||a>b)a=b,c=d}return c},createMovieClip:function(a,c,d){a=a.createEmptyMovieClip(c,d);this.make();for(c=0;c<this.modules.length;c++)for(var d=1*c,b=0;b<this.modules[c].length;b++){var e=1*b;this.modules[c][b]&&(a.beginFill(0,100),a.moveTo(e,d),a.lineTo(e+1,d),a.lineTo(e+1,d+1),a.lineTo(e,d+1),a.endFill())}return a}, setupTimingPattern:function(){for(var a=8;a<this.moduleCount-8;a++)null==this.modules[a][6]&&(this.modules[a][6]=0==a%2);for(a=8;a<this.moduleCount-8;a++)null==this.modules[6][a]&&(this.modules[6][a]=0==a%2)},setupPositionAdjustPattern:function(){for(var a=j.getPatternPosition(this.typeNumber),c=0;c<a.length;c++)for(var d=0;d<a.length;d++){var b=a[c],e=a[d];if(null==this.modules[b][e])for(var f=-2;2>=f;f++)for(var i=-2;2>=i;i++)this.modules[b+f][e+i]=-2==f||2==f||-2==i||2==i||0==f&&0==i?!0:!1}},setupTypeNumber:function(a){for(var c= j.getBCHTypeNumber(this.typeNumber),d=0;18>d;d++){var b=!a&&1==(c>>d&1);this.modules[Math.floor(d/3)][d%3+this.moduleCount-8-3]=b}for(d=0;18>d;d++)b=!a&&1==(c>>d&1),this.modules[d%3+this.moduleCount-8-3][Math.floor(d/3)]=b},setupTypeInfo:function(a,c){for(var d=j.getBCHTypeInfo(this.errorCorrectLevel<<3|c),b=0;15>b;b++){var e=!a&&1==(d>>b&1);6>b?this.modules[b][8]=e:8>b?this.modules[b+1][8]=e:this.modules[this.moduleCount-15+b][8]=e}for(b=0;15>b;b++)e=!a&&1==(d>>b&1),8>b?this.modules[8][this.moduleCount- b-1]=e:9>b?this.modules[8][15-b-1+1]=e:this.modules[8][15-b-1]=e;this.modules[this.moduleCount-8][8]=!a},mapData:function(a,c){for(var d=-1,b=this.moduleCount-1,e=7,f=0,i=this.moduleCount-1;0<i;i-=2)for(6==i&&i--;;){for(var g=0;2>g;g++)if(null==this.modules[b][i-g]){var n=!1;f<a.length&&(n=1==(a[f]>>>e&1));j.getMask(c,b,i-g)&&(n=!n);this.modules[b][i-g]=n;e--; -1==e&&(f++,e=7)}b+=d;if(0>b||this.moduleCount<=b){b-=d;d=-d;break}}}};o.PAD0=236;o.PAD1=17;o.createData=function(a,c,d){for(var c=p.getRSBlocks(a, c),b=new t,e=0;e<d.length;e++){var f=d[e];b.put(f.mode,4);b.put(f.getLength(),j.getLengthInBits(f.mode,a));f.write(b)}for(e=a=0;e<c.length;e++)a+=c[e].dataCount;if(b.getLengthInBits()>8*a)throw Error("code length overflow. ("+b.getLengthInBits()+">"+8*a+")");for(b.getLengthInBits()+4<=8*a&&b.put(0,4);0!=b.getLengthInBits()%8;)b.putBit(!1);for(;!(b.getLengthInBits()>=8*a);){b.put(o.PAD0,8);if(b.getLengthInBits()>=8*a)break;b.put(o.PAD1,8)}return o.createBytes(b,c)};o.createBytes=function(a,c){for(var d= 0,b=0,e=0,f=Array(c.length),i=Array(c.length),g=0;g<c.length;g++){var n=c[g].dataCount,h=c[g].totalCount-n,b=Math.max(b,n),e=Math.max(e,h);f[g]=Array(n);for(var k=0;k<f[g].length;k++)f[g][k]=255&a.buffer[k+d];d+=n;k=j.getErrorCorrectPolynomial(h);n=(new q(f[g],k.getLength()-1)).mod(k);i[g]=Array(k.getLength()-1);for(k=0;k<i[g].length;k++)h=k+n.getLength()-i[g].length,i[g][k]=0<=h?n.get(h):0}for(k=g=0;k<c.length;k++)g+=c[k].totalCount;d=Array(g);for(k=n=0;k<b;k++)for(g=0;g<c.length;g++)k<f[g].length&& (d[n++]=f[g][k]);for(k=0;k<e;k++)for(g=0;g<c.length;g++)k<i[g].length&&(d[n++]=i[g][k]);return d};s=4;for(var j={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]],G15:1335,G18:7973,G15_MASK:21522,getBCHTypeInfo:function(a){for(var c=a<<10;0<=j.getBCHDigit(c)-j.getBCHDigit(j.G15);)c^=j.G15<<j.getBCHDigit(c)-j.getBCHDigit(j.G15);return(a<<10|c)^j.G15_MASK},getBCHTypeNumber:function(a){for(var c=a<<12;0<=j.getBCHDigit(c)- j.getBCHDigit(j.G18);)c^=j.G18<<j.getBCHDigit(c)-j.getBCHDigit(j.G18);return a<<12|c},getBCHDigit:function(a){for(var c=0;0!=a;)c++,a>>>=1;return c},getPatternPosition:function(a){return j.PATTERN_POSITION_TABLE[a-1]},getMask:function(a,c,d){switch(a){case 0:return 0==(c+d)%2;case 1:return 0==c%2;case 2:return 0==d%3;case 3:return 0==(c+d)%3;case 4:return 0==(Math.floor(c/2)+Math.floor(d/3))%2;case 5:return 0==c*d%2+c*d%3;case 6:return 0==(c*d%2+c*d%3)%2;case 7:return 0==(c*d%3+(c+d)%2)%2;default:throw Error("bad maskPattern:"+ a);}},getErrorCorrectPolynomial:function(a){for(var c=new q([1],0),d=0;d<a;d++)c=c.multiply(new q([1,l.gexp(d)],0));return c},getLengthInBits:function(a,c){if(1<=c&&10>c)switch(a){case 1:return 10;case 2:return 9;case s:return 8;case 8:return 8;default:throw Error("mode:"+a);}else if(27>c)switch(a){case 1:return 12;case 2:return 11;case s:return 16;case 8:return 10;default:throw Error("mode:"+a);}else if(41>c)switch(a){case 1:return 14;case 2:return 13;case s:return 16;case 8:return 12;default:throw Error("mode:"+ a);}else throw Error("type:"+c);},getLostPoint:function(a){for(var c=a.getModuleCount(),d=0,b=0;b<c;b++)for(var e=0;e<c;e++){for(var f=0,i=a.isDark(b,e),g=-1;1>=g;g++)if(!(0>b+g||c<=b+g))for(var h=-1;1>=h;h++)0>e+h||c<=e+h||0==g&&0==h||i==a.isDark(b+g,e+h)&&f++;5<f&&(d+=3+f-5)}for(b=0;b<c-1;b++)for(e=0;e<c-1;e++)if(f=0,a.isDark(b,e)&&f++,a.isDark(b+1,e)&&f++,a.isDark(b,e+1)&&f++,a.isDark(b+1,e+1)&&f++,0==f||4==f)d+=3;for(b=0;b<c;b++)for(e=0;e<c-6;e++)a.isDark(b,e)&&!a.isDark(b,e+1)&&a.isDark(b,e+ 2)&&a.isDark(b,e+3)&&a.isDark(b,e+4)&&!a.isDark(b,e+5)&&a.isDark(b,e+6)&&(d+=40);for(e=0;e<c;e++)for(b=0;b<c-6;b++)a.isDark(b,e)&&!a.isDark(b+1,e)&&a.isDark(b+2,e)&&a.isDark(b+3,e)&&a.isDark(b+4,e)&&!a.isDark(b+5,e)&&a.isDark(b+6,e)&&(d+=40);for(e=f=0;e<c;e++)for(b=0;b<c;b++)a.isDark(b,e)&&f++;a=Math.abs(100*f/c/c-50)/5;return d+10*a}},l={glog:function(a){if(1>a)throw Error("glog("+a+")");return l.LOG_TABLE[a]},gexp:function(a){for(;0>a;)a+=255;for(;256<=a;)a-=255;return l.EXP_TABLE[a]},EXP_TABLE:Array(256), LOG_TABLE:Array(256)},m=0;8>m;m++)l.EXP_TABLE[m]=1<<m;for(m=8;256>m;m++)l.EXP_TABLE[m]=l.EXP_TABLE[m-4]^l.EXP_TABLE[m-5]^l.EXP_TABLE[m-6]^l.EXP_TABLE[m-8];for(m=0;255>m;m++)l.LOG_TABLE[l.EXP_TABLE[m]]=m;q.prototype={get:function(a){return this.num[a]},getLength:function(){return this.num.length},multiply:function(a){for(var c=Array(this.getLength()+a.getLength()-1),d=0;d<this.getLength();d++)for(var b=0;b<a.getLength();b++)c[d+b]^=l.gexp(l.glog(this.get(d))+l.glog(a.get(b)));return new q(c,0)},mod:function(a){if(0> this.getLength()-a.getLength())return this;for(var c=l.glog(this.get(0))-l.glog(a.get(0)),d=Array(this.getLength()),b=0;b<this.getLength();b++)d[b]=this.get(b);for(b=0;b<a.getLength();b++)d[b]^=l.gexp(l.glog(a.get(b))+c);return(new q(d,0)).mod(a)}};p.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]];p.getRSBlocks=function(a,c){var d=p.getRsBlockTable(a,c);if(void 0==d)throw Error("bad rs block @ typeNumber:"+a+"/errorCorrectLevel:"+c);for(var b=d.length/3,e=[],f=0;f<b;f++)for(var h=d[3*f+0],g=d[3*f+1],j=d[3*f+2],l=0;l<h;l++)e.push(new p(g,j));return e};p.getRsBlockTable=function(a,c){switch(c){case 1:return p.RS_BLOCK_TABLE[4*(a-1)+0];case 0:return p.RS_BLOCK_TABLE[4*(a-1)+1];case 3:return p.RS_BLOCK_TABLE[4* (a-1)+2];case 2:return p.RS_BLOCK_TABLE[4*(a-1)+3]}};t.prototype={get:function(a){return 1==(this.buffer[Math.floor(a/8)]>>>7-a%8&1)},put:function(a,c){for(var d=0;d<c;d++)this.putBit(1==(a>>>c-d-1&1))},getLengthInBits:function(){return this.length},putBit:function(a){var c=Math.floor(this.length/8);this.buffer.length<=c&&this.buffer.push(0);a&&(this.buffer[c]|=128>>>this.length%8);this.length++}};"string"===typeof h&&(h={text:h});h=r.extend({},{render:"canvas",256,height:256,typeNumber:-1, correctLevel:2,background:"#ffffff",foreground:"#000000"},h);return this.each(function(){var a;if("canvas"==h.render){a=new o(h.typeNumber,h.correctLevel);a.addData(h.text);a.make();var c=document.createElement("canvas");c.width=h.width;c.height=h.height;for(var d=c.getContext("2d"),b=h.width/a.getModuleCount(),e=h.height/a.getModuleCount(),f=0;f<a.getModuleCount();f++)for(var i=0;i<a.getModuleCount();i++){d.fillStyle=a.isDark(f,i)?h.foreground:h.background;var g=Math.ceil((i+1)*b)-Math.floor(i*b), j=Math.ceil((f+1)*b)-Math.floor(f*b);d.fillRect(Math.round(i*b),Math.round(f*e),g,j)}}else{a=new o(h.typeNumber,h.correctLevel);a.addData(h.text);a.make();c=r("<table></table>").css("width",h.width+"px").css("height",h.height+"px").css("border","0px").css("border-collapse","collapse").css("background-color",h.background);d=h.width/a.getModuleCount();b=h.height/a.getModuleCount();for(e=0;e<a.getModuleCount();e++){f=r("<tr></tr>").css("height",b+"px").appendTo(c);for(i=0;i<a.getModuleCount();i++)r("<td></td>").css("width", d+"px").css("background-color",a.isDark(e,i)?h.foreground:h.background).appendTo(f)}}a=c;jQuery(a).appendTo(this)})}})(jQuery);