Home > Web Front-end > H5 Tutorial > HTML5 hybrid development QR code scanning and calling local camera example tutorial

HTML5 hybrid development QR code scanning and calling local camera example tutorial

小云云
Release: 2017-12-30 13:43:35
Original
8380 people have browsed it

I received a task today, saying that I need to use hybrid development and use H5 to call the local camera to scan the QR code. I have done native Android QR code scanning before, mainly by calling the zxing plug-in. , and even got a flash. But I had never touched a pure H5, and I didn’t know what to do with it, so I went home at night and started looking for solutions online. The following is my understanding and code of H5 scanning QR codes and calling local cameras. I hope Can help everyone.

Popular science website:

How H5 generates Android component objects
H5 calls Android local camera api
Online QR code image generator

QR code scanning :(Using the mui framework, the following is the html code)


<!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="">  
      <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>  
      <h1 class="mui-title" style="color: #0E76E1;">H5webapp二维码扫描</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">  
    
    var height = window.innerHeight + &#39;px&#39;;//获取页面实际高度  
    var width = window.innerWidth + &#39;px&#39;;  
    document.getElementById("bcid").style.height= height;  
    document.getElementById("bcid").style.width= width; 

           scan = null;//扫描对象  
        mui.plusReady(function () {  //通过mui初始化扫描
              mui.init();  
          startRecognize();  
           });  
              
        function startRecognize(){  //开启扫描
           try{  
              var filter;  
             //自定义的扫描控件样式  
             var styles = {frameColor: "#29E52C",scanbarColor: "#29E52C",background: ""}  
            //扫描控件构造  
            scan = new plus.barcode.Barcode(&#39;bcid&#39;,filter,styles);  
            scan.onmarked = onmarked;   
            scan.onerror = onerror;  //扫描错误
            scan.start();  
            //打开关闭闪光灯处理  
            var flag = false;  
            document.getElementById("turnTheLight").addEventListener(&#39;tap&#39;,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 ) {  //这个是扫描二维码的回调函数,type是扫描二维码回调的类型
                    var text = &#39;&#39;;  
                    switch(type){ //QR,EAN13,EAN8都是二维码的一种编码格式,result是返回的结果
                        case plus.barcode.QR:  
                        text = &#39;QR: &#39;;  
                        break;  
                        case plus.barcode.EAN13:  
                        text = &#39;EAN13: &#39;;  
                        break;  
                        case plus.barcode.EAN8:  
                        text = &#39;EAN8: &#39;;  
                        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>
Copy after login

The following is the package structure: the development tool is hbuilder

mui.plusReady function is mainly for initialization

startRecognize() is to enable the function of scanning QR codes

onerror is the error message

onmarked is the key point, it is the callback function after scanning the QR code, type is the QR code recognition type, and result is the QR code callback The content

scanPicture() can directly identify the local QR code picture and analyze it

H5 calls the local camera


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <script type="text/javascript" charset="utf-8">
        document.addEventListener( "plusready", function(){
            mui.init();
        });
        function getCamera(){
            var cam = plus.camera.getCamera();
            //字符串数组,摄像头支持的拍照分辨率
            var Resolutions = cam.supportedImageResolutions[0];
            //字符串数组,摄像头支持的拍照文件格式
            var Formats = cam.supportedImageFormats[0];
            //调用拍照方法
            //capturedFile,拍照完成后,照片的存放地址
            cam.captureImage(function(capturedfile){
                //拍照成功
                alert(capturedfile);//打印一下
            },function(){
                //拍照失败
            },{
                //拍照参数
                format : Formats,
                index : 1//1表示主摄像头,2表示辅摄像头
            });
            
            var Resolutions = cam.supportedImageResolutions[0];
            //字符串数组,摄像头支持的拍照文件格式
            var Formats = cam.supportedImageFormats[0];
            //调用拍照方法
            //capturedFile,拍照完成后,照片的存放地址
            cam.captureImage(function(capturedFile){
                //拍照成功
                alert(capturedFile);//打印一下
                //调用系统方法,根据照片地址获取照片
                plus.io.resolvLocalFileSystemURL(capturedFile,
                    //成功的回调函数
                    //entry文件的相关信息
                    function(entry){
                        var img = document.createElement("img");
                        img.src = entry.toLocalURL();
                        document.documentElement.appendChild(img);
                    },function(){
                        //失败的回调函数
                    });
            });
        }
    </script>
</head>
<body>
    <button onclick="getCamera()">照相</button>
</body>
</html>
Copy after login

mui.init(); is the initialization of mui framework

getCamera() method is to first generate the camera object, then take a photo, and after taking the photo, print out the generated image path.

Have you learned it? I feel like giving it a try.

Related recommendations:

How to implement QR code recognition in PHP - example sharing

How to generate QR code in TP5 Encapsulation in

Use plug-in to generate QR code notes

The above is the detailed content of HTML5 hybrid development QR code scanning and calling local camera example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template