前天用js生成二维码,节省服务器资源及带宽
原版jquery.qrcode不能生成logo,本文采用的是修改版
一、显示效果
二、页面
<!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: '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/haveqing/9505689
java生成二维码QRCode Zxing SwetakeQRCode barcode4j
http://happyqing.iteye.com/blog/2292524
参考:
使用jquery-qrcode生成二维码
http://www.cnblogs.com/xcsn/archive/2013/08/14/3258035.html
JS生成带logo的二维码
http://my.oschina.net/xuwang1993/blog/647291
Qrcode生成二维码(logo篇)之jquery.qrcode.min.js
http://blog.csdn.net/gao36951/article/details/48975353