不少人很想知道怎麼掃一掃二維碼就能打開網站,就能添加聯繫人,就能連接wifi,今天作個demo(續集)

有些功能部分手機不能使用,網站,通信錄,wifi基本上每一個手機均可以使用。(瀏覽器自帶的掃描就夠了,QQ掃碼和微信掃碼部分手機不能直接鏈接wifijavascript

在看以前你能夠掃一掃下面幾個二維碼先看看效果:html

上篇網站介紹了一下經常使用格式(http://www.cnblogs.com/dunitian/p/4998714.html),其實掃二維碼的本質就是解析出一段字符串,爲何有一些神奇的功能呢?那是字符串的格式知足一些系統內置的協議或者格式,因此係統就幫你幹了相似於發短信,打電話,添加聯繫人,鏈接wifi之類的事情。能夠想像,你開了個店,店門口有個免費wifi的二維碼,而後本身名片也是一個二維碼~~~~是否是有點高大上的感受?java

其實代碼並無什麼技術含量,既然有人要求,那就寫一下吧,此次就不侷限平臺了,寫了個通用的demojquery

核心類庫:jquery.qrcode.min.js瀏覽器

核心代碼:微信

//中文字符處理
        function toUtf8(str) {
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    out += str.charAt(i);
                } else if (c > 0x07FF) {
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else {
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        }

 

//生成二維碼
        function outputQRCod(txt, width, height) {
            //先清空
            $("#code").empty();
            //中文格式轉換
            var str = toUtf8(txt);
            //生成二維碼
            $("#code").qrcode({
                render: "table",
                width: width,
                height: height,
                text: str
            });
        }

完整代碼:網站

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>生成二維碼</title>
    <script src="JavaScript/jquery-1.8.3.min.js"></script>
    <script src="JavaScript/jquery.qrcode.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //沒有中文就能夠這麼簡單
            $('#code').qrcode("http://dnt.dkill.net");

            //普通轉換
            $("#txt_btn").click(function () {
                outputQRCod($("#inputTxt").val(), 200, 200);
            });

            //URL演示
            $("#url_btn").click(function () {
                var urlTxt = $("#inputUrl").val();
                if (urlTxt.indexOf("http://") < 0) {
                    urlTxt = 'http://' + urlTxt;
                }
                outputQRCod(urlTxt, 400, 400);
            });

            //聯繫人添加演示
            $("#people_btn").click(function () {
                var txt = "BIZCARD:N:" + $('#inputName').val() + ";T:" + $('#inputPost').val() + ";C:" + $('#inputCompany').val() + ";A:" + $('#inputAddress').val() + ";B:" + $('#inputMobile').val() + ";E:" + $('#inputEmail').val() + ";;";
                outputQRCod(txt, 400, 400);
            });

            //WiFi鏈接演示
            $("#wifi_btn").click(function () {
                var txt = "WIFI:T:" + $('#WiFiType').val() + ";S:" + $('#inputWiFiName').val() + ";P:" + $('#inputWiFiPass').val() + ";;";
                console.log(txt);
                outputQRCod(txt, 400, 400);
            });
        });

        //中文字符處理
        function toUtf8(str) {
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    out += str.charAt(i);
                } else if (c > 0x07FF) {
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else {
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        }

        //生成二維碼
        function outputQRCod(txt, width, height) {
            //先清空
            $("#code").empty();
            //中文格式轉換
            var str = toUtf8(txt);
            //生成二維碼
            $("#code").qrcode({
                render: "table",
                width: width,
                height: height,
                text: str
            });
        }

    </script>
</head>

<body>
    <div id="main">
        <div class="demo">
            <p>請輸入內容而後點擊按鈕生成二維碼:</p>
            <div id="code"></div>
            <h2>演示1:</h2>
            <p>
                普通文本:<input type="text" class="input" id="inputTxt" value="">
                <input type="button" id="txt_btn" value="生成二維碼">
            </p>
            <h2>演示2:</h2>
            <p>
                URL 演示:<input type="text" class="input" id="inputUrl" value="">
                <input type="button" id="url_btn" value="生成二維碼">
            </p>
            <h2>演示3:</h2>
            <p>加聯繫人:(選填)<input type="button" id="people_btn" value="生成二維碼"></p>
            <p>
                姓名:<input type="text" class="input" id="inputName" value=""><br />
                <br />
                職位:<input type="text" class="input" id="inputPost" value=""><br />
                <br />
                公司:<input type="text" class="input" id="inputCompany" value=""><br />
                <br />
                地址:<input type="text" class="input" id="inputAddress" value=""><br />
                <br />
                手機:<input type="text" class="input" id="inputMobile" value=""><br />
                <br />
                郵箱:<input type="text" class="input" id="inputEmail" value=""><br />
                <br />
            </p>
            <h2>演示4:(如今的wifi通常都是WPA的,WEP的基本上10分鐘內就能破解了)</h2>
            <p>
                WiFi名稱:<input type="text" class="input" id="inputWiFiName" value=""><br />
                <br />
                WiFi密碼:<input type="text" class="input" id="inputWiFiPass" value=""><br />
                <br />
                WiFi類型:<select id="WiFiType"><option value="WPA">WPA/WPA2</option>
                    <option value="WEP">WEP</option>
                    <option value="nopass">無加密</option>
                </select>
                <input type="button" id="wifi_btn" value="生成二維碼">
            </p>
        </div>
    </div>
</body>
</html>

大家要什麼效果就安裝格式本身編輯一下就能夠了,先閃了~~有機會再說一說二維碼的美化加密

demo下載:http://pan.baidu.com/s/1pJGhV0furl

相關文章
相關標籤/搜索