QRCode.js -- 用 Javascript 生产二维码

QRCode.js是一个生成二维码的JS库。QRCode.js支持在DOM中使用跨浏览器Canvas和table标签的。 QRCode.js不依赖其他JS库。


基本用法:

1
2
3
4
< div  id = "qrcode" ></ div >
< script  type = "text/javascript" >
new QRCode(document.getElementById("qrcode"), "http://jindo.dev.naver.com/collie");
</ script >


还可以添加其它选项:

1
2
3
4
5
6
7
8
9
10
11
< div  id = "qrcode" ></ div >
< script  type = "text/javascript" >
var qrcode = new QRCode(document.getElementById("qrcode"), {
     text: "http://jindo.dev.naver.com/collie",
     width: 128,
     height: 128,
     colorDark : "#000000",
     colorLight : "#ffffff",
     correctLevel : QRCode.CorrectLevel.H
});
</ script >


也可以使用一些方法:

1
2
qrcode.clear();  // 清除二维码
qrcode.makeCode( "http://naver.com" ); // 生成一个新的二维码


浏览器兼容性:

IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile 等等



案例演示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
< html >
< head  lang = "en" >
     < meta  charset = "UTF-8" >
     < meta  http-equiv = "Content-Type"  content = "text/html; charset=UTF-8"  />
     < meta  name = "viewport"  content = "width=device-width,initial-scale=1,user-scalable=no"  />
     < title ></ title >
</ head >
< body >
 
< input  id = "text"  type = "text"  value = "http://dapengtalk.blog.51cto.com"  style = "width:80%"  />< br  />
< div  id = "qrcode"  style = "width:100px; height:100px; margin-top:15px;" ></ div >
 
< script  src = "js/jquery-1.8.3.min.js" ></ script >
< script  src = "js/qrcode.js" ></ script >
< script >
 
     var qrcode = new QRCode(document.getElementById("qrcode"), {
         width : 100,
         height : 100
     });
     function makeCode () {
         var elText = document.getElementById("text");
 
         if (!elText.value) {
             alert("Input a text");
             elText.focus();
             return;
         }
 
         qrcode.makeCode(elText.value);
     }
     makeCode();
     $("#text").
     on("blur", function () {
         makeCode();
     }).
     on("keydown", function (e) {
         if (e.keyCode == 13) {
             makeCode();
         }
     });
 
</ script >
</ body >
</ html >


页面截图:

wKiom1hFDVqDfb1qAAAzBXxWuao851.png


扫描二维码:

wKioL1hFDyeR1hrIAAhJCwfMxKA149.png



Github 地址:https://github.com/davidshimjs/qrcodejs


本文转自   frwupeng517   51CTO博客,原文链接:http://blog.51cto.com/dapengtalk/1879568