Vue implements QR code scanning function (with style)

Vue implements QR code scanning function (with style)

need:
Use vue to realize QR code scanning;

Plugins:
QRCodeReader;

Plugin Download

npm install --save vue-qrcode-reader

Notice:
The camera can only be called under the https protocol to scan the code.
You can configure devServer in vue.config.js: {https: true}
Or refer to the previous article to use the Nuxt framework on the front end, configure local https access and configure local certificates

insert image description here

<template>
  <div>
    <p class="error">{{ error }}</p>
    <!--Error message-->

    <p class="decode-result">
      Scan Results:
      {{ result }}
    </p>
    <!--Scan Results-->

    <qrcode-stream @decode="onDecode" @init="onInit" style="height: 100vh;">
      <div>
        <div class="qr-scanner">
          <div class="box">
            <div class="line"></div>
            <div class="angle"></div>
          </div>
        </div>
      </div>
    </qrcode-stream>
  </div>
</template>

<script>
// Download the plugin // cnpm install --save vue-qrcode-reader

// Import import { QrcodeStream } from 'vue-qrcode-reader'

export default {
  // Register components: { QrcodeStream },

  data() {
    return {
      result: '', // Scan result information error: '' // Error message }
  },

  methods: {
    onDecode(result) {
      alert(result)
      this.result = result
    },

    async onInit(promise) {
      try {
        await promise
      } catch (error) {
        if (error.name === 'NotAllowedError') {
          this.error = 'ERROR: You need to grant camera access'
        } else if (error.name === 'NotFoundError') {
          this.error = 'ERROR: There is no camera on this device'
        } else if (error.name === 'NotSupportedError') {
          this.error = 'ERROR: Security context required (HTTPS, localhost)'
        } else if (error.name === 'NotReadableError') {
          this.error = 'ERROR: The camera is occupied'
        } else if (error.name === 'OverconstrainedError') {
          this.error = 'ERROR: Camera installation is inappropriate'
        } else if (error.name === 'StreamApiNotSupportedError') {
          this.error = 'ERROR: This browser does not support the streaming API'
        }
      }
    }
  }
}
</script>

<style scoped>
.error {
  font-weight: bold;
  color: red;
}
</style>

<style scoped>
    /* * {
      margin: 0;
      padding: 0;
    }
    body {
      height: 700px;
      margin: 0;
    } */

    .qr-scanner {
      background-image:
        linear-gradient(0deg,
          transparent 24%,
          rgba(32, 255, 77, 0.1) 25%,
          rgba(32, 255, 77, 0.1) 26%,
          transparent 27%,
          transparent 74%,
          rgba(32, 255, 77, 0.1) 75%,
          rgba(32, 255, 77, 0.1) 76%,
          transparent 77%,
          transparent),
        linear-gradient(90deg,
          transparent 24%,
          rgba(32, 255, 77, 0.1) 25%,
          rgba(32, 255, 77, 0.1) 26%,
          transparent 27%,
          transparent 74%,
          rgba(32, 255, 77, 0.1) 75%,
          rgba(32, 255, 77, 0.1) 76%,
          transparent 77%,
          transparent);
      background-size: 3rem 3rem;
      background-position: -1rem -1rem;
      width: 100%;
      /* height: 100%; */
      height: 100vh;
      position: relative;
      background-color: #1110;

      /* background-color: #111; */
    }

    .qr-scanner .box {
      width: 213px;
      height: 213px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      overflow: hidden;
      border: 0.1rem solid rgba(0, 255, 51, 0.2);
      /* background: url('http://resource.beige.world/imgs/gongconghao.png') no-repeat center center; */
    }

    .qr-scanner .line {
      height: calc(100% - 2px);
      width: 100%;
      background: linear-gradient(180deg, rgba(0, 255, 51, 0) 43%, #00ff33 211%);
      border-bottom: 3px solid #00ff33;
      transform: translateY(-100%);
      animation: radar-beam 2s infinite alternate;
      animation-timing-function: cubic-bezier(0.53, 0, 0.43, 0.99);
      animation-delay: 1.4s;
    }

    .qr-scanner .box:after,
    .qr-scanner .box:before,
    .qr-scanner .angle:after,
    .qr-scanner .angle:before {
      content: '';
      display: block;
      position: absolute;
      width: 3vw;
      height: 3vw;

      border: 0.2rem solid transparent;
    }

    .qr-scanner .box:after,
    .qr-scanner .box:before {
      top: 0;
      border-top-color: #00ff33;
    }

    .qr-scanner .angle:after,
    .qr-scanner .angle:before {
      bottom: 0;
      border-bottom-color: #00ff33;
    }

    .qr-scanner .box:before,
    .qr-scanner .angle:before {
      left: 0;
      border-left-color: #00ff33;
    }

    .qr-scanner .box:after,
    .qr-scanner .angle:after {
      right: 0;
      border-right-color: #00ff33;
    }

    @keyframes radar-beam {
      0% {
        transform: translateY(-100%);
      }

      100% {
        transform: translateY(0);
      }
    }
  </style>

OK, let's implement the code scanning function in a code vue project

Project address: https://github.com/wkl007/vue-scan-demo.git
The project is mainly a code scanning function. The core code is

 <div class="scan">
    <div id="bcid">
      <div style="height:40%"></div>
      <p class="tip">...Loading...</p>
    </div>
    <footer>
      <button @click="startRecognize">1. Create a control</button>
      <button @click="startScan">2. Start scanning</button>
      <button @click="cancelScan">3. End scanning</button>

      <button @click="closeScan">4. Close the control</button>
    </footer>
  </div>
</template>

<script type='text/ecmascript-6'>
  let scan = null

  export default {
    data () {
      return {
        codeUrl: '',
      }
    },
    methods: {
      // Create a scanning control startRecognize () {
        let that = this
        if (!window.plus) return
        scan = new plus.barcode.Barcode('bcid')
        scan.onmarked = onmarked

        function onmarked (type, result, file) {
          switch (type) {
            case plus.barcode.QR:
              type = 'QR'
              break
            case plus.barcode.EAN13:
              type = 'EAN13'
              break
            case plus.barcode.EAN8:
              type = 'EAN8'
              break
            default:
              type = 'Other' + type
              break
          }
          result = result.replace(/\n/g, '')
          that.codeUrl = result
          alert(result)
          that.closeScan()

        }
      },
      // Start scanning startScan () {
        if (!window.plus) return
        scan.start()
      },
      // Close the scan cancelScan () {
        if (!window.plus) return
        scan.cancel()
      },
      // Close the barcode recognition control closeScan () {
        if (!window.plus) return
        scan.close()
      },
    }
  }
</script>
<style lang="less">
  .scan {
    height: 100%;

    #bcid {
      width: 100%;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 3rem;
      text-align: center;
      color: #fff;
      background: #ccc;
    }

    footer {
      position: absolute;
      left: 0;
      bottom: 1rem;
      height: 2rem;
      line-height: 2rem;
      z-index: 2;
    }
  }
</style>

This is the end of this article about Vue's implementation of the code scanning function with styles. For more relevant Vue code scanning function content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implement QR code scanning function through Vue
  • Vue - Example of front-end generation of QR code
  • How to dynamically generate QR codes in Vue
  • Vue WeChat scans QR code but Apple can only save pictures (solution)
  • How to use canvas in Vue to realize the synthesis of QR code and picture poster
  • In-depth understanding of Vue's method of generating QR codes using vue-qr

<<:  Briefly understand the two common methods of creating files in Linux terminal

>>:  Baota Linux panel command list

Recommend

How to use custom images in Html to display checkboxes

If you need to use an image to implement the use ...

HTML fixed title column, title header table specific implementation code

Copy code The code is as follows: <!DOCTYPE ht...

Use Javascript to develop sliding-nav navigation plug-in with sliding bar effect

Table of contents 1. Introduction 2. Usage 3. Dev...

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

Complete steps to configure a static IP address for a Linux virtual machine

Preface In many cases, we will use virtual machin...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

Solution to nginx-ingress-controller log persistence solution

Recently I saw an article on a public account tha...

Apache Calcite code for dialect conversion

definition Calcite can unify Sql by parsing Sql i...

A detailed introduction to the Linux directory structure

When you first start learning Linux, you first ne...

Web skills: Multiple IE versions coexistence solution IETester

My recommendation Solution for coexistence of mul...

WeChat applet uses canvas to draw clocks

This article shares the specific code of using ca...