当前位置:   article > 正文

vue+h5开发扫描二维码 html5-qrcode(条形码识别不到)_vue2 html5-qrcode

vue2 html5-qrcode

前提

由于涉及到调用手机摄像头,涉及到隐私,必须在HTTPS环境使用。

  1. <template>
  2. <view class="qrcode">
  3. <uni-nav-bar left-icon="left" :background-color="' '" :border="false" title="扫码" :fixed="true" @clickLeft="$back" :shadow='true' :leftText='"取消"'>
  4. </uni-nav-bar>
  5. <view id="reader"></view>
  6. <view class="error" v-if="isError">
  7. <view class="on1">相机权限被拒绝,请尝试如下操作:</view>
  8. <view>· 刷新页面后重试;</view>
  9. <view>· 在系统中检测当前App或浏览器的相机权限是否被禁用;</view>
  10. <view>· 如果依然不能体验,建议在微信中打开链接;</view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import {
  16. Html5Qrcode
  17. } from "html5-qrcode";
  18. export default {
  19. props:{
  20. isError:{
  21. type:Boolean,
  22. default:false
  23. }
  24. },
  25. created() {
  26. this.getCameras()
  27. },
  28. beforeDestroy() {
  29. this.stop();
  30. },
  31. methods: {
  32. $back(){
  33. uni.navigateBack()
  34. },
  35. getCameras() {
  36. Html5Qrcode.getCameras()
  37. .then((devices) => {
  38. console.log(devices,'sss')
  39. if (devices && devices.length) {
  40. this.html5QrCode = new Html5Qrcode("reader");
  41. this.start();
  42. }
  43. })
  44. .catch((err) => {
  45. // handle err
  46. this.html5QrCode = new Html5Qrcode("reader");
  47. this.error = "ERROR: 您需要授予相机访问权限"
  48. this.$emit("err", this.error)
  49. });
  50. },
  51. start() {
  52. //environment后置 user前置
  53. this.html5QrCode
  54. .start({
  55. facingMode: "environment"
  56. }, {
  57. fps: 2,
  58. qrbox: {
  59. width: 200,
  60. height: 200
  61. },
  62. },
  63. (decodedText, decodedResult) => {
  64. this.$emit("ok", decodedText)
  65. }
  66. )
  67. .catch((err) => {
  68. this.$emit("err", err)
  69. });
  70. },
  71. stop() {
  72. this.html5QrCode.stop().then((ignore) => {
  73. // QR Code scanning is stopped.
  74. console.log("QR Code scanning stopped.");
  75. })
  76. .catch((err) => {
  77. // Stop failed, handle it.
  78. console.log("Unable to stop scanning.");
  79. });
  80. },
  81. }
  82. }
  83. </script>
  84. <style scoped lang="scss" >
  85. .qrcode {
  86. position: absolute;
  87. top: 0;
  88. left: 0;
  89. width: 100vw;
  90. height: 100vh;
  91. // position: relative;
  92. background-image: linear-gradient(0deg, transparent 24%, rgba(32, 255, 77, .1) 25%, rgba(32, 255, 77, .1) 26%, transparent 27%, transparent 74%, rgba(32, 255, 77, .1) 75%, rgba(32, 255, 77, .1) 76%, transparent 77%, transparent), linear-gradient(90deg, transparent 24%, rgba(32, 255, 77, .1) 25%, rgba(32, 255, 77, .1) 26%, transparent 27%, transparent 74%, rgba(32, 255, 77, .1) 75%, rgba(32, 255, 77, .1) 76%, transparent 77%, transparent);
  93. background-size: 3rem 3rem;
  94. background-position: -1rem -1rem;
  95. z-index: 10;
  96. background-color: rgba(17, 17, 17, 0);
  97. z-index: 999;
  98. }
  99. #reader {
  100. position: absolute;
  101. top: 50%;
  102. left: 0;
  103. transform: translateY(-50%);
  104. z-index: 1000;
  105. }
  106. .error {
  107. color: #fff;
  108. padding: 40rpx;
  109. font-size: 24rpx;
  110. background-color: #333;
  111. position: fixed;
  112. top: 50%;
  113. left: 50%;
  114. transform: translate(-50%, -50%);
  115. width: 630rpx;
  116. border-radius: 15rpx;
  117. }
  118. .on1{
  119. font-size: 30rpx;
  120. }
  121. </style>

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

闽ICP备14008679号