用HTML5 可以实现二维码扫描识别的功能吗
2个回答
2016-12-01 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
恰好这几天在尝试,没有兼容所有浏览器。只在手机上试过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: absolute; top: 0; left: 0;width:100%;height:100%;text-align: center;border: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:auto;width:200px;height:200px;border: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 = 0; i < length * 4; i += 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>
---------------------------------------------------------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: absolute; top: 0; left: 0;width:100%;height:100%;text-align: center;border: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:auto;width:200px;height:200px;border: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 = 0; i < length * 4; i += 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>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
信飞智创
2024-12-23 广告
2024-12-23 广告
二维码防伪标签的制作涉及多个步骤。首先,设计防伪元素,如独特的图案和文字,确保难以仿制。接着,选择可信赖的二维码生成工具,为每个标签生成标识码。随后,将防伪元素嵌入二维码图案中,并选择合适材料进行印刷。印刷完成后,对标签进行质检,确保符合设...
点击进入详情页
本回答由信飞智创提供
展开全部
损公肥私 私心杂念
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询