H5扫码功能实现

H5扫码功能的实现

功能实现要用到H5+

1.首先需要下载Hbuilder,然后创建H5+项目。再把自己的项目文件替换到新创建的项目里。项目要引入common.js
2.添加一个扫码按钮,给按钮添加点击事件
$(".Scan-three").on("click",function(){
  openBarcode()
})
//打开扫码界面
function openBarcode() {
  createWithoutTitle('barcode_scan.html', {
    titleNView: {
      type: 'float',
      backgroundColor: 'rgba(215,75,40,0.3)',
      titleText: '扫一扫',
      titleColor: '#FFFFFF',
      autoBackButton: true,
      buttons: [
        {
          fontSrc: '_www/helloh5.ttf',
          text: '\ue401',
          fontSize: '18px',
          onclick: 'javascript:switchFlash()',
        },
      ],
    },
  })
}
function scaned(t, r) {
  //扫码结果返回这里
console.log(t+r)
}
 
3.新创建一个html,Scan扫码界面代码:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
    />
    <meta name="HandheldFriendly" content="true" />
    <meta name="MobileOptimized" content="320" />
    <title>Scan</title>
    <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
    <style type="text/css">
      #bcid {
        width: 100%;
        position: absolute;
        top: 0px;
        bottom: 0px;
        text-align: center;
      }

      .tip {
        color: #ffffff;
        font-weight: bold;
        text-shadow: 0px -1px #103e5c;
      }
    </style>
  </head>
  <body style="#000000;">
    <div id="bcid">
      <div style="height: 40%;"></div>
      <p class="tip">...载入中...</p>
    </div>
  </body>
  
  <script src="js/scan.min.js"></script>
  <script type="text/javascript" src="js/common.js"></script>
</html>

4.Scan扫码界面js

var ws = null,
  wo = null
var scan = null
// H5 plus事件处理
function plusReady() {
  // 获取窗口对象
  ws = plus.webview.currentWebview()
  wo = ws.opener()
  // 开始扫描
  ws.addEventListener(
    'show',
    function () {
      scan = new plus.barcode.Barcode('bcid')
      scan.onmarked = onmarked
      scan.start({})
      createSubview()
    },
    false
  )

  // 显示页面并关闭等待框
  ws.show('pop-in')
}
document.addEventListener('plusready', plusReady, false)

// 二维码扫描成功

function onmarked(type, result) {
  switch (type) {
    case plus.barcode.QR:
      type = 'QR'
      break
    case plus.barcode.EAN13:
      type = 'EAN13'
      break
    case plus.barcode.EAN8:
      type = 'EAN8'
      break
    default:
      type = '其它' + type
      break
  }
  result = result.replace(/\r\n/g, '')
  wo.evalJS("scaned('" + type + "','" + result + "')")// 里面的方法自己创建自己需要的功能
  back()
}

// 创建子窗口
function createSubview() {
  //
}
// 开关闪光灯
var bFlash = false
var AVCaptureDevice = null
var Camera = null

function switchFlash() {
  bFlash = !bFlash
  scan.setFlash(bFlash)
  ws.setStyle({
    titleNView: {
      buttons: [
        {
          fontSrc: '_www/helloh5.ttf',
          text: bFlash ? '\ue400' : '\ue401',
          fontSize: '18px',
          onclick: 'javascript:switchFlash()',
        },
      ],
    },
  })
}

5.效果图
扫码界面
在这里插入图片描述

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根据提供的引用内容,uni-app官网不支持H5扫码功能,但是可以通过引用中的manifest.json配置来实现H5扫码功能。具体步骤如下: 1.在manifest.json文件中添加如下配置: ```json "h5": { "devServer": { "https": true } } ``` 2.在需要使用扫码功能的页面中引入js文件: ```html <script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script> ``` 3.在页面中添加扫码区域: ```html <div id="qrcode"></div> ``` 4.在页面的script标签中编写生成二维码和扫码的逻辑: ```javascript import QRCode from 'qrcodejs2' export default { mounted() { const qrcode = new QRCode('qrcode', { text: 'https://www.example.com', // 二维码内容 width: 256, // 二维码宽度 height: 256, // 二维码高度 colorDark: '#000000', // 二维码颜色 colorLight: '#ffffff', // 二维码背景色 correctLevel: QRCode.CorrectLevel.H // 二维码容错率 }) qrcode._htOption.colorDark = '#000000' // 二维码颜色 qrcode._htOption.colorLight = '#ffffff' // 二维码背景色 const scanner = new Instascan.Scanner({ video: document.getElementById('preview'), mirror: false }) scanner.addListener('scan', function (content) { console.log(content) }) Instascan.Camera.getCameras().then(function (cameras) { if (cameras.length > 0) { scanner.start(cameras[0]) } else { console.error('No cameras found.') } }).catch(function (e) { console.error(e) }) } } ``` 以上代码中,我们使用了qrcodejs2库来生成二维码,使用了Instascan库来实现扫码功能。需要注意的是,Instascan库需要在https环境下才能正常工作。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值