• 纯js生成QRCode


    纯js,不依赖jquery,非常好用,废话不多说,直接上代码!

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

    另外,推荐使用 JsBarcode 生成条形码,也是纯js,不依赖jquery,非常方便!

  • 相关阅读:
    vue路由跳转的方式(一)
    ElementUi树形目录
    Element UI问题总结
    angular入门
    IntelliJ IDEA 指定Java编译版本
    Python 笔记 v1
    Typora极简教程
    Gitbook在Windows上安装
    IntelliJ IDEA中查看UML类图
    服务器最大连接数问题
  • 原文地址:https://www.cnblogs.com/rangeon/p/7483796.html
Copyright © 2020-2023  润新知