当前位置:   article > 正文

vue3 html5-qrcode 实现扫描二维码 仿照wx扫码样式_vue3 html 扫一扫

vue3 html 扫一扫

直接上代码:npm i html5-qrcode

  1. <van-button class="scanButton" @click="stop" type="success">结束扫码</van-button>
  2. <van-search v-model="searchName" show-action label="" placeholder="请输入姓名" @search="onSearch">
  3. <template #action>
  4. <div @click="getCameras">
  5. <van-icon name="scan" />
  6. 扫码报到
  7. </div>
  8. </template>
  9. </van-search>
  10. import { Html5Qrcode } from 'html5-qrcode';
  11. const getCameras = () => {
  12. scanCodeSta.value = true;
  13. // showToast('正在初始化扫码,请稍后');
  14. showToast({
  15. message: '正在初始化扫码,请稍后',
  16. position: 'bottom',
  17. });
  18. Html5Qrcode.getCameras().then((devices) => {
  19. if (devices && devices.length) {
  20. // 如果有2个摄像头,1为前置的
  21. if (devices.length > 1) {
  22. cameraId.value = devices[1].id;
  23. } else {
  24. cameraId.value = devices[0].id;
  25. }
  26. devicesInfo.value = devices;
  27. // start开始扫描
  28. start();
  29. }
  30. }).catch((err) => {
  31. console.log('获取设备信息失败', err); // 获取设备信息失败
  32. showToast('获取设备信息失败');
  33. });
  34. }
  35. const start = () => {
  36. html5QrCode.value = new Html5Qrcode('reader');
  37. setTimeout(() => {
  38. closeToast();
  39. }, 700);
  40. html5QrCode.value.start(cameraId.value, {
  41. focusMode: 'continuous', //设置连续聚焦模式
  42. // fps: 10, // 设置每秒多少帧
  43. fps: 5, //设置扫码识别速度
  44. // qrbox: { width: '100%', height: '100%' }, // 设置取景范围
  45. qrbox: { width: 320, height: 320 }, // 设置取景范围
  46. // aspectRatio: 1.777778, // 可选,视频馈送需要的纵横比,(4:3--1.333334, 16:9--1.777778, 1:1--1.0)传递错误的纵横比会导致视频不显示
  47. }, (decodedText, decodedResult) => {
  48. // showToast('扫描的结果===', decodedText, decodedResult);
  49. result.value = decodedText;
  50. if (decodedText) {
  51. stop();
  52. handleScanCheckin();
  53. }
  54. }, (errorMessage) => {
  55. // showToast('暂无扫描结果');
  56. console.log('暂无扫描结果', errorMessage);
  57. }).catch((err) => {
  58. console.log(`Unable to start scanning, error: ${err}`);
  59. });
  60. }
  61. const stop = () => {
  62. scanCodeSta.value = false;
  63. if (!html5QrCode.value) {
  64. return;
  65. }
  66. html5QrCode.value.stop().then((ignore) => {
  67. console.log('QR Code scanning stopped.', ignore);
  68. }).catch((err) => {
  69. console.log('Unable to stop scanning.', err);
  70. });
  71. }
  72. :deep(#qr-shaded-region) {
  73. overflow: hidden;
  74. }
  75. :deep(#qr-shaded-region::after) {
  76. position: absolute;
  77. content: '';
  78. width: 100%;
  79. top: 0;
  80. left: 0;
  81. transform: translateY(-100%);
  82. height: 50px;
  83. background: linear-gradient(180deg, rgba(0, 255, 51, 0) 43%, #00ff33 211%);
  84. border-bottom: 1px solid #00ff33;
  85. animation: scan-animation 2s linear infinite;
  86. }
  87. @keyframes scan-animation {
  88. 0% {
  89. top: 0;
  90. }
  91. 100% {
  92. top: 100%;
  93. }
  94. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/859654
推荐阅读
相关标签
  

闽ICP备14008679号