移动端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

2 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <template>
  2. <div class="scaner" ref="scaner">
  3. <div class="banner" v-if="showBanner">
  4. <i class="close_icon" @click="() => showBanner = false"></i>
  5. <p class="text">若当前浏览器无法扫码,请切换其他浏览器尝试</p>
  6. </div>
  7. <div class="cover">
  8. <p class="line"></p>
  9. <span class="square top left"></span>
  10. <span class="square top right"></span>
  11. <span class="square bottom right"></span>
  12. <span class="square bottom left"></span>
  13. <p class="tips">将二维码放入框内,即可自动扫描</p>
  14. </div>
  15. <video
  16. v-show="showPlay"
  17. class="source"
  18. ref="video"
  19. :width="videoWH.width"
  20. :height="videoWH.height"
  21. controls
  22. ></video>
  23. <canvas v-show="!showPlay" ref="canvas" />
  24. <button v-show="showPlay" @click="run">开始</button>
  25. </div>
  26. </template>
  27. <script>
  28. // eslint-disable-next-line no-unused-vars
  29. import adapter from 'webrtc-adapter';
  30. import jsQR from 'jsqr';
  31. export default {
  32. name: 'Scaner',
  33. props: {
  34. // 使用后置相机
  35. useBackCamera: {
  36. type: Boolean,
  37. default: true
  38. },
  39. // 扫描识别后停止
  40. stopOnScaned: {
  41. type: Boolean,
  42. default: true
  43. },
  44. drawOnfound: {
  45. type: Boolean,
  46. default: true
  47. },
  48. // 线条颜色
  49. lineColor: {
  50. type: String,
  51. default: '#03C03C'
  52. },
  53. // 线条宽度
  54. lineWidth: {
  55. type: Number,
  56. default: 2
  57. },
  58. // 视频宽度
  59. videoWidth: {
  60. type: Number,
  61. default: document.documentElement.clientWidth || document.body.clientWidth
  62. },
  63. // 视频高度
  64. videoHeight: {
  65. type: Number,
  66. default: document.documentElement.clientHeight - 48 || document.body.clientHeight - 48
  67. },
  68. responsive: {
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. data () {
  74. return {
  75. showPlay: false,
  76. showBanner: true,
  77. containerWidth: null,
  78. active: false
  79. }
  80. },
  81. computed: {
  82. videoWH () {
  83. if (this.containerWidth) {
  84. const width = this.containerWidth;
  85. const height = width * 0.75;
  86. return { width, height };
  87. }
  88. return { width: this.videoWidth, height: this.videoHeight };
  89. }
  90. },
  91. watch: {
  92. active: {
  93. immediate: true,
  94. handler(active) {
  95. if (!active) {
  96. this.fullStop();
  97. }
  98. }
  99. }
  100. },
  101. methods: {
  102. // 画线
  103. drawLine (begin, end) {
  104. this.canvas.beginPath();
  105. this.canvas.moveTo(begin.x, begin.y);
  106. this.canvas.lineTo(end.x, end.y);
  107. this.canvas.lineWidth = this.lineWidth;
  108. this.canvas.strokeStyle = this.lineColor;
  109. this.canvas.stroke();
  110. },
  111. // 画框
  112. drawBox (location) {
  113. if (this.drawOnfound) {
  114. this.drawLine(location.topLeftCorner, location.topRightCorner);
  115. this.drawLine(location.topRightCorner, location.bottomRightCorner);
  116. this.drawLine(location.bottomRightCorner, location.bottomLeftCorner);
  117. this.drawLine(location.bottomLeftCorner, location.topLeftCorner);
  118. }
  119. },
  120. tick () {
  121. if (this.$refs.video && this.$refs.video.readyState === this.$refs.video.HAVE_ENOUGH_DATA) {
  122. this.$refs.canvas.height = this.videoWH.height;
  123. this.$refs.canvas.width = this.videoWH.width;
  124. this.canvas.drawImage(this.$refs.video, 0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
  125. const imageData = this.canvas.getImageData(0, 0, this.$refs.canvas.width, this.$refs.canvas.height);
  126. let code = false;
  127. try {
  128. code = jsQR(imageData.data, imageData.width, imageData.height);
  129. } catch (e) {
  130. console.error(e);
  131. }
  132. if (code) {
  133. this.drawBox(code.location);
  134. this.found(code.data);
  135. }
  136. }
  137. this.run();
  138. },
  139. // 初始化
  140. setup () {
  141. if (this.responsive) {
  142. this.$nextTick(() => {
  143. this.containerWidth = this.$refs.scaner.clientWidth;
  144. });
  145. }
  146. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  147. this.previousCode = null;
  148. this.parity = 0;
  149. this.active = true;
  150. this.canvas = this.$refs.canvas.getContext("2d");
  151. const facingMode = this.useBackCamera ? { exact: 'environment' } : 'user';
  152. const handleSuccess = stream => {
  153. if (this.$refs.video.srcObject !== undefined) {
  154. this.$refs.video.srcObject = stream;
  155. } else if (window.videoEl.mozSrcObject !== undefined) {
  156. this.$refs.video.mozSrcObject = stream;
  157. } else if (window.URL.createObjectURL) {
  158. this.$refs.video.src = window.URL.createObjectURL(stream);
  159. } else if (window.webkitURL) {
  160. this.$refs.video.src = window.webkitURL.createObjectURL(stream);
  161. } else {
  162. this.$refs.video.src = stream;
  163. }
  164. this.$refs.video.playsInline = true;
  165. const playPromise = this.$refs.video.play();
  166. playPromise.catch(() => (this.showPlay = true));
  167. playPromise.then(this.run);
  168. };
  169. navigator.mediaDevices
  170. .getUserMedia({ video: { facingMode } })
  171. .then(handleSuccess)
  172. .catch(() => {
  173. navigator.mediaDevices
  174. .getUserMedia({ video: true })
  175. .then(handleSuccess)
  176. .catch(error => {
  177. this.$emit("error-captured", error);
  178. });
  179. });
  180. }
  181. },
  182. run () {
  183. if (this.active) {
  184. requestAnimationFrame(this.tick);
  185. }
  186. },
  187. found (code) {
  188. if (this.previousCode !== code) {
  189. this.previousCode = code;
  190. } else if (this.previousCode === code) {
  191. this.parity += 1;
  192. }
  193. if (this.parity > 2) {
  194. this.active = this.stopOnScanned ? false : true;
  195. this.parity = 0;
  196. this.$emit("code-scanned", code);
  197. }
  198. },
  199. // 完全停止
  200. fullStop () {
  201. if (this.$refs.video && this.$refs.video.srcObject) {
  202. this.$refs.video.srcObject.getTracks().forEach(t => t.stop());
  203. }
  204. }
  205. },
  206. mounted () {
  207. this.setup();
  208. },
  209. beforeDestroy () {
  210. this.fullStop();
  211. }
  212. }
  213. </script>
  214. <style lang="css" scoped>
  215. .scaner {
  216. background: #000000;
  217. position: fixed;
  218. top: 48PX;
  219. left: 0;
  220. width: 100%;
  221. height: 100%;
  222. height: -webkit-calc(100% - 48PX);
  223. height: -moz-calc(100% - 48PX);
  224. height: -ms-calc(100% - 48PX);
  225. height: -o-calc(100% - 48PX);
  226. height: calc(100% - 48PX);
  227. }
  228. .scaner .banner {
  229. width: 340PX;
  230. position: absolute;
  231. top: 16PX;
  232. left: 50%;
  233. margin-left: -170PX;
  234. background: #FA74A2;
  235. border-radius: 8PX;
  236. box-sizing: border-box;
  237. padding: 12PX;
  238. opacity: 0.9;
  239. box-shadow: 1PX 1PX 10PX rgba(0, 0, 0, 0.2);
  240. }
  241. .scaner .banner .text {
  242. padding: 0;
  243. margin: 0;
  244. color: #FFFFFF;
  245. font-size: 12PX;
  246. text-align: justify;
  247. text-align-last: left;
  248. }
  249. .scaner .banner .close_icon {
  250. display: inline-block;
  251. height: 24PX;
  252. width: 24PX;
  253. /*background: url('../assets/close.png') no-repeat center;*/
  254. background-size: auto 100%;
  255. position: absolute;
  256. right: 8PX;
  257. top: 8PX;
  258. }
  259. .scaner .cover {
  260. height: 220PX;
  261. width: 220PX;
  262. position: absolute;
  263. top:50%;
  264. left:50%;
  265. -webkit-transform: translate(-50%,-50%);
  266. -moz-transform: translate(-50%,-50%);
  267. -ms-transform: translate(-50%,-50%);
  268. -o-transform: translate(-50%,-50%);
  269. transform: translate(-50%,-50%);
  270. border: .5PX solid #999999;
  271. z-index: 1111;
  272. }
  273. .scaner .cover .line {
  274. width: 200PX;
  275. height: 1PX;
  276. margin-left: 10PX;
  277. background: #5F68E8;
  278. background: linear-gradient(to right, transparent, #5F68E8, #0165FF, #5F68E8, transparent);
  279. position: absolute;
  280. -webkit-animation: scan 1.75s infinite linear;
  281. -moz-animation: scan 1.75s infinite linear;
  282. -ms-animation: scan 1.75s infinite linear;
  283. -o-animation: scan 1.75s infinite linear;
  284. animation: scan 1.75s infinite linear;
  285. -webkit-animation-fill-mode: both;
  286. -moz-animation-fill-mode: both;
  287. -ms-animation-fill-mode: both;
  288. -o-animation-fill-mode: both;
  289. animation-fill-mode: both;
  290. border-radius: 1PX;
  291. }
  292. .scaner .cover .square {
  293. display: inline-block;
  294. height: 20PX;
  295. width: 20PX;
  296. position: absolute;
  297. }
  298. .scaner .cover .square.top {
  299. top: 0;
  300. border-top: 1PX solid #5F68E8;
  301. }
  302. .scaner .cover .square.left {
  303. left: 0;
  304. border-left: 1PX solid #5F68E8;
  305. }
  306. .scaner .cover .square.bottom {
  307. bottom: 0;
  308. border-bottom: 1PX solid #5F68E8;
  309. }
  310. .scaner .cover .square.right {
  311. right: 0;
  312. border-right: 1PX solid #5F68E8;
  313. }
  314. .scaner .cover .tips {
  315. position: absolute;
  316. bottom: -48PX;
  317. width: 100%;
  318. font-size: 14PX;
  319. color: #FFFFFF;
  320. opacity: 0.8;
  321. }
  322. @-webkit-keyframes scan {
  323. 0% {top: 0}
  324. 25% {top: 50PX}
  325. 50% {top: 100PX}
  326. 75% {top: 150PX}
  327. 100% {top: 200PX}
  328. }
  329. @-moz-keyframes scan {
  330. 0% {top: 0}
  331. 25% {top: 50PX}
  332. 50% {top: 100PX}
  333. 75% {top: 150PX}
  334. 100% {top: 200PX}
  335. }
  336. @-o-keyframes scan {
  337. 0% {top: 0}
  338. 25% {top: 50PX}
  339. 50% {top: 100PX}
  340. 75% {top: 150PX}
  341. 100% {top: 200PX}
  342. }
  343. @keyframes scan {
  344. 0% {top: 0}
  345. 25% {top: 50PX}
  346. 50% {top: 100PX}
  347. 75% {top: 150PX}
  348. 100% {top: 200PX}
  349. }
  350. </style>