MUI框架使用HTML5实现二维码扫描功能


本文摘自PHP中文网,作者小云云,侵删。

一、简介

Barcode模块管理条码扫描,提供常见的条码(二维码及一维码)的扫描识别功能,可调用设备的摄像头对条码图片扫描进行数据输入。通过plus.barcode可获取条码码管理对象。

二、实现的效果

三、实现 代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

<!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 ,p{ 

            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> 

      <h1 class="mui-title" style="color: #0E76E1;">物品二维码扫描</h1> 

      <span class="mui-icon mui-icon-spinner-cycle mui-spin mui-pull-right" id="turnTheLight"></span> 

    </header> 

    <p id="bcid">    

         <!--盛放扫描控件的p-->         

    </p> 

    <p class="mui-bar mui-bar-footer" style="padding: 0px;"

        <p class="fbt" onclick="scanPicture();">从相册选择二维码</p> 

        <p class="fbt mui-action-back">取  消</p> 

    </p> 

    <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,p占满整个页面

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

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

1

2

3

4

var height = window.innerHeight + 'px';//获取页面实际高度 

var width = window.innerWidth + 'px'

document.getElementById("bcid").style.height= height; 

document.getElementById("bcid").style.width= width;

b,扫描控件有上下边距

采用填充黑色来淡化视觉上面的差异,未实际解决,(如果你解决的话,欢迎留言,谢谢)

相关推荐:

HTML5混合开发二维码扫描以及调用本地摄像头实例教程

H5制作二维码扫描和解析的代码实例

Html5实现二维码扫描并解析 _html5教程技巧

以上就是MUI框架使用HTML5实现二维码扫描功能的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

HTML5 storag存储详解

详解HTML5头部meta标签的功能

HTML5 canvas基本绘图之绘制阴影效果

什么是HTML5技术

HTML5/css3 3d立方体旋转动画经典案例

快速开发基于HTML5网络拓扑图应用的详解(图文)

HTML5标签选择方法指引

HTML5怎么禁止横屏

详解h5新属性audio音频和video视频的控制代码实例

h5文件异步上传

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...