一.簡介
1.說明
qrcode其實是通過使用jQuery實現圖形渲染,畫圖,支持canvas(HTML5)和table兩種方式,您可以到https://github.com/jeromeetienne/jquery-qrcode獲取最新的代碼。
2.語法格式 syntax
$(selector).qrcode(options);
3.參數說明 options
1 { 2 // render method: 'canvas', 'image' or 'div' 3 render: 'canvas', 4 5 // version range somewhere in 1 .. 40 6 minVersion: 1, 7 maxVersion: 40, 8 9 // error correction level: 'L', 'M', 'Q' or 'H' 10 ecLevel: 'L', 11 12 // offset in pixel if drawn onto existing canvas 13 left: 0, 14 top: 0, 15 16 // size in pixel 17 size: 200, 18 19 // code color or image element 20 fill: '#000', 21 22 // background color or image element, null for transparent background 23 background: null, 24 25 // content 26 text: 'no text', 27 28 // corner radius relative to module width: 0.0 .. 0.5 29 radius: 0, 30 31 // quiet zone in modules 32 quiet: 0, 33 34 // modes 35 // 0: normal 36 // 1: label strip 37 // 2: label box 38 // 3: image strip 39 // 4: image box 40 mode: 0, 41 42 mSize: 0.1, 43 mPosX: 0.5, 44 mPosY: 0.5, 45 46 label: 'no label', 47 fontname: 'sans', 48 fontcolor: '#000', 49 50 image: null 51 }
二.使用方法
1.靜態添加
①.首先在頁面中加入jquery庫文件和qrcode插件:
1 <script type="text/javascript" src="jquery.js"></script> 2 <script type="text/javascript" src="jquery.qrcode.min.js"></script>
②.在頁面中需要顯示二維碼的地方加入以下代碼:
1 <div id="code"></div>
③.調用qrcode插件:
qrcode支持canvas和table兩種方式進行圖片渲染,默認使用canvas方式,效率最高,當然要瀏覽器支持html5。直接調用如下:
1 $('#code').qrcode("http://www.baidu.com"); //任意字符串
您也可以通過以下方式調用:
1 $("#code").qrcode({ 2 render: "table", //table方式 3 width: 200, //寬度 4 height:200, //高度 5 text: "www.baidu.com" //任意內容 6 });
2.動態添加
①.首先在頁面中加入jquery庫文件和qrcode插件(同上):
1 <script type="text/javascript" src="jquery.js"></script> 2 <script type="text/javascript" src="jquery.qrcode.min.js"></script>
②.在頁面中需要顯示二維碼的地方加入以下代碼(同上):
1 <div id="code"></div>
③.調用qrcode插件:
1 var QRcode = $('<div>'); 2 QRcode.attr('id' ,"androidQR"); //為<div>添加屬性id 3 QRcode.css('float' ,"right"); //這里演示如何進行css操作 4 QRcode.qrcode({ 5 width: 110, 6 height:110, 7 text: 'http://www.baidu.com' 8 }); 9 $('#code').append(QRcode); //將二維碼元素添append到頁面上id為‘code’的元素上
三.識別中文
jquery-qrcode是采用charCodeAt()方式進行編碼轉換的。而這個方法默認會獲取它的Unicode編碼,如果有中文內容,在生成二維碼前就要把字符串轉換成UTF-8,然后再生成二維碼。您可以通過以下函數來轉換中文字符串:
1 function toUtf8(str) { 2 var out, i, len, c; 3 out = ""; 4 len = str.length; 5 for(i = 0; i < len; i++) { 6 c = str.charCodeAt(i); 7 if ((c >= 0x0001) && (c <= 0x007F)) { 8 out += str.charAt(i); 9 } else if (c > 0x07FF) { 10 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); 11 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); 12 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 13 } else { 14 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); 15 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 16 } 17 } 18 return out; 19 }
示例代碼如下:
1 var str = toUtf8("你真可愛!"); 2 $('#code').qrcode(str);
參考: http://www.helloweba.com/view-blog-226.html
https://larsjung.de/jquery-qrcode/
四.其他生成二維碼的方法
除了jquery,還有很多可以生成二維碼的方法,例如:
1.利用Google API生成二維碼
2.使用PHP二維碼生成類庫PHP QR Code生成二維碼
3.使用原生JavaScript生成二維碼
等方法,本文不作說明。