首页 > 其他分享 >前端H5使用html5QrCode实现扫一扫识别二维码

前端H5使用html5QrCode实现扫一扫识别二维码

时间:2023-04-13 19:00:59浏览次数:35  
标签:html5QrCode scan 100% 扫一扫 height width H5

 

 

 

 

 

 Vue版本:npm install html5-qrcode

<template>
  <div class="index">
    <!-- 扫一扫 -->
    <div class="scan" v-if="isScanning">
      <div class="scan-box">
        <div id="scanning" width="800px"></div>
      </div>
      <span class="close-scan" @click="stopScan"><svg t="1681382339822" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2626" width="200" height="200"><path d="M512 960c-247.039484 0-448-200.960516-448-448S264.960516 64 512 64 960 264.960516 960 512 759.039484 960 512 960zM512 128.287273c-211.584464 0-383.712727 172.128262-383.712727 383.712727 0 211.551781 172.128262 383.712727 383.712727 383.712727 211.551781 0 383.712727-172.159226 383.712727-383.712727C895.712727 300.415536 723.551781 128.287273 512 128.287273z" fill="#ffffff" p-id="2627"></path><path d="M557.05545 513.376159l138.367639-136.864185c12.576374-12.416396 12.672705-32.671738 0.25631-45.248112s-32.704421-12.672705-45.248112-0.25631l-138.560301 137.024163-136.447897-136.864185c-12.512727-12.512727-32.735385-12.576374-45.248112-0.063647-12.512727 12.480043-12.54369 32.735385-0.063647 45.248112l136.255235 136.671523-137.376804 135.904314c-12.576374 12.447359-12.672705 32.671738-0.25631 45.248112 6.271845 6.335493 14.496116 9.504099 22.751351 9.504099 8.12794 0 16.25588-3.103239 22.496761-9.247789l137.567746-136.064292 138.687596 139.136568c6.240882 6.271845 14.432469 9.407768 22.65674 9.407768 8.191587 0 16.352211-3.135923 22.591372-9.34412 12.512727-12.480043 12.54369-32.704421 0.063647-45.248112L557.05545 513.376159z" fill="#ffffff" p-id="2628"></path></svg></span>
    </div>
    <button class="start-scan" v-if="!isScanning"  @click="startScan" >开始扫一扫</button>
  </div>
</template>

<script>
  import { Html5Qrcode } from 'html5-qrcode';
  export default {
    data() {
      return {
        isScanning: false // 是否正在扫一扫
      }
    },
    created() {
    },
    mounted() {
      this.startScan()
    },
    methods: {
      startScan() { // 开始扫码
        this.isScanning = true
        this.$nextTick(() => {
          this.html5QrCode = new Html5Qrcode('scanning');
          this.html5QrCode.start({
              facingMode: "environment"
            }, {
              fps: 10,
              aspectRatio: 1, // 4:3:1.333334 16:9:1.777778 1:1:1.0
              qrbox: {
                width: 280,
                height: 280
              },
            }, (decodedText, decodedResult) => {
              this.stopScan() // 结束扫码
              alert("识别结果" + decodedText)
            }, errorMessage => {
            },
          )
          .catch(err => {});
        })
      },
      stopScan() { // 结束扫码
        this.html5QrCode.stop().then(() => {
          this.isScanning = false
        })
      }
    }
  }
</script>

<style lang="scss">
  .index {
    width: 100%;
    height: 100%;
    overflow: auto;
    .scan{
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: #000;
      .scan-box{
        width: 100%;
      }
      .close-scan{
        position: absolute;
        top: 20px;
        right: 20px;
        svg{
          width: 40px;
          height: 40px;
        }
      }
    }
    .start-scan{
      position: absolute;
      top: calc(50% - 25px);
      left: calc(50% - 100px);
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
      width: 200px;
      height: 50px;
      background: #00aaff;
      color: #fff;
      border-radius: 10px;
    }
  }
</style>

 

标签:html5QrCode,scan,100%,扫一扫,height,width,H5
From: https://www.cnblogs.com/kinwai/p/17316033.html

相关文章

  • taro3.6.4 在H5 端使用 tabBar 出现bug --- taro Cannot read properties of undefi
    taro3.6.4在H5端使用tabBar出现bug解决办法就是换版本3.6.2好像没有这个问题已经有人反馈了,https://github.com/NervJS/taro/issues/13581taroCannotreadpropertiesofundefined(reading'list')......
  • h5使用高德获取用户当前位置信息
    在index.html文件中引入高德js文件:key需要从高德获取获取key<scripttype="text/javascript"src="https://webapi.amap.com/maps?v=1.4.15&key=f77da011880c2d55aeccba6446b85c78"></script>js文件我将方法写入了单独的js文件:locationService.jsimportAMapfr......
  • taro 3.0 官方模板运行报错 插件依赖 "@tarojs/plugin-platform-h5" 加载失败
    taroError:插件依赖"@tarojs/plugin-platform-h5"加载失败,请检查插件配置报错如下,原因:node版本的问题,使用nvm切换node版本就可以了......
  • 可视化H5(h5py)文件
    与深度学习打交道的一般都遇见过后缀名为.h5的文件,它该如何打开?它是一种层级形式的、用于存储和组织大量数据的文件格式。想要直观可视化内容很简单,只需安装一个python第三方库即可pipinstallvitablesvitablesxxxx.h5执行该命后,就会弹出一个可视化页面,此时h5文件......
  • 使用navigator.geolocation解决h5公众号定位不准确的问题
    封装js(utils/geolocation.min.js):window.qq=window.qq||{},qq.maps=qq.maps||{},window.soso||(window.soso=qq),soso.maps||(soso.maps=qq.maps),qq.maps.Geolocation=function(){"usestrict";vare=[],t=null,o=0,n="_geoIframe_"+Math.ceil(1e7*Mat......
  • Python中将CSV文件转换为H5AD文件
    将CSV文件转换为H5AD文件,可以按照以下步骤进行:使用pandas库将CSV文件读入到Python中,并将其转换为dataframe格式: importpandasaspddf=pd.read_csv('data.csv')使用anndata库将dataframe转换为AnnData对象: importanndataasadadata=ad.AnnData(df)将......
  • h5 - pc 使用 pdf.js 预览pdf -配合文件流实现 - 遇到的坑总结
    1.pdf.js下载看我这篇随笔【h5-使用pdf.js预览pdf-岑惜-博客园(cnblogs.com)】2.html调用页面的局部代码<body><divstyle="height:100vh;margin:0auto"><iframestyle="height:100%;width:100%;border:none"id="fvic"src="&......
  • H5 如何实现唤起 APP?
    1、不同平台的实现方式有些不同,一般常见的有这几种,分别是:URLScheme(通用)UniversalLink(iOS)AppLink、ChromeIntents(android)2、推荐使用:3、优缺点对比......
  • h5移动端应用实现类似原生页面切换效果
    1.原理将vue-router中的跳转方法重写并监听,根据不同的跳转类型加载不同的过渡动画。2.实现创建router-helper.js文件exportconstDirectionType={//进入动画In:'in',//退出动画Out:'out',/**不应用动画,用于处理IOS侧滑冲突*/None:''}//路由动画export......
  • 【实战】SpringBoot+uniapp+uview打造H5+小程序+APP入门学习的聊天小项目
    JavaDogChatv1.0.0基于SpringBoot+uniapp简单通讯聊天软件......