DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> 使用jquery組件qrcode生成二維碼及應用指南
使用jquery組件qrcode生成二維碼及應用指南
編輯:JQuery特效代碼     

有一些耗cpu的計算,完全可以在客戶端上計算,比如生成二維碼。

qrcode其實是通過計算,然後使用jquery實現圖形渲染和畫圖。支持canvas和table兩種方式生成我們所需的二維碼。

具體用法
qrcode是jquery組件,需要至少兩個js, 就是 jquery 和 jquery.qrcode。可以到https://github.com/jeromeetienne/jquery-qrcode獲取最新的代碼。

代碼如下:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>

在頁面上,需要顯示二維碼的地方加入一個空元素(此處用div)

代碼如下:
<div id="qrcode"></div>

在需要生成二維碼的時候,調用一下語句直接生成。

代碼如下:
$("#qrcode").qcrode("http://www.cnblogs.com");//需要生成的頁面

詳細參數

參數 默認值 說明
render canvas 渲染方式,支持canvas和table
width 無 寬度
height 無 高度
text 無 需要生成的url
 
如:

代碼如下:
$("#qrcode").qcrode({
    render: "table",
    width: 200,
    height: 200,
    text: "http://www.cnblogs.com"
});

解決url中有中文方法

我們試驗的時候發現不能識別中文內容的二維碼,通過查找多方資料了解到,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;   
}

下載二維碼

用前端畫出來的二維碼,不是一個canvas就是一個table,如何下載呢?我們只需要將canvas的內容畫到image上,下載下來即可。

代碼如下:
function download(canvasElem, filename, imageType) {
    var event, saveLink, imageData, defalutImageType;
    defalutImageType = 'png';//定義默認圖片類型
    imageData = canvasElem.toDataURL(imageType || defalutImageType);//將canvas元素轉化為image data
    saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
    saveLink.href = imageData;
    saveLink.download = filename;
    event = document.createEvent('MouseEvents');
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    saveLink.dispatchEvent(event);
};

在angular中的簡單封裝

在angular中使用,可以封裝成directive。不過要確保已經引入了之前的兩個js文件。

代碼如下:
var appModule = angular.module('app', []);
appModule.directive('qrcode', function() {
    return {
        restrict: "A",
        scope: {
          text    : '=',
          options : '='
        },
        link: function(scope, elem, attrs) {
          var $elem, options;
          $elem = $(elem);
          options = { //獲取元素的寬度和高度
            width: $elem.width(),
            height: $elem.height()
          };
          angular.extend(options, scope.options);
          scope.$watch('text', function(newText) {
            if (newText) {
              options.text = newText;
              $(elem).qrcode(options);//重新生成二維碼
            }
          });
        };
    };
});

下載的方法在angular中可以封裝成一個service使用。

小伙伴們會使用qrcode生成二維碼了吧,確實很好用,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved