QRCode.js:使用javaScript生成二维码
[ 2017/11/06, JavaScript , 2879阅, 0评 ]
QRCode.js是一个用于生成二维码的JavaScript库。主要是通过获取DOM的标签,再通过HTML5 Canvas绘制而成,不依赖任何库。

1.基本用法

<div id="qrcode"></div>
<script type="text/javascript">
new QRCode(document.getElementById("qrcode"), "https://www.eqifei.net");  // 设置要生成二维码的链接
</script>

或者使用一些可选参数设置:

var qrcode = new QRCode("test", {
    text: "https://www.eqifei.net",
    width: 128,
    height: 128,
    colorDark : "#000000",
    colorLight : "#ffffff",
    correctLevel : QRCode.CorrectLevel.H
});

同样我们可以使用以下方法:

qrcode.clear(); // 清除代码
qrcode.makeCode("https://www.eqifei.net"); // 生成另外一个二维码

2.浏览器支持

支持该库的浏览器有:IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile, 等。

3.实例代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Javascript二维码生成库:QRCode</title>
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://static.runoob.com/assets/qrcode/qrcode.min.js"></script>
</head>

<body>
<input id="text" type="text" value="https://www.eqifei.net" style="width:80%"/>
<br/>
<div id="qrcode" style="width:100px;height:100px;margin-top:15px;"></div>
<script type="text/javascript">
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>

4.资源下载

Qrcode 库及实例下载:qrcodejs-04f46c6.zip

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

有朋自远方来...评论一下呗O(∩_∩)O