温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在HTML5中使用MUI框架实现一个二维码扫描功能

发布时间:2021-04-13 17:12:14 来源:亿速云 阅读:229 作者:Leah 栏目:web开发

怎么在HTML5中使用MUI框架实现一个二维码扫描功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<!doctype html>   <html>      <head>       <meta charset="UTF-8">       <title></title>       <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />       <link href="css/mui.min.css" rel="stylesheet" />       <script src="js/mui.min.js"></script>       <style type="text/css">           #bcid{               width: 100%;               height: 100%;               position: absolute;               background: #000000;           }           html, body ,div{               height:100%;               width: 100%;           }           .fbt{               color: #0E76E1;               width: 50%;               background-color: #ffffff;               float: left;                line-height: 44px;               text-align: center;           }       </style>      </head>     <body>       <header class="mui-bar mui-bar-nav" style="background-color: #ffffff;">         <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>         <h2 class="mui-title" style="color: #0E76E1;">物品二维码扫描</h2>         <span class="mui-icon mui-icon-spinner-cycle mui-spin mui-pull-right" id="turnTheLight"></span>       </header>       <div id="bcid">               <!--盛放扫描控件的div-->               </div>       <div class="mui-bar mui-bar-footer" style="padding: 0px;">           <div class="fbt" onclick="scanPicture();">从相册选择二维码</div>           <div class="fbt mui-action-back">取  消</div>       </div>       <script type="text/javascript">              scan = null;//扫描对象           mui.plusReady(function () {                 mui.init();             startRecognize();              });           function startRecognize(){              try{                 var filter;                //自定义的扫描控件样式                var styles = {frameColor: "#29E52C",scanbarColor: "#29E52C",background: ""}               //扫描控件构造               scan = new plus.barcode.Barcode('bcid',filter,styles);               scan.onmarked = onmarked;                scan.onerror = onerror;               scan.start();               //打开关闭闪光灯处理               var flag = false;               document.getElementById("turnTheLight").addEventListener('tap',function(){                  if(flag == false){                     scan.setFlash(true);                     flag = true;                  }else{                    scan.setFlash(false);                    flag = false;                  }               });             }catch(e){               alert("出现错误啦:\n"+e);                }             };               function onerror(e){                       alert(e);               };               function onmarked( type, result ) {                       var text = '';                       switch(type){                           case plus.barcode.QR:                           text = 'QR: ';                           break;                           case plus.barcode.EAN13:                           text = 'EAN13: ';                           break;                           case plus.barcode.EAN8:                           text = 'EAN8: ';                           break;                       }                       alert( text + " : "+ result );               };             // 从相册中选择二维码图片            function scanPicture() {               plus.gallery.pick(function(path){                   plus.barcode.scan(path,onmarked,function(error){                       plus.nativeUI.alert( "无法识别此图片" );                   });               },function(err){                   plus.nativeUI.alert("Failed: "+err.message);               });           }                  </script>       </body>   </html>

三、做的过程中遇见的问题

   a,div占满整个页面

       1,此div宽高都为100%,父级元素的高度也为此(依次类推直至根节点),或者此div的position为absolute;

       2,可采用js动态设置页面宽高

var height = window.innerHeight + 'px';//获取页面实际高度   var width = window.innerWidth + 'px';   document.getElementById("bcid").style.height= height;   document.getElementById("bcid").style.width= width;

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI