H5扫描识别二维码
H5扫描识别⼆维码
索尼维修部参考⽂章:
引⼊的⽂件是qrcode的js插件.github上可以直接下载
HTML:
<div class="wrapper wrapper-full-page">
<div class="full-page">
qq空间留言图片
<div class="content">
<div class="container">
<div class="card">
<canvas id="qr-canvas" width="100%"></canvas>
<div id="outdiv" >
<video id="vedio"></video>
</div>
哈尔滨旅游景区</div>
<div class="card">
<form action="{:url('Payment/idealtokenqr')}" method="post" id="pay">
<input type="hidden" name="ideal_token" id="ideal-token" value=""/>
</form>
<button type="button" onclick="javascript:history.back(-1);" class="btn btn-fill" >返回</button>            </div>
</div>
</div>
</div>
</div>
JS:
<script type="text/javascript" src="__JS__/qrcode/grid.js"></script>
<script type="text/javascript" src="__JS__/qrcode/version.js"></script>
<script type="text/javascript" src="__JS__/qrcode/detector.js"></script>
<script type="text/javascript" src="__JS__/qrcode/formatinf.js"></script>
<script type="text/javascript" src="__JS__/qrcode/errorlevel.js"></script>
<script type="text/javascript" src="__JS__/qrcode/bitmat.js"></script>
<script type="text/javascript" src="__JS__/qrcode/datablock.js"></script>
<script type="text/javascript" src="__JS__/qrcode/bmparser.js"></script>
<script type="text/javascript" src="__JS__/qrcode/datamask.js"></script>
<script type="text/javascript" src="__JS__/qrcode/rsdecoder.js"></script>
<script type="text/javascript" src="__JS__/qrcode/gf256poly.js"></script>
<script type="text/javascript" src="__JS__/qrcode/gf256.js"></script>
<script type="text/javascript" src="__JS__/qrcode/decoder.js"></script>
<script type="text/javascript" src="__JS__/qrcode/qrcode.js"></script>土豆炖茄子
<script type="text/javascript" src="__JS__/qrcode/findpat.js"></script>
<script type="text/javascript" src="__JS__/qrcode/alignpat.js"></script>
<script type="text/javascript" src="__JS__/qrcode/databr.js"></script>
<script type="text/javascript">
var video = document.querySelector('video');
var canvas = ElementById("qr-canvas");
var width = document.body.clientWidth - 30;
var height = width;
canvas.style.width =  width+ "px";
canvas.style.height = height + "px";
canvas.width = width;
canvas.height = height;
var ctx = Context("2d");
var ctx = Context("2d");
ctx.clearRect(0, 0, width, height);
//设置⼆维码回调函数
qrcode.callback = read;
var getImgTiming = null;
// ⽼的浏览器可能根本没有实现 mediaDevices,所以我们可以先设置⼀个空的对象
if (diaDevices === undefined) {
}
// ⼀些浏览器部分⽀持 mediaDevices。我们不能直接给对象设置 getUserMedia
// 因为这样可能会覆盖已有的属性。这⾥我们只会在没有getUserMedia属性的时候添加它。
if (UserMedia === undefined) {
// ⾸先,如果有getUserMedia的话,就获得它
var getUserMedia = navigator.webkitGetUserMedia || GetUserMedia;
/
/ ⼀些浏览器根本没实现它 - 那么就返回⼀个error到promise的reject来保持⼀个统⼀的接⼝
if (!getUserMedia) {
ject(new Error('getUserMedia is not implemented in this browser'));
}
// 否则,为⽼的UserMedia⽅法包裹⼀个Promise
return new Promise(function(resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
}
}
// 旧的浏览器可能没有srcObject
if ("srcObject" in video) {
video.srcObject = stream;
} else {
// 防⽌在新的浏览器⾥使⽤它,应为它已经不再⽀持了
video.src = ateObjectURL(stream);
}
video.play();
drawVideoAtCanvas();
};
})
.catch(function(err) {
alert(err.name + ": " + ssage);
});
// 将视频帧绘制到Canvas对象上,Canvas每60ms切换帧,形成⾁眼视频效果
function drawVideoAtCanvas() {
window.setInterval(function () {
ctx.drawImage(video, 0, 0);
}, 60);
// 定时进⾏图⽚转换成⼆维码
getImgTiming = window.setInterval(function () {
getQrCode();
}, 1000);
}
// 转化图⽚,并解析
function getQrCode() {
var dataURL = DataURL("image/png");
var re = getBlobBydataURI(dataURL,'image/png');
var re = getBlobBydataURI(dataURL,'image/png');
qrcode.decode(getObjectURL(re));
}
function read(res){
if(res == 'error decoding QR Code'){
}else{
clearInterval(getImgTiming);
alert(res);
return false;
$("#ideal-token").val(res);
$("#pay").submit();
}
}
function getObjectURL(file){
var url = null ;
if (ateObjectURL!=undefined) { // basic
url = ateObjectURL(file) ;
泰康人寿万能险} else if (window.URL!=undefined) { // mozilla(firefox)
苏教版二年级下册数学期末试卷
url = ateObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome              url = ateObjectURL(file) ;
}
return url ;
}
function getBlobBydataURI(dataURI,type) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type:type });
}
</script>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。