基于MUI框架的HTML5+的二维码扫描实现

来源:互联网 发布:ubuntu 22端口被拒绝 编辑:程序博客网 时间:2024/06/03 11:45
Barcode的一个实现案例
一、简介
        Barcode模块管理条码扫描,提供常见的条码(二维码及一维码)的扫描识别功能,可调用设备的摄像头对条码图片扫描进行数据输入。通过plus.barcode可获取条码码管理对象。

二、实现的效果

实现效果图


三、实现 代码

[html] view plain copy
  1. <!doctype html>  
  2. <html>  
  3.    <head>  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
  7.     <link href="css/mui.min.css" rel="stylesheet" />  
  8.     <script src="js/mui.min.js"></script>  
  9.     <style type="text/css">  
  10.         #bcid{  
  11.             width: 100%;  
  12.             height: 100%;  
  13.             position: absolute;  
  14.             background: #000000;  
  15.         }  
  16.         html, body ,div{  
  17.             height:100%;  
  18.             width: 100%;  
  19.         }  
  20.         .fbt{  
  21.             color: #0E76E1;  
  22.             width: 50%;  
  23.             background-color: #ffffff;  
  24.             float: left;   
  25.             line-height: 44px;  
  26.             text-align: center;  
  27.         }  
  28.     </style>  
  29.    </head>  
  30.   <body>  
  31.     <header class="mui-bar mui-bar-nav" style="background-color: #ffffff;">  
  32.       <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>  
  33.       <h1 class="mui-title" style="color: #0E76E1;">物品二维码扫描</h1>  
  34.       <span class="mui-icon mui-icon-spinner-cycle mui-spin mui-pull-right" id="turnTheLight"></span>  
  35.     </header>  
  36.           
  37.     <div id="bcid">     
  38.          <!--盛放扫描控件的div-->          
  39.     </div>  
  40.           
  41.     <div class="mui-bar mui-bar-footer" style="padding: 0px;">  
  42.         <div class="fbt" onclick="scanPicture();">从相册选择二维码</div>  
  43.         <div class="fbt mui-action-back">取  消</div>  
  44.     </div>  
  45.           
  46.     <script type="text/javascript">  
  47.            scan = null;//扫描对象  
  48.         mui.plusReady(function () {  
  49.               mui.init();  
  50.           startRecognize();  
  51.            });  
  52.               
  53.         function startRecognize(){  
  54.            try{  
  55.               var filter;  
  56.              //自定义的扫描控件样式  
  57.              var styles = {frameColor: "#29E52C",scanbarColor: "#29E52C",background: ""}  
  58.             //扫描控件构造  
  59.             scan = new plus.barcode.Barcode('bcid',filter,styles);  
  60.             scan.onmarked = onmarked;   
  61.             scan.onerror = onerror;  
  62.             scan.start();  
  63.             //打开关闭闪光灯处理  
  64.             var flag = false;  
  65.             document.getElementById("turnTheLight").addEventListener('tap',function(){  
  66.                if(flag == false){  
  67.                   scan.setFlash(true);  
  68.                   flag = true;  
  69.                }else{  
  70.                  scan.setFlash(false);  
  71.                  flag = false;  
  72.                }  
  73.             });  
  74.           }catch(e){  
  75.             alert("出现错误啦:\n"+e);  
  76.              }  
  77.           };  
  78.             function onerror(e){  
  79.                     alert(e);  
  80.             };  
  81.             function onmarked( type, result ) {  
  82.                     var text = '';  
  83.                     switch(type){  
  84.                         case plus.barcode.QR:  
  85.                         text = 'QR: ';  
  86.                         break;  
  87.                         case plus.barcode.EAN13:  
  88.                         text = 'EAN13: ';  
  89.                         break;  
  90.                         case plus.barcode.EAN8:  
  91.                         text = 'EAN8: ';  
  92.                         break;  
  93.                     }  
  94.                     alert( text + " : "+ result );  
  95.                       
  96.             };    
  97.                   
  98.         // 从相册中选择二维码图片   
  99.         function scanPicture() {  
  100.             plus.gallery.pick(function(path){  
  101.                 plus.barcode.scan(path,onmarked,function(error){  
  102.                     plus.nativeUI.alert( "无法识别此图片" );  
  103.                 });  
  104.             },function(err){  
  105.                 plus.nativeUI.alert("Failed: "+err.message);  
  106.             });  
  107.         }         
  108.                   
  109.         </script>  
  110.     </body>  
  111. </html>  


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

   a,div占满整个页面
       1,此div宽高都为100%,父级元素的高度也为此(依次类推直至根节点),或者此div的position为absolute;
       2,可采用js动态设置页面宽高
[html] view plain copy
  1. var height = window.innerHeight + 'px';//获取页面实际高度  
  2. var width = window.innerWidth + 'px';  
  3. document.getElementById("bcid").style.heightheight;  
  4. document.getElementById("bcid").style.widthwidth;  
原创粉丝点击