jquery.qrcode.js 是居於jquery類庫的繪制二維碼的插件,用它來實現二維碼圖形渲染支持canvas和table兩種繪圖方式。
(jquery.qrcode.js 設置顯示方式為table時在webkit核心瀏覽器如chrome下會變形)
以下是測試代碼(增加了顏色控制,可以設置4個區塊的顏色值,需要指定render為table。),效果:
jquery.qrcode生成彩色二維碼" src="http://www.jbxue.com/d/file/2014/08/20140808213149602.jpg" height="149" width="217">
完整代碼:
復制代碼 代碼示例:
<html>
<head>
<title>JS生成二維碼-www.jbxue.com</title>
<script type="text/ javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/ javascript" src="jquery.qrcode.min.js"></script>
<style>
#output{
margin-left:300px;
margin-top:100px;
}
</style>
</head>
<body>
<div id="output"></div>
<script>
window.onload = function () {
var trs = $('#output').qrcode({
width: 100,
height: 100,
render: "canvas", //設置渲染方式 table canvas
text: utf16to8("javascript生成二維碼"),
background: "#ffffff", //背景顏色
foreground: "red" //前景顏色
}).find('tr'), trLen = Math.floor(trs.size() / 2), tdLen = Math.floor(trs.eq(0).find('td').size() / 2), tds, bgColor;
var colors = [['#ff0000', '#0100e2'], ['#00ed01', '#9f4d95']];
trs.each(function (j) {
tds = $(this).find('td');
tds.each(function (i) {
bgColor = this.style.backgroundColor;
if (bgColor == 'red') {
this.style.backgroundColor = colors[j < trLen ? 0 : 1][i < tdLen ? 0 : 1];
}
});
});
}
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));
} // www.jbxue.com
}
return out;
}
</script>
</body>
</html>
<head>
<title>JS生成二維碼-www.jbxue.com</title>
<script type="text/ javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/ javascript" src="jquery.qrcode.min.js"></script>
<style>
#output{
margin-left:300px;
margin-top:100px;
}
</style>
</head>
<body>
<div id="output"></div>
<script>
window.onload = function () {
var trs = $('#output').qrcode({
width: 100,
height: 100,
render: "canvas", //設置渲染方式 table canvas
text: utf16to8("javascript生成二維碼"),
background: "#ffffff", //背景顏色
foreground: "red" //前景顏色
}).find('tr'), trLen = Math.floor(trs.size() / 2), tdLen = Math.floor(trs.eq(0).find('td').size() / 2), tds, bgColor;
var colors = [['#ff0000', '#0100e2'], ['#00ed01', '#9f4d95']];
trs.each(function (j) {
tds = $(this).find('td');
tds.each(function (i) {
bgColor = this.style.backgroundColor;
if (bgColor == 'red') {
this.style.backgroundColor = colors[j < trLen ? 0 : 1][i < tdLen ? 0 : 1];
}
});
});
}
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));
} // www.jbxue.com
}
return out;
}
</script>
</body>
</html>
代碼說明:
jquery-qrcode這個庫是采用 charCodeAt這個方式進行編碼轉換的,而這個方法默認會獲取它的 Unicode 編碼,一般的解碼器都是采用UTF-8, ISO-8859-1等方式,英文是沒有問題,如果是中文,一般情況下Unicode是UTF-16實現,長度2位,而UTF-8編碼是3位,這樣二維碼的編解碼就不匹配了。
解決方法:
在二維碼編碼前把字符串轉換成UTF-8,具體代碼如上utf16to8函數所示。
轉載 http://www.jbxue.com/article/24133.html 收藏一下