framework7+vue+cordova移动开发(三)调用摄像头扫一扫

1.在某个.vue里面添加下面代码

<template>
  <div id="app">
    <p>测试扫一扫</p>
    <!-- 扫一扫按钮 -->
    <button @click="handleClick" >扫一扫</button>
  </div>
</template>

<script>
export default {
  name: 'App',
  el: '#app',
  methods: {
    handleClick: 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(error)
        }
      )
    }
  }
}
</script>

2、在index.html 的引入cordova.js和cordova_plugins.js

<body>
  <div id="app"></div>
  <% if (process.env.TARGET === 'cordova') { %>
  <script type="text/javascript" src="cordova.js"></script>
  <!-- 扫一扫包的引用 -->
  <script type="text/javascript" src="cordova_plugins.js"></script>
  <% } %>
  <!-- built script files will be auto injected -->
</body>

</html>

3、app.js中加入


if (process.env.TARGET === 'cordova'){
  /* eslint-disable no-new */
  document.addEventListener('deviceready', function () {
    // Init App
    new Vue({
      el: '#app',
      panel: {
        swipe: 'left',
        leftBreakpoint: 768,
        rightBreakpoint: 1024,
      },
      render: (h) => h(App),
      // Register App Component
      components: {
        app: App,
        statusbarOverlay:false
      },
    });
    window.navigator.splashscreen.hide()
  }, false);
}else{
  new Vue({
    el: '#app',
    panel: {
      swipe: 'left',
      leftBreakpoint: 768,
      rightBreakpoint: 1024,
    },
    render: (h) => h(App),
    // Register App Component
    components: {
      app: App,
      statusbarOverlay:false
    },
  });
}

4、添加相应的插件
cordova plugin add phonegap-plugin-barcodescanner --save

然后自然就是打包测试了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值