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

Detailed tutorial on installing MySQL database on Alibaba Cloud Server

Table of contents Preface 1. Uninstall MySQL 2. I...

Detailed description of common events and methods of html text

Event Description onactivate: Fired when the objec...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...

How to hide elements on the Web and their advantages and disadvantages

Example source code: https://codepen.io/shadeed/p...

Use dockercompose to build springboot-mysql-nginx application

In the previous article, we used Docker to build ...

MySQL deep paging (how to quickly paginate tens of millions of data)

Table of contents Preface Case optimization summa...

Vue virtual Dom to real Dom conversion

There is another tree structure Javascript object...

Four practical tips for JavaScript string operations

Table of contents Preface 1. Split a string 2. JS...

MySql8 WITH RECURSIVE recursive query parent-child collection method

background When developing a feature similar to c...

IE8 uses multi-compatibility mode to display web pages normally

IE8 will have multiple compatibility modes . IE pl...

Mysql queries the transactions being executed and how to wait for locks

Use navicat to test and learn: First use set auto...