aa.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div class="page-scan">
  3. <!--返回-->
  4. <van-nav-bar
  5. title="扫描二维码/条形码"
  6. fixed
  7. @click-left="clickIndexLeft()"
  8. class="scan-index-bar"
  9. >
  10. <template #left>
  11. <van-icon name="arrow-left" size="18" color="#fff" />
  12. <span style="color: #fff"> 取消 </span>
  13. </template>
  14. </van-nav-bar>
  15. <!-- 扫码区域 -->
  16. <video ref="video" id="video" class="scan-video" autoplay></video>
  17. <!-- 提示语 -->
  18. <div v-show="tipShow" class="scan-tip">{{ tipMsg }}</div>
  19. </div>
  20. </template>
  21. <script>
  22. import { BrowserMultiFormatReader } from "@zxing/library";
  23. // import { Dialog, Notify } from 'vant';
  24. import { Dialog } from "vant";
  25. export default {
  26. name: "scanCodePage",
  27. components: {
  28. [Dialog.Component.name]: Dialog.Component,
  29. },
  30. data() {
  31. return {
  32. loadingShow: false,
  33. codeReader: null,
  34. scanText: "",
  35. vin: null,
  36. tipMsg: "正在尝试识别....",
  37. tipShow: false,
  38. };
  39. },
  40. created() {
  41. this.codeReader = new BrowserMultiFormatReader();
  42. console.log("测试", this.codeReader);
  43. this.openScan();
  44. },
  45. destroyed() {
  46. this.codeReader.reset();
  47. },
  48. watch: {
  49. $route(to, from) {
  50. if (to.path == "/scanCodePage") {
  51. this.codeReader = new BrowserMultiFormatReader();
  52. this.openScanTwo();
  53. }
  54. },
  55. },
  56. methods: {
  57. async openScan() {
  58. this.codeReader
  59. .getVideoInputDevices()
  60. .then((videoInputDevices) => {
  61. this.tipShow = true;
  62. this.tipMsg = "正在调用摄像头...";
  63. console.log("videoInputDevices", videoInputDevices);
  64. // 默认获取第一个摄像头设备id
  65. let firstDeviceId = videoInputDevices[0].deviceId;
  66. // 获取第一个摄像头设备的名称
  67. const videoInputDeviceslablestr = JSON.stringify(
  68. videoInputDevices[0].label
  69. );
  70. if (videoInputDevices.length > 1) {
  71. // 判断是否后置摄像头
  72. if (videoInputDeviceslablestr.indexOf("back") > -1) {
  73. firstDeviceId = videoInputDevices[0].deviceId;
  74. } else {
  75. firstDeviceId = videoInputDevices[1].deviceId;
  76. }
  77. }
  78. this.decodeFromInputVideoFunc(firstDeviceId);
  79. })
  80. .catch((err) => {
  81. this.tipShow = false;
  82. console.error(err);
  83. });
  84. },
  85. async openScanTwo() {
  86. this.codeReader = await new BrowserMultiFormatReader();
  87. this.codeReader
  88. .getVideoInputDevices()
  89. .then((videoInputDevices) => {
  90. this.tipShow = true;
  91. this.tipMsg = "正在调用摄像头...";
  92. console.log("videoInputDevices", videoInputDevices);
  93. // 默认获取第一个摄像头设备id
  94. let firstDeviceId = videoInputDevices[0].deviceId;
  95. // 获取第一个摄像头设备的名称
  96. const videoInputDeviceslablestr = JSON.stringify(
  97. videoInputDevices[0].label
  98. );
  99. if (videoInputDevices.length > 1) {
  100. // 判断是否后置摄像头
  101. if (videoInputDeviceslablestr.indexOf("back") > -1) {
  102. firstDeviceId = videoInputDevices[0].deviceId;
  103. } else {
  104. firstDeviceId = videoInputDevices[1].deviceId;
  105. }
  106. }
  107. this.decodeFromInputVideoFunc(firstDeviceId);
  108. })
  109. .catch((err) => {
  110. this.tipShow = false;
  111. console.error(err);
  112. });
  113. },
  114. decodeFromInputVideoFunc(firstDeviceId) {
  115. this.codeReader.reset(); // 重置
  116. this.scanText = "";
  117. this.codeReader.decodeFromInputVideoDeviceContinuously(
  118. firstDeviceId,
  119. "video",
  120. (result, err) => {
  121. this.tipMsg = "正在尝试识别...";
  122. this.scanText = "";
  123. if (result) {
  124. Dialog.alert({
  125. title: "扫描结果",
  126. message: result.text,
  127. }).then(() => {
  128. this.codeReader = null;
  129. this.$destroy();
  130. this.$router.back();
  131. });
  132. console.log("扫描结果", result);
  133. this.scanText = result.text;
  134. if (this.scanText) {
  135. this.tipShow = false;
  136. // 这部分接下去的代码根据需要,读者自行编写了
  137. // this.$store.commit('app/SET_SCANTEXT', result.text);
  138. // console.log('已扫描的小票列表', this.$store.getters.scanTextArr);
  139. }
  140. }
  141. if (err && !err) {
  142. this.tipMsg = "识别失败";
  143. setTimeout(() => {
  144. this.tipShow = false;
  145. }, 2000);
  146. console.error(err);
  147. }
  148. }
  149. );
  150. },
  151. clickIndexLeft() {
  152. // 返回上一页
  153. this.codeReader = null;
  154. this.$destroy();
  155. this.$router.back();
  156. },
  157. },
  158. };
  159. </script>
  160. <style lang="scss">
  161. .scan-index-bar {
  162. background-image: linear-gradient(-45deg, #42a5ff, #59cfff);
  163. }
  164. .van-nav-bar__title {
  165. color: #fff !important;
  166. }
  167. .scan-video {
  168. height: 80vh;
  169. }
  170. .scan-tip {
  171. width: 100vw;
  172. text-align: center;
  173. margin-bottom: 10vh;
  174. color: white;
  175. font-size: 5vw;
  176. }
  177. .page-scan {
  178. overflow-y: hidden;
  179. background-color: #363636;
  180. }
  181. </style>