QrcodeStream

Events

decode

  • Payload Type: String

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

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

TIP

If you scan the same QR code multiple times in a row, decode is still only emitted once. When you hold a QR code in the camera, frames are actually decoded multiple times a second but you don't want to be flooded with decode events that often. That's why the last decoded QR code is always cached and only new results are propagated. However changing the value of camera resets this internal cache.

detect

  • Payload Type: Promise<Object>

The detect event is basically a verbose version of decode. decode only gives you the string encoded by QR codes. detect on the other hand ...

  • is always emitted before decode
  • gives you the unprocessed raw image data
  • gives you the coordinates of the QR code in the camera frame
  • does NOT silently fail in case of errors
<qrcode-stream @detect="onDetect"></qrcode-stream>
methods: {
  async onDetect (promise) {
    try {
      const {
        imageData,    // raw image data of image/frame
        content,      // decoded String
        location      // QR code coordinates
      } = await promise

      // ...
    } catch (error) {
      // ...
    }
  }
}

init

  • Payload Type: Promise<void>

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.

WARNING

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-stream @init="onInit"></qrcode-stream>
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') {
        // did you requested the front camera although there is none?
      } else if (error.name === 'StreamApiNotSupportedError') {
        // browser seems to be lacking features
      }
    } finally {
      // hide loading indicator
    }
  }
}

Props

track

  • Input Type: Boolean, Function
  • Default: true

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. If tracking is disabled the camera stream is scanned much less frequently. So if you encounter performance problems on your target device, this might help.

You can also pass a function with track 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.

WARNING

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. To be safe don't access this at all.

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

<qrcode-stream :track="repaint"></qrcode-stream>
methods: {
  repaint (location, ctx) {
    const {
      topLeftCorner,
      topRightCorner,
      bottomLeftCorner,
      bottomRightCorner,
      // topLeftFinderPattern,
      // topRightFinderPattern,
      // bottomLeftFinderPattern
    } = location

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

    ctx.beginPath()
    ctx.moveTo(topLeftCorner.x, topLeftCorner.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()
  }
}

camera

  • Input Type: String
  • Default: auto
  • Valid Inputs: auto, rear, front, off

With the camera prop you can control which camera to access on the users device.

  • Use front or rear to force request the front or rear camera respectively.
  • If you choose auto the rear camera is requested by default. But if a device like a laptop has only a front camera installed, auto will fallback to that.
  • Use off to not request a camera at all or in other words: turn the camera off.

Every time the camera prop is modified, a new camera stream is requested so the init event is emitted again. That way you can catch errors. For example when the front camera is requested on a device that doesn't have one.

<qrcode-stream :camera="camera" @init="onCameraChange"></qrcode-stream>
data () {
  return {
    camera: 'auto'
  }
},

methods: {
  startFrontCamera () {
    this.camera = 'front'
  },

  onCameraChange (promise) {
    promise.catch(error => {
      const cameraMissingError = error.name === 'OverconstrainedError'
      const triedFrontCamera = this.camera === 'front'

      if (triedFrontCamera && cameraMissingError) {
        // no front camera on this device
      }
    })
  }
}

Slots

default

Any distributed content overlays the camera stream, wrapped in a position: absolute container.

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