使用jquery.qrcode產生二維碼

來源:互聯網
上載者:User

標籤:get   獲得   func   轉換   編碼轉換   方便   實現   釣魚   ble   

二維碼應用已經滲透到我們的生活工作當中,您只需要用手機對著二維碼“掃一掃”即可獲得所對應的資訊,方便我們瞭解商家、購物、觀影等等。本文將介紹一款基於jquery的二維碼產生外掛程式qrcode,在頁面中調用該外掛程式就能產生對應的二維碼。

qrcode其實是通過使用jQuery實現圖形渲染,畫圖,支援canvas(HTML5)和table兩種方式,您可以到https://github.com/jeromeetienne/jquery-qrcode擷取最新的代碼。

如何使用

1、首先在頁面中加入jquery庫檔案和qrcode外掛程式。

<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js"></script> 
 

2、在頁面中需要顯示二維碼的地方加入以下代碼:

<div id="code"></div> 
 

3、調用qrcode外掛程式。

qrcode支援canvas和table兩種方式進行圖片渲染,預設使用canvas方式,效率最高,當然要瀏覽器支援html5。直接調用如下:

$(‘#code‘).qrcode("http://www.helloweba.com"); //任一字元串 
 

您也可以通過以下方式調用:

$("#code").qrcode({     render: "table", //table方式     width: 200, //寬度     height:200, //高度     text: "www.helloweba.com" //任意內容 }); 
 

這樣就可以在頁面中直接產生一個二維碼,你可以用手機“掃一掃”功能讀取二維碼資訊。

識別中文

我們實驗的時候發現不能識別中文內容的二維碼,通過尋找多方資料瞭解到,jquery-qrcode是採用charCodeAt()方式進行編碼轉換的。而這個方法預設會擷取它的Unicode編碼,如果有中文內容,在產生二維碼前就要把字串轉換成UTF-8,然後再產生二維碼。您可以通過以下函數來轉換中文字串:

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;    } 

 

 

以下樣本:

var str = toUtf8("釣魚島是中國的!"); $(‘#code‘).qrcode(str); 
 
聲明:本文為原創文章,helloweba.com和作者擁有著作權,如需轉載,請註明來源於helloweba.com並保留原文連結:http://www.helloweba.com/view-blog-226.html 

使用jquery.qrcode產生二維碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.