2.能够在原生浏览器和微信客户端中扫描二维码并且解析
2.优点:
web端或者是 h5端可以直接完成扫码的工作;
3.缺点:
图片不清晰很容易解析失败(拍照扫描图片需要镜头离二维码的距离很近),相对于 native 呼起的摄像头解析会有1-2秒的延时。
说明:
此插件需要配合zepto.js 或者 jQuery.js使用
使用方法:
1.在需要使用的页面按照下面顺序引入lib目录下的 js 文件
复制代码
代码如下:
<script src="lib/zepto.js"></script>
<script src="lib/qrcode.lib.min.js"></script>
<script src="lib/qrcode.js"></script>
恰好这几天在尝试,没有兼容所有浏览器。只在手机上试过firefox, 自带浏览器试过不行。以下是代码:---------------------------------------------------------Html----------------------------------------------------------------
<form id="picForm" action="@Url.Action("scan")" method="post">
<input type="hidden" value="" id="code" name="code" />
<input type="hidden" value="" id="status" name="status" />
<section style="position: relative">
<video width="100%" id="video" autoplay="autoplay"></video>
<div id="result" style="position: absolutetop: 0left: 0width:100%height:100%text-align: centerborder:1px solid yellow" onclick="scanCode()">
<section class="smtwo" style="text-align:center">
<h3 ><a class="back" href="${ctx }/index/index.htm">
<img src="images/smtwo_1.png" style="border: none" alt=""></a>请将扫描框对准设备或包装上的二维码 </h3>
</section>
<div id="getCode" style="margin:autowidth:200pxheight:200pxborder:1px solid red"></div>
<canvas width="200" height="200" id="canvas"></canvas>
</div>
</section>
</form
--------------------------------------------------------------Js--------------------------------------------------------------------
<script>
var video, canvas, videoObj
window.addEventListener('DOMContentLoaded', function () {
'use strict'
//调取摄像头
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL
if (navigator.getUserMedia) {
videoObj = { video: true }
video = document.getElementById("video")// $("#video").get(0)
canvas = document.getElementById("canvas")// $("#canvas").get(0)
navigator.getUserMedia({
video: true
}, gotStream, noStream)
} else {
alert('Native web camera streaming (getUserMedia) not supported in this browser.')
}
//调取摄像头成功的回调函数
function gotStream(stream) {
if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream
} else {
video.src = (window.URL &&window.URL.createObjectURL(stream)) || stream
}
video.play()
//启动摄像头成功之后开始获取二维码
scanCode()
}
//调取摄像头失败的回调函数
function noStream(error) {
alert('An error occurred: [CODE ' + error.code + ']')
}
}, false)
//抓取video画面放入canvas
function photograph() {
var context = canvas.getContext("2d")
// 截取取景框范围(这里用canvas做取景框)
//var x = canvas.clientLeft
//var y = canvas.clientTop
//context.drawImage(video,x, y, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height)
var box = getBox()
context.drawImage(video, box.X, box.Y, box.W, box.H, 0, 0, box.W , box.H)
//imageConvertToGray(context)
var imgData = canvas.toDataURL("image/png")
$("#code").val(imgData)
}
function getBox() {
var box = $("#getCode")
var position = { X: $(box).offset().top, Y: $(box).offset().left, W: $(box).width(), H: $(box).height() }
return position
}
//将图片处理成黑白的(二维码扫描需要处理黑白色图片,如果仅用于拍照这一步就省略了)
function imageConvertToGray(ctx) {
var length = canvas.width * canvas.height
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
for (var i = 0i <length * 4i += 4) {
var myRed = imageData.data[i]
var myGreen = imageData.data[i + 1]
var myBlue = imageData.data[i + 2]
myGray = parseInt((myRed + myGreen + myBlue) / 3)
imageData.data[i] = myGray
imageData.data[i + 1] = myGray
imageData.data[i + 2] = myGray
}
ctx.putImageData(imageData, 0, 0)
}
function scanCode() {
//生成图片的base64码
photograph()
var data = { "code": $("#code").val() }
ajaxPostCompliate("@Url.Action("scan")", data, function (result) {
console.info("-----------result--------------" + result)
if (result != null) {//扫描出结果
var obj = JSON.parse(result)
if (obj.org != undefined) {
location.href = "@Url.Action("ScanResult")?org=" + obj.org + "&tp=" + obj.tp + "&ad=" + obj.ad
} else {
alert("该二维码非ME+授权编码")
}
} else {//继续扫描
setTimeout(function () {
scanCode()
}, 2000)
}
})
}
</script>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)