(轉)js jquery.qrcode生成二維碼 帶logo 支持中文


場景:公司最最近在開發二維碼支付業務,所以需要做一個html5中的二維碼生成和部署!

前天用js生成二維碼,節省服務器資源及帶寬

原版jquery.qrcode不能生成logo,本文采用的是修改版

 

1 顯示效果

2 頁面

<!DOCTYPE HTML>
<html>
<head>
<title>生成帶有logo的二維碼</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--此處需要引入三個JS文件
  一、jquery-1.8.2.js 
  二、excanvas.js
  三、qrcode.js
  順序要注意,不能亂了順序;
-->
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/excanvas.js"></script>
<script src="js/qrcode.js"></script>
<script type="text/javascript">
$(function() {
    $("#bt").bind("click",
        function() {
                $("#qrcode_div").empty();
            var text = $("#text").val();
            $('#qrcode_div').qrcode({
                render: 'canvas',
                text: utf16to8(text),
                height: 200,
                width: 200,
                typeNumber: -1,            //計算模式
                correctLevel: QRErrorCorrectLevel.M,//糾錯等級
                background: "#ffffff",    //背景顏色
                foreground: "#000000",    //前景顏色
                //logo圖片地址
         //src: 'http://pic1.cxtuku.com/00/07/42/b03695caf529.jpg'
src: 'logo.png' }); //console.info("wwww"); } ); }); function utf16to8(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; } </script> </head> <body> <input type="text" id="text" value="甜甜,我愛你!" style="width:200px;"/> <input type="button" value="生成二維碼" id="bt" /> <div id="qrcode_div" style="margin-top:10px;"> </div> </body> </html>

 

糾錯等級:

QRErrorCorrectLevel.L 7%的字碼可被修正

QRErrorCorrectLevel.M 15%的字碼可被修正

QRErrorCorrectLevel.Q 25%的字碼可被修正

QRErrorCorrectLevel.H 30%的字碼可被修正

 

jquery-1.8.2.js可采用壓縮文件jquery-1.8.2.min.js(自行下載)

excanvas.js可采用壓縮文件excanvas.compiled.js(自行下載)

項目資源下載地址:

 http://download.csdn.net/detail/cs408/9651615

友情提醒:

     如果覺得logo圖片有點小,修改qrcode.js的48左右的代碼

     

     具體怎么改傻瓜都知道,^_^(建議不要太大,不然二維碼識別不了)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM