使用js向二維碼新增文字
阿新 • • 發佈:2018-12-26
原理:先通過canvas生成二維碼,然後在二維碼上加上文字,涉及到canvas操作。
文字框居中演算法:
程式碼如下
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>批量生成二維碼並伺服器</title>
<link rel="stylesheet" type="text/css" href="../css/main.css" />
<style type="text/css">
.demo{width :100%; margin:40px auto 0 auto; min-height:250px;}
.demo p{line-height:30px}
#code{margin-top:10px}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>
<script type="text/javascript" src="canvas2image.js" ></script>
<script type="text/javascript">
var arr = [];//作為全域性變數,可以通過遍歷陣列來實現批量生成二維碼功能
arr.push(['https://www.baidu.com','百度'])
arr.push(['https://http://www.sina.com.cn/','新浪'])
$(function(){
console.log(arr.length)
for(var i=0;i<arr.length;i++){
var urlstr = arr[i][0];
console.log(urlstr)
$("#code" ).qrcode({
render: "canvas",
width:1000,
height:1000,
text: urlstr,
foreground: "#FE007F" //前景顏色
});}
})
</script>
<style>
#code canvas{
margin:8px;
}
</style>
</head>
<body>
<div id="main">
<div class="demo">
<div id="code"></div>
<button id="btn" onclick="down()">download</button>
<button id="btn" onclick="addText(1000,120,20)">新增文字</button>
</div>
</div>
<script>
/**
* [addText 向canvas新增文字]
* @param {[type]} canvassize [canvas寬高]
* @param {[type]} fontsize [文字大小]
* @param {[type]} padding [周圍留空隙]
*/
function addText(canvassize,fontsize,padding,divBackground="#fff",fontColor="#a50054"){
var canvases = document.getElementsByTagName('canvas');
var fontsizeH = fontsize+padding*3; //文字高度
var rectW = 0; //長方形背景寬度
var width = 0;
var centerx = canvassize/2 - rectW/2; //居中的x
var centery = canvassize/2 - fontsizeH/2; //居中的y
for(var i=0;i<canvases.length;i++){
var ctx = canvases[i].getContext("2d");
width = ctx.measureText(arr[i][1]).width; //文字長度,不知道為什麼這裡的width是中文字數的10倍,故需要乘上fontsize/10獲取到真實的文字寬度
rectW = width*fontsize/10+padding*2; //長方形背景寬度,應左右留padding
centerx = canvassize/2 - rectW/2; //長方形背景居中的x
centery = canvassize/2 - fontsizeH/2 //長方形背景居中的y
ctx.fillStyle=divBackground; //長方形背景顏色
ctx.fillRect (centerx,centery,rectW, fontsizeH);
ctx.font = fontsize+"px Calibri"; //設定文字大小
ctx.fillStyle = fontColor;
ctx.fillText(arr[i][1], centerx+padding, centery+fontsize+padding/2)
//經測試,若引數y只填centery,則文字顯示在長方形背景的上邊,所以需要加上fontsize長方形背景才能包裹文字
}
}
/**
* [down ajax將二維碼上傳到伺服器]
* @return {[type]} [description]
*/
function down(){
$.ajaxSetup({ async : false });
var canvases = document.getElementsByTagName("canvas");
for(var i =0;i<arr.length;i++){
var base64= canvases[i].toDataURL("image/png");
$.post('upload.php',{"img":base64,"filename":encodeURI(arr[i][1])},function(data){if(data){console.log(1)}else{console.log(2)}})
}
}
</script>
</body>
</html>
upload.php
<?php
header("Content-Type: text/html;charset=utf-8");
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$file=urldecode($_POST['filename']);
$file=iconv("UTF-8","gb2312",$file);//解決儲存中文檔名亂碼的問題
//var_dump($img)
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . $file . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
git:https://github.com/kristen154/someutils/tree/master/QRCODEimport