当前位置:   article > 正文

html5qrcode的使用(自留笔记)_html5-qrcode

html5-qrcode

本次项目为基于vue2+vant的H5,套壳APP,无法使用APK的扫码时选择使用h5qrcode进行扫码,实现扫码单。

1.安装html5-qrcode

 npm i html5-qrcode
2.在所需组件处使用

模版:

  1. <div class="canTab" @click="startScan" v-if="!isScanning"><van-icon name="scan" size="45" color="#fff" /></div>
  2. <div class="scan" v-if="isScanning">
  3. <div class="scan-box">
  4. <div id="scanning" width="800px"></div>
  5. </div>
  6. <span class="close-scan" @click="stopScan">
  7. <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">
  8. <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>
  9. <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>
  10. </svg>
  11. </span>
  12. </div>

JS:

  1. // 引入扫描二维码
  2. import { Html5Qrcode } from 'html5-qrcode';
  3. data() {
  4. return {
  5. isScanning: false, // 扫码
  6. }
  7. method: {
  8. //扫码接单
  9. startScan(){
  10. this.isScanning = true
  11. this.$nextTick(() => {
  12. this.html5QrCode = new Html5Qrcode('scanning');
  13. this.html5QrCode.start({
  14. facingMode: "environment"
  15. }, {
  16. fps: 10,
  17. aspectRatio: 1, // 4:31.333334 16:91.777778 1:11.0
  18. qrbox: {
  19. width: 280,
  20. height: 280
  21. },
  22. }, (decodedText, decodedResult) => {
  23. let codeUrl = decodedText; //获取二维码信息
  24. // 以下为自己所需的业务逻辑处理
  25. this.stopScan() // 结束扫码
  26. // alert("识别结果" + decodedText)
  27. }, errorMessage => {
  28. },
  29. )
  30. .catch(err => {});
  31. });
  32. },
  33. // 结束扫码
  34. stopScan() {
  35. this.html5QrCode.stop().then(() => {
  36. this.isScanning = false;
  37. })
  38. },
  39. }

style:

  1. /* 扫码样式 */
  2. .scan {
  3. display: flex;
  4. align-items: center;
  5. justify-content: center;
  6. flex-direction: column;
  7. position: fixed;
  8. top: 0;
  9. left: 0;
  10. width: 100%;
  11. height: 100%;
  12. background: #000;
  13. .scan-box {
  14. width: 100%;
  15. }
  16. .close-scan {
  17. position: absolute;
  18. top: 20px;
  19. right: 20px;
  20. svg {
  21. width: 40px;
  22. height: 40px;
  23. }
  24. }
  25. }
  26. .start-scan{
  27. position: absolute;
  28. top: calc(50% - 25px);
  29. left: calc(50% - 100px);
  30. display: flex;
  31. align-items: center;
  32. justify-content: center;
  33. border: none;
  34. width: 200px;
  35. height: 50px;
  36. background: #00aaff;
  37. color: #fff;
  38. border-radius: 10px;
  39. }

这是复制别人的逻辑,已经表明出处,仅供自己记录笔记,防止以后使用时忘记。

完整代码(此处仅有扫描二维码代码,其他已摘去):

  1. <template>
  2. <div class="driverMyBox">
  3. <div class="canTab" @click="startScan" v-if="!isScanning">
  4. <van-icon name="scan" size="45" color="#fff" />
  5. </div>
  6. <div class="scan" v-if="isScanning">
  7. <div class="scan-box">
  8. <div id="scanning" width="800px"></div>
  9. </div>
  10. <span class="close-scan" @click="stopScan">
  11. <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">
  12. <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>
  13. <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>
  14. </svg>
  15. </span>
  16. </div>
  17. </div>
  18. </template>
  19. <script type='text/javascript'>
  20. // 引入扫描二维码
  21. import { Html5Qrcode } from 'html5-qrcode';
  22. export default {
  23. data() {
  24. return {
  25. //扫码
  26. isScanning: false, // 是否正在扫一扫
  27. }
  28. //扫码接单
  29. startScan(){
  30. this.isScanning = true;
  31. this.$nextTick(() => {
  32. this.html5QrCode = new Html5Qrcode('scanning');
  33. this.html5QrCode.start({
  34. facingMode: "environment"
  35. }, {
  36. fps: 10,
  37. aspectRatio: 1, // 4:31.333334 16:91.777778 1:11.0
  38. qrbox: {
  39. width: 280,
  40. height: 280
  41. },
  42. }, (decodedText, decodedResult) => {
  43. let codeUrl = decodedText; //获取扫码信息
  44. //这里进行自己的业务逻辑操作
  45. this.stopScan() // 结束扫码
  46. // alert("识别结果" + decodedText)
  47. }, errorMessage => {
  48. },
  49. )
  50. .catch(err => {});
  51. });
  52. },
  53. // 结束扫码
  54. stopScan() {
  55. this.html5QrCode.stop().then(() => {
  56. this.isScanning = false;
  57. })
  58. },
  59. }
  60. </script>
  61. <style lang='scss' scoped>
  62. /* 扫码样式 */
  63. .scan {
  64. display: flex;
  65. align-items: center;
  66. justify-content: center;
  67. flex-direction: column;
  68. position: fixed;
  69. top: 0;
  70. left: 0;
  71. width: 100%;
  72. height: 100%;
  73. background: #000;
  74. .scan-box {
  75. width: 100%;
  76. }
  77. .close-scan {
  78. position: absolute;
  79. top: 20px;
  80. right: 20px;
  81. svg {
  82. width: 40px;
  83. height: 40px;
  84. }
  85. }
  86. }
  87. .start-scan{
  88. position: absolute;
  89. top: calc(50% - 25px);
  90. left: calc(50% - 100px);
  91. display: flex;
  92. align-items: center;
  93. justify-content: center;
  94. border: none;
  95. width: 200px;
  96. height: 50px;
  97. background: #00aaff;
  98. color: #fff;
  99. border-radius: 10px;
  100. }
  101. </style>

技术借鉴出处:

https://www.cnblogs.com/kinwai/p/17316033.html

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

闽ICP备14008679号