如何在Phonegap中扫描QR码

时间:2016-06-23 05:32:02

标签: javascript cordova phonegap-plugins

我正在开始使用phonegap,我在phonegap中学习了许多插件,但是当我想在手机屏幕中使用QR扫描仪时,我会陷入困境。如果有

,请给我任何解决方案

1 个答案:

答案 0 :(得分:6)

在我的cordova应用程序中尝试了这个二维码扫描。希望它应该工作正常。代码如下:

<强>的index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>QR Scanner</title>
    </head>
    <body>        
        <script type="text/javascript" src="cordova.js"></script>   
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/app.js"></script>
        Click scan button to scan the QR code: <input type="button" value="scan" name = "scan" id="scan"/>
    </body>
</html>

<强> app.js

$(document).ready(function() {
    document.addEventListener("deviceready", onDeviceReady, false);
});

function onDeviceReady() {      
     $('#scan').click( function() 
        {
          cordova.plugins.barcodeScanner.scan(
          function (result) {
              alert("We got a barcode\n" +
                    "Result: " + result.text + "\n" +
                    "Format: " + result.format + "\n" +
                    "Cancelled: " + result.cancelled);            
          }, 
          function (error) {
              alert("Scanning failed: " + error);
          });
        }
     );
}

确保从phonegap-plugin-barcodescanner link添加jquery库js文件和QR代码扫描程序插件以试用此示例。