Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vue-qrcode-reader

Package Overview
Dependencies
Maintainers
1
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-qrcode-reader

A Vue.js component, accessing the device camera and allowing users to read QR-Codes, within the browser

  • 0.8.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-15.97%
Maintainers
1
Weekly downloads
 
Created
Source

vue-qrcode-reader

vue2 gzip-size npm npm

A Vue.js component, accessing the device camera and allowing users to read QR codes, within the browser.

Demos

Browser support

Internet ExplorerEdgeFirefoxChromeSafari
NoYesYesYes11+
  • Chrome requires HTTPS or localhost.
  • on iOS, it doesn't (yet?) work in non-Safari browsers (see #29)
  • more details on Caniuse

Usage

decode event

Once a stream from the users camera is loaded, it is displayed and continuously scanned for QR codes. Results are indicated by the decode event.

<qrcode-reader @decode="onDecode"></qrcode-reader>
methods: {
  onDecode (decodedString) {
    // ...
  }
}

init event

It might take a while before the component is ready and the scanning process starts. The user has to be asked for camera access permission first and the camera stream has to be loaded.

If you want to show a loading indicator, you can listen for the init event. It's emitted as soon as the component is mounted and carries a promise which resolves when everything is ready. The promise is rejected if initialization fails. This can have a couple of reasons.

:point_right: In Chrome you can't prompt users for permissions a second time. Once denied, users can only manually grant them. Make sure your users understand why you need access to their camera before you mount this component. Otherwise they might panic and deny and then get frustrated because they don't know how to change their decision.

<qrcode-reader @init="onInit"></qrcode-reader>
methods: {
  async onInit (promise) {
    // show loading indicator

    try {
      await promise

      // successfully initialized
    } catch (error) {
      if (error.name === 'NotAllowedError') {
        // user denied camera access permisson
      } else if (error.name === 'NotFoundError') {
        // no suitable camera device installed
      } else if (error.name === 'NotSupportedError') {
        // page is not served over HTTPS (or localhost)
      } else if (error.name === 'NotReadableError') {
        // maybe camera is already in use
      } else if (error.name === 'OverconstrainedError') {
        // passed constraints don't match any camera. Did you requested the front camera although there is none?
      } else {
        // browser is probably lacking features (WebRTC, Canvas)
      }
    } finally {
      // hide loading indicator
    }
  }
}

track prop

By default detected QR codes are visually highlighted. A transparent canvas overlays the camera stream. When a QR code is detected, its location is painted to the canvas. You can enable/disable this feature by passing true/false via the track prop.

You can also pass a function to customize the way the location is painted. This function is called to produce each frame. It receives the location object as the first argument and a CanvasRenderingContext2D instance as the second argument.

:point_right: Avoid access to reactive properties in this function (like stuff in data, computed or your Vuex store). The function is called several times a second and might cause memory leaks. If you want to be save don't access this at all.

Say you want to paint in a different color that better fits your overall page theme.

<qrcode-reader :track="repaintLocation"></qrcode-reader>
methods: {
  repaintLocation (location, ctx) {
    if (location !== null) {
      const {
        topLeftCorner,
        topRightCorner,
        bottomLeftCorner,
        bottomRightCorner,
      } = location

      ctx.strokeStyle = 'blue' // instead of red

      ctx.beginPath()
      ctx.moveTo(topLeftCorner.x, topRightCorner.y)
      ctx.lineTo(bottomLeftCorner.x, bottomLeftCorner.y)
      ctx.lineTo(bottomRightCorner.x, bottomRightCorner.y)
      ctx.lineTo(topRightCorner.x, topRightCorner.y)
      ctx.lineTo(topLeftCorner.x, topLeftCorner.y)
      ctx.closePath()

      ctx.stroke()
    }
  }
}

Distributed content

Distributed content will overlay the camera stream, wrapped in a position: absolute container.

<qrcode-reader>
  <b>stuff here overlays the camera stream</b>
</qrcode-reader>

paused prop

With the paused prop you can prevent further decode propagation and functions passed via track are stopped being called. Useful for example if you're only interested in the first result. This will also freeze the camera stream.

<qrcode-reader @decode="onDecode" :paused="paused"></qrcode-reader>
data () {
  return {
    paused: false
  }
},

methods: {
  onDecode (content) {
    this.paused = true
    // ...
  }
}

video-constraints prop

This component uses getUserMedia to request camera streams. This method accepts a constraints object to configure for example if front or rear camera should be accessed. This is passed by default:

{
  audio: false, // don't request microphone access
  video: {
    facingMode: { ideal: 'environment' }, // use rear camera if available
    width: { min: 360, ideal: 1280, max: 1920 }, // constrain video width resolution
    height: { min: 240, ideal: 720, max: 1080 } // constrain video height resolution
  }
}

You can change the video part using the video-constraints prop. Note that you only have to pass properties you want to override. If you want to use the front camera for example and change nothing else, pass this:

<qrcode-reader :video-constraints="{ facingMode: 'user' }"></qrcode-reader>

:point_right: If you change this property after initialization, a new camera stream will be requested and the init event will be emitted again.

Installation

yarn add vue-qrcode-reader

or using NPM:

npm install --save vue-qrcode-reader

Default import

Register component globally:

import Vue from 'vue'
import VueQrcodeReader from 'vue-qrcode-reader'

Vue.use(VueQrcodeReader)

Register locally in other components scope:

import Vue from 'vue'
import { QrcodeReader } from 'vue-qrcode-reader'

Vue.component('my-component', {
  components: { QrcodeReader },

  // ...
)

⚠️ A css file is included when importing the package. You may have to setup your bundler to embed the css in your page.

Distribution import

Register component globally:

import 'vue-qrcode-reader/dist/vue-qrcode-reader.css'
import VueQrcodeReader from 'vue-qrcode-reader/dist/vue-qrcode-reader.common'

Vue.use(VueQrcodeReader)

Register locally in other components scope:

import 'vue-qrcode-reader/dist/vue-qrcode-reader.css'
import { QrcodeReader } from 'vue-qrcode-reader/dist/vue-qrcode-reader.common'

Vue.component('my-component', {
  components: { QrcodeReader },

  // ...
)

⚠️ You may have to setup your bundler to embed the css file in your page.

Browser

<link rel="stylesheet" href="vue-qrcode-reader/dist/vue-qrcode-reader.css"/>

<script src="vue.js"></script>
<script src="vue-qrcode-reader/dist/vue-qrcode-reader.browser.js"></script>

The plugin should be auto-installed. If not, you can install it manually with the instructions below.

Register component globally:

Vue.use(VueQrcodeReader)

Register locally in other components scope:

Vue.component('my-component', {
  components: {
    'qrcode-reader': VueQrcodeReader.QrcodeReader
  },

  // ...
)

Source import

Register component globally:

import Vue from 'vue'
import VueQrcodeReader from 'vue-qrcode-reader/src'

Vue.use(VueQrcodeReader)

Register locally in other components scope:

import Vue from 'vue'
import { QrcodeReader } from 'vue-qrcode-reader/src'

Vue.component('my-component', {
  components: { QrcodeReader },

  // ...
)

⚠️ You need to configure your bundler to compile .vue files. More info in the official documentation.


License

MIT

Keywords

FAQs

Package last updated on 14 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc