QRCode.js:使用 JavaScript 生成二维码

什么是 QRCode.js?

QRCode.js 是一个用于生成二维码的 JavaScript 库。主要是经过获取 DOM 的标签,再经过 HTML5 Canvas 绘制而成,不依赖任何库。javascript

基本用法css

载入 JavaScript 文件html

<script type="text/javascript" src="http://static.runoob.com/assets/qrcode/qrcode.min.js"></script>

 DOM结构java

<div id="qrcode"></div>

JavaScropt调用jquery

//简单形式
new QRCode(document.getElementById("qrcode"), "http://www.runoob.com");  // 设置要生成二维码的连接

// 设置参数方式
var qrcode = new QRCode("test", { text: "http://www.runoob.com", width: 128, height: 128, colorDark : "#000000", colorLight : "#ffffff", correctLevel : QRCode.CorrectLevel.H }); // 使用 API
qrcode.clear(); qrcode.makeCode('new content');
View Code

参数说明

new QRCode(element, option)
名称 默认值 说明
element - 显示二维码的元素或该元素的 ID
option   参数配置

option 参数说明浏览器

名称 默认值 说明
width 256 图像宽度
height 256 图像高度
colorDark "#000000" 前景色
colorLight "#ffffff" 背景色
correctLevel QRCode.CorrectLevel.L 容错级别,可设置为:

QRCode.CorrectLevel.Lide

QRCode.CorrectLevel.Mui

QRCode.CorrectLevel.Qspa

QRCode.CorrectLevel.Hscala

API 接口

名称 说明
makeCode(text) 设置二维码内容
clear() 清除二维码。(仅在不支持 Canvas 的浏览器下有效)

浏览器支持

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

实例代码

HTML 代码

<input id="text" type="text" value="http://www.runoob.com" /><br /> <div id="qrcode"></div>
View Code

CSS 代码

#qrcode { width:160px; height:160px; margin-top:15px;
}
View Code

JavaScript 代码

var qrcode = new QRCode("qrcode"); 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(); } });
View Code

 HTML完整代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<title>Javascript 二维码生成库:QRCode</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<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="http://www.runoob.com" 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>
View Code