条码扫描主要还是用于手机扫描二维码的功能,其他功能也是可以的如:一维码、二维码 、条形码等等
<!DOCTYPE html><html><head><meta charset="utf-8"/><meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/><title>心得</title><script type="text/javascript">//二维码扫码是不需要检查是否已载入plus,因为用户得点击按钮后才能开始扫码,在用户点击按钮之前plus早已载入//创建扫描控件var scan=null;function startRecognize() {//Barcode(某个div的id值,)document.getElementById("bcid").style.display='block';scan = new plus.barcode.Barcode('bcid');scan.onmarked = onmarked; //扫码成功时回调的函数scan.onerror = onerror; //扫码失败时回调的函数setTimeout(function(){startScan();},1000);//延迟1秒后开始扫码(几乎没有人在点击扫码后刚刚好二维码就放前面,就算放前面延迟一秒后在扫码也没事)}//onmarked函数返回两个值,识别到的条码类型和识别到的条码数据function onmarked(type,result) {if(type!=0){//不是二维码alert('当前扫描到的不是二维码');return '';}document.getElementById("ewm").innerHTML = result;//扫码成功后的内容document.getElementById("bcid").style.display='none';scan.close();}function onerror(cuowu){alert('扫码失败:'+cuowu.message);}function startScan(){scan.start();//开始调用系统摄像头获取图片数据进行扫描识别,当识别出条码数据时通过onmarked回调函数返回。}function cancelScan(){document.getElementById("bcid").style.display='none';scan.close();//还是觉得用关闭代替取消比较合适}</script></head><body><input type='button' οnclick='startRecognize()' value='创建扫描控件' /><!--input type='button' οnclick='startScan()' value='开始扫描' /--><input type='button' οnclick='cancelScan()' value='取消扫描' /><div id= "bcid" style="height:500px;width: 100%;background:#333333;display: none;"></div><div style="height: 35px;line-height: 35px;" id="ewm">二维码扫码显示区域</div></body></html>