当前位置:   article > 正文

H5端扫码识别二维码 VUE(开箱即用)_h5扫码获取内容

h5扫码获取内容

公司有这个需要本来想CV,找了很久没找到,都是些残文和AI文章。就干脆自己写了一个。

CV就能用,开箱即用兄弟们,觉得好用的点个赞吧兄弟们。

目前只做了移动端的适配哦,用电脑打开是打不开的做了强制限制的。

不罗嗦,上代码

需要安装JSQR去解析二维码

控制台命令: npm install jsqr --save

  1. <template>
  2. <div>
  3. <div v-if="xsyc">
  4. <div ref="videocontainer" v-if="xsycerweima">
  5. <div class="video-container">
  6. <!--video用于显示媒体设备的视频流,自动播放 autoplay-->
  7. <video id="video" autoplay playsinline webkit-playsinline ref="video" ></video>
  8. <!--描绘video截图-->
  9. <canvas id="canvas" style="width: 100%;height: 100%;" v-show="videofalse"></canvas>
  10. <!-- 遮罩层 -->
  11. <div class="wrap" ref="wrap">
  12. <!-- 扫码线 -->
  13. <!-- 直角线 -->
  14. <i class="iconfont icnfontzhijiao1" >&#xe603;</i>
  15. <i class="iconfont icnfontzhijiao2">&#xe601;</i>
  16. <i class="iconfont icnfontzhijiao3">&#xe604;</i>
  17. <i class="iconfont icnfontzhijiao4">&#xe602;</i>
  18. <div class="saomasan">
  19. </div>
  20. </div>
  21. <div ref="shengyupx" class="shengyupx">
  22. <div class="dibubuju">
  23. <i class="iconfont">&#xe600;</i>
  24. <div>
  25. 扫描二维码
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. <div v-else>
  32. 识别到你的二维码信息是: {{ codedata }}
  33. </div>
  34. </div>
  35. <div v-else>
  36. <div class="PCduan">
  37. 扫描二维码暂不支持电脑端,请移步到手机端。
  38. </div>
  39. </div>
  40. </div>
  41. </template>
  1. <script>
  2. import jsQR from 'jsqr'
  3. export default {
  4. name: 'LoginIndex',
  5. data () {
  6. return {
  7. videofales: false,
  8. video: null,
  9. canvas: null,
  10. ctx: null,
  11. xsyc: true,
  12. xsycerweima: true,
  13. codedata: '',
  14. intervalId: null
  15. }
  16. },
  17. components: {
  18. },
  19. created () {
  20. const system = {}
  21. system.pingtai = /(Win32|Win16|WinCE|Mac68K|MacIntel|MacIntel|MacPPC|Linux mips64)/i.test(navigator.platform)
  22. if (system.pingtai) {
  23. // 电脑
  24. console.log('电脑')
  25. this.xsyc = false
  26. } else {
  27. // 手机
  28. console.log('手机')
  29. this.xsyc = true
  30. }
  31. },
  32. mounted () {
  33. // 获取DOM元素
  34. if (this.xsyc) {
  35. this.video = document.getElementById('video')
  36. this.canvas = document.getElementById('canvas')
  37. this.canvas.width = window.innerWidth
  38. this.canvas.height = window.innerHeight
  39. this.ctx = this.canvas.getContext('2d')
  40. console.log(window)
  41. this.scan()
  42. this.intervalId = setInterval(() => {
  43. this.capture()
  44. }, 100)
  45. }
  46. },
  47. methods: {
  48. // 兼容浏览器获取用户媒体方法
  49. // constrains 参数用于指定要使用的摄像头和音频设备。success 和 error 参数用于处理成功和失败的情况。
  50. getUserMedia (constrains, success, error) {
  51. if (navigator.mediaDevices.getUserMedia) {
  52. // 最新标准API
  53. navigator.mediaDevices.getUserMedia(constrains).then(success).catch(error)
  54. } else if (navigator.webkitGetUserMedia) {
  55. // webkit内核浏览器
  56. navigator.webkitGetUserMedia(constrains).then(success).catch(error)
  57. } else if (navigator.mozGetUserMedia) {
  58. // Firefox浏览器
  59. navigator.mozGetUserMedia(constrains).then(success).catch(error)
  60. } else if (navigator.getUserMedia) {
  61. // 旧版API
  62. navigator.getUserMedia(constrains).then(success).catch(error)
  63. }
  64. },
  65. success (stream) {
  66. // 将一个视频流(stream)转换为一个可以被视频元素加载的URL,然后将这个URL设置为视频元素的源。这样,浏览器就可以自动播放这个视频流了。
  67. // 兼容webkit核心浏览器
  68. // 将视频流设置为video元素的源
  69. this.video.srcObject = stream
  70. this.video.play()
  71. },
  72. error (error) {
  73. console.log('访问用户媒体设备失败:' + error.name + ' ' + error.message)
  74. },
  75. // 调用摄像头
  76. scan () {
  77. if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
  78. // 调用用户媒体设备,访问摄像头
  79. this.getUserMedia({
  80. audio: false,
  81. video: {
  82. facingMode: 'environment', // user 前置摄像头 environment 后置摄像头
  83. width: { ideal: 1280 },
  84. height: { ideal: 720 },
  85. frameRate: { ideal: 30 },
  86. codec: 'video/mp4;codecs="avc1.42E01E"'
  87. }
  88. }, this.success, this.error)
  89. } else {
  90. alert('你的浏览器不支持访问用户媒体设备')
  91. }
  92. },
  93. // 循环这个动作就可以实现扫码的效果
  94. capture () {
  95. const videoWidth = window.innerWidth
  96. const videoHeight = window.innerHeight
  97. this.ctx.drawImage(this.video, 0, 0, videoWidth, videoHeight)
  98. // 截取高亮部位图片
  99. const cavaswidth = this.canvas.width
  100. const cavasheight = this.canvas.height
  101. const centerX = cavaswidth / 2
  102. const centerY = cavasheight / 2
  103. // 这里是扫描区域的控制 和 this.$refs.wrap.offsetWidth的大小一样 调整wrap的宽度可以调整扫描区域
  104. const desiredSquareSize = this.$refs.wrap.offsetWidth
  105. const squareSize = desiredSquareSize
  106. const x = centerX - squareSize / 2
  107. const y = centerY - squareSize / 2
  108. // 需要识别的二维码部分给jsQR去解析
  109. const imageData = this.ctx.getImageData(x, y, squareSize, squareSize)
  110. const code = jsQR(imageData.data, squareSize, squareSize)
  111. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
  112. if (code) {
  113. console.log('找到二维码', code.data)
  114. this.codedata = code.data
  115. this.xsycerweima = false
  116. clearInterval(this.intervalId)
  117. this.video.stop()
  118. } else {
  119. console.log('未找到二维码')
  120. }
  121. }
  122. }
  123. }
  124. </script>
  1. <style scoped>
  2. @font-face {
  3. font-family: 'iconfont'; /* Project id 4504064 */
  4. src: url('//at.alicdn.com/t/c/font_4504064_wfyvfsvop7d.woff2?t=1712828573992') format('woff2'),
  5. url('//at.alicdn.com/t/c/font_4504064_wfyvfsvop7d.woff?t=1712828573992') format('woff'),
  6. url('//at.alicdn.com/t/c/font_4504064_wfyvfsvop7d.ttf?t=1712828573992') format('truetype');
  7. }
  8. .iconfont{
  9. font-family:"iconfont" !important;
  10. font-size:25px;font-style:normal;
  11. -webkit-font-smoothing: antialiased;
  12. -webkit-text-stroke-width: 0.2px;
  13. -moz-osx-font-smoothing: grayscale;
  14. }
  15. *{
  16. margin:0px; padding:0px;
  17. overflow: hidden;
  18. }
  19. .video-container {
  20. /* width: 100vw;
  21. height: 100vh; */
  22. overflow: hidden;
  23. }
  24. #video {
  25. /* object-fit: cover; */
  26. /* object-position: center center; */
  27. /* object-position: ; */
  28. /* height: 100%; */
  29. width: 100%;
  30. object-fit: contain;
  31. position: absolute;
  32. overflow: hidden;
  33. }
  34. #canvas {
  35. width: 100vw;
  36. height: 100vh;
  37. }
  38. #capture {
  39. content:'';
  40. position: absolute;
  41. top: 0;
  42. right: 0;
  43. }
  44. .wrap {
  45. content:'';
  46. position: absolute;
  47. width: 70vw;
  48. height: 70vw;
  49. left: 50%;
  50. top: 50%;
  51. transform: translate(-50%,-50%); /*默认居中*/
  52. box-shadow: 0 0 0 999vw rgba(0, 0, 0, .8); /*足够大的投影*/
  53. }
  54. .shengyupx {
  55. position: absolute;
  56. background-color:rgba(44, 40, 40, 1);
  57. bottom: -1vh;
  58. height: 10vh;
  59. width: 100%;
  60. z-index: 999;
  61. overflow: hidden;
  62. }
  63. .dibubuju {
  64. display: flex;
  65. flex-direction: column;
  66. justify-content: center;
  67. align-items: center;
  68. color: #007bff;
  69. font-size: 2vw;
  70. }
  71. .icnfontzhijiao1 {
  72. content:'';
  73. position: absolute;
  74. left: 0.4vw;
  75. top: -0.4vh;
  76. color: #007bff;
  77. }
  78. .icnfontzhijiao2 {
  79. content:'';
  80. position: absolute;
  81. right: 0.4vw;
  82. top: -0.4vh;
  83. color: #007bff;
  84. }
  85. .icnfontzhijiao3 {
  86. content:'';
  87. position: absolute;
  88. left: 0.4vw;
  89. bottom: -0.7vh;
  90. color: #007bff;
  91. }
  92. .icnfontzhijiao4 {
  93. content:'';
  94. position: absolute;
  95. right: 0.4vw;
  96. bottom: -0.7vh;
  97. color: #007bff;
  98. }
  99. .saomasan {
  100. content:'';
  101. position: absolute;
  102. left: 50%;
  103. top: 0%;
  104. transform: translate(-50%,-50%); /*默认居中*/
  105. height: 3px;
  106. width: 300px;
  107. background: -webkit-linear-gradient(left,rgba(255, 255, 255, 0),#007bff,rgba(255,255,255,0));
  108. background: linear-gradient(to right, rgba(255, 255, 255, 0),#007bff,rgba(255,255,255,0));
  109. animation: moveLine 3s linear infinite;
  110. }
  111. @keyframes moveLine {
  112. 0% { transform: translate(-50%,-50%) translateY(-300%); }
  113. 50% { transform: translate(-50%,-50%) translateY(10000%); }
  114. 100% { transform: translate(-50%,-50%) translateY(-100%); }
  115. }
  116. .PCduan {
  117. display: flex;
  118. justify-content: center;
  119. align-items: center;
  120. height: 100vh;
  121. font-size: 4vw;
  122. }
  123. </style>

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

闽ICP备14008679号