其中已经包括了qrcode.js,style.css,index.html共三个文件

 index.html :

复制代码
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>qrcode.js插件将你的内容转换成二维码格式</title>
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <div class="lanren">
            <h1>请在下面输入框中输入内容</h1>
            <input type="text" placeholder="请在这里输入内容" id="demo">
            <button id="send">点击生成二维码</button>
            <div id="qrcode"></div>
        </div>
        <script src="js/qrcode.js"></script>
        <script>
            window.onload = function() {
                var content = document.getElementById("qrcode");//放二维码的容器
                var qrcode = new QRCode(content, {
                    width: 200,
                    height: 200
                });
                document.getElementById("send").onclick = function() {
                    var value = document.getElementById("demo").value;//文字内容信息
                    qrcode.makeCode(value);//将文字内容信息生成二维码
                    content.style.display = "block";//显示二维码
                }
            }
        </script>
    </body>

</html>
复制代码

style.css :

复制代码
.lanren {
    width: 500px;
    height: auto;
    margin: 0 auto;
    overflow: hidden;
    text-align: center;
}

.lanren #qrcode {
    width: 200px;
    height: 200px;
    position: relative;
    margin: 10px auto;
    display: none;
}

.lanren input {
    width: 200px;
    height: 20px;
    line-height: 20px;
    padding: 4px;
    outline: 0;
}

.lanren button {
    display: block;
    width: 120px;
    height: 45px;
    background: #0cc;
    text-align: center;
    line-height: 45px;
    border-radius: 45px;
    margin: 5px auto;
    border: none;
    cursor: pointer;
    color: #fff;
    outline: 0;
}
复制代码

qrcode.js :

复制代码
/*from tccdn minify at 2014-6-4 14:59:43,file:/cn/c/c/qrcode.js*/
/**
 * @fileoverview
 * - Using the \'QRCode for Javascript library\'
 * - Fixed dataset of \'QRCode for Javascript library\' for support full-spec.
 * - this library has no dependencies.
 * 
 * @author davidshimjs
 * @see <a href="http://www.d-project.com/" target="_blank">http://www.d-project.com/</a>
 * @see <a href="http://jeromeetienne.github.com/jquery-qrcode/" target="_blank">http://jeromeetienne.github.com/jquery-qrcode/</a>
 */
var QRCode;

(function () {
    //---------------------------------------------------------------------
    // QRCode for JavaScript
    //
    // Copyright (c) 2009 Kazuhiko Arase
    //
    // URL: http://www.d-project.com/
    //
    // Licensed under the MIT license:
    //   http://www.opensource.org/licenses/mit-license.php
    //
    // The word "QR Code" is registered trademark of 
    // DENSO WAVE INCORPORATED
    //   http://www.denso-wave.com/qrcode/faqpatent-e.html
    //
    //---------------------------------------------------------------------
    function QR8bitByte(data) {
        this.mode = QRMode.MODE_8BIT_BYTE;
        this.data = data;
        this.parsedData = [];

        // Added to support UTF-8 Characters
        for (var i = 0, l = this.data.length; i < l; i++) {
            var byteArray = [];
            var code = this.data.charCodeAt(i);

            if (code > 0x10000) {
                byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
                byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
                byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
                byteArray[3] = 0x80 | (code & 0x3F);
            } else if (code > 0x800) {
                byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
                byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
                byteArray[2] = 0x80 | (code & 0x3F);
            } else if (code > 0x80) {
                byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
                byteArray[1] = 0x80 | (code & 0x3F);
            } else {
                byteArray[0] = code;
            }

            this.parsedData.push(byteArray);
        }

        this.parsedData = Array.prototype.concat.apply([], this.parsedData);

        if (this.parsedData.length != this.data.length) {
            this.parsedData.unshift(191);
            this.parsedData.unshift(187);
            this.parsedData.unshift(239);
        }
    }

    QR8bitByte.prototype = {
        getLength: function (buffer) {
            return this.parsedData.length;
        },
        write: function (buffer) {
            for (var i = 0, l = this.parsedData.length; i < l; i++) {
                buffer.put(this.parsedData[i], 8);
            }
        }
    };

    function QRCodeModel(typeNumber, errorCorrectLevel) {
        this.typeNumber = typeNumber;
        this.errorCorrectLevel = errorCorrectLevel;
        this.modules = null;
        this.moduleCount = 0;
        this.dataCache = null;
        this.dataList = [];
    }

function QRPolynomial(num, shift) {
    if (num.length == undefined) throw new Error(num.length + "/" + shift);
    var offset = 0;
    while (offset < num.length && num[offset] == 0) offset++;
    this.num = new Array(num.length - offset + shift);
    for (var i = 0; i < num.length - offset; i++) this.num[i] = num[i + offset];
}

function QRRSBlock(totalCount, dataCount) {
    this.totalCount = totalCount, this.dataCount = dataCount;
}

function QRBitBuffer() {
    this.buffer = [], this.length = 0;
}

    QRCodeModel.prototype = {
        "addData": function(data) {
            var newData = new QR8bitByte(data);
            this.dataList.push(newData), this.dataCache = null;
        },
        "isDark": function(row, col) {
            if (row < 0 || this.moduleCount <= row || col < 0 || this.moduleCount <= col) throw new Error(row + "," + col);
            return this.modules[row][col];
        },
        "getModuleCount": function() {
            return this.moduleCount;
        },
        "make": function() {
            this.makeImpl(!1, this.getBestMaskPattern());
        },
        "makeImpl": function(test, maskPattern) {
            this.moduleCount = this.typeNumber * 4 + 17, this.modules = new Array(this.moduleCount);
            for (var row = 0; row < this.moduleCount; row++) {
                this.modules[row] = new Array(this.moduleCount);
                for (var col = 0; col < this.moduleCount; col++) this.modules[row][col] = null;
            }
            this.setupPositionProbePattern(0, 0),
            this.setupPositionProbePattern(this.moduleCount - 7, 0),
            this.setupPositionProbePattern(0, this.moduleCount - 7),
            this.setupPositionAdjustPattern(), this.setupTimingPattern(),
            this.setupTypeInfo(test, maskPattern),
            this.typeNumber >= 7 && this.setupTypeNumber(test),
            this.dataCache == null && (this.dataCache = QRCodeModel.createData(this.typeNumber, this.errorCorrectLevel, this.dataList)), this.mapData(this.dataCache, maskPattern);
        },
        "setupPositionProbePattern": function(row, col) {
            for (var r = -1; r <= 7; r++) {
                if (row + r <= -1 || this.moduleCount <= row + r) continue;
                for (var c = -1; c <= 7; c++) {
                    if (col + c <= -1 || this.moduleCount <= col + c) continue;
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;
                }
            }
        },
        "getBestMaskPattern": function() {
            var minLostPoint = 0, pattern = 0;
            for (var i = 0; i < 8; i++
版权声明:本文为limeiky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/limeiky/p/12558693.html