移动端H5内嵌扫一扫实现拍照且解析二维码

2018-09-12  本文已影响0人  AjingA

若直接使用此代码请下载qrcode.js代码并引入

     <!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" />
     <style type="text/css">
        body {
            margin: 0;
        }
        
        .module-content {
            min-width: 320px;
            max-width: 1000px;
            width: 100%;
            color: #000;
            margin: 0 auto;
            padding-left: 10px;
            box-sizing: border-box;
        }
        
        .box h3 {
            font-weight: 300;
            margin: 0;
            font-size: 20px;
            height: 60px;
            line-height: 60px;
            color: #000;
        }
        
        .url-box {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
        }
        
        #file {
            position: absolute;
            width: 120px;
            height: 120px;
            opacity: 0;
            top: 0;
            left: 0;
            overflow: hidden;
            z-index: 10;
        }
    </style>
</head>

<body>
    <div class="module-content">
        <div class="box">
            <div style="position: relative;">
                <img style="width: 120px;height: 120px;" src="http://www.jq22.com/demo/jQueryQrcode201709202327/tu.png" />
                <input type="file" onchange="getUrl(this,'file-url')" name="" id="file" value="" />
                <p class="url-box" id="urlBox"></p>
            </div>
        </div>
    </div>
    <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
    <script src="qrcode.js" type="text/javascript" charset="utf-8"></script>
    <script src="code.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        function getUrl(e, param) {
            analyticCode.getUrl(param, e, function(url1, url2) {
                e.nextElementSibling.innerHTML = url1;
                e.previousElementSibling.src = url2;
            });
        }
                 
        !function(){
           let getObjectURL = function(file){
           let url = null ;
          if (window.createObjectURL!=undefined) { // basic
               url = window.createObjectURL(file) ;
             } else if (window.URL!=undefined) { // mozilla(firefox)
               url = window.URL.createObjectURL(file) ;
             } else if (window.webkitURL!=undefined) { // webkit or chrome
               url = window.webkitURL.createObjectURL(file) ;
             }
             return url ;
             }


        window.analyticCode = {
               getUrl : function(type,elem,fn){
               let url = null,src = null;
                 if(type === 'img-url'){
                    url = elem.src;
                   }else if(type === 'file-url' && elem.files.length > 0){
                    url = getObjectURL(elem.files[0]);
                   }
                    qrcode.decode(url);
                    qrcode.callback = function(imgMsg){
                   fn(imgMsg,url);
                   }
             }
           }
         }()

    </script>
  </body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读