qr-scanner

repository·master·Indexed 25 days ago

https://github.com/nimiq/qr-scanner

A lightweight, high-performance JavaScript QR scanner library (v1.4.2) that supports web cam video streams and single image scanning. It utilizes the browser's native BarcodeDetector API when available and runs in a WebWorker to maintain UI responsiveness. Features include camera management (listCameras, setCamera), flashlight control, custom scan regions, and support for various image sources including HTMLImageElement, File/Blob, and Data URIs.

Tokens
2.9K
Snippets
11
Records
27
Agent score
34%

What's inside qr-scanner

  1. Use QrScanner with UMD build for non-module scripts

    master

    For projects that are not based on ES6 modules, you can use the UMD build qr-scanner.umd.min.js via a standard script tag.

    <script src="path/to/qr-scanner.umd.min.js"></script>
    <script>
        // do something with QrScanner
    </script>
  2. Import QrScanner using dynamic import

    master

    If your project does not use ES6 modules natively, you can load the library using a dynamic import() call.

    import('path/to/qr-scanner.min.js').then((module) => {
        const QrScanner = module.default;
        // do something with QrScanner
    });
  3. Use the legacy build for older browser support

    master
    The standard build uses ECMAScript 2017 features like async. To support older browsers, use qr-scanner.legacy.min.js. This is an ES6 (ES2015) compatible UMD build that includes polyfills and inlines the worker script to support browsers that lack dynamic import capabilities.
  4. Import QrScanner as an ES6 module

    master

    If you are using a module bundler (like Webpack or Rollup) or plain ES6 modules, import QrScanner directly. Note that qr-scanner.min.js is an ES6 module and requires the importing script to be a module (e.g., <script type="module">).

    import QrScanner from 'path/to/qr-scanner.min.js'; // if using plain es6 import
    import QrScanner from 'qr-scanner'; // if installed via package and bundling with a module bundler like webpack or rollup
  5. Perform Web Cam Scanning

    master

    To scan QR codes using a live camera stream, follow these steps:

    1. Create an HTML <video> element to render the stream:

      <video></video>
    2. Initialize a QrScanner instance. To use the modern API (which returns detailed scan results), you must provide an options object. If you omit the options object, the scanner uses a deprecated API that returns only a simple string.

    3. Start scanning by calling .start(). Note that your page must be served via HTTPS to access the camera.

    4. Stop scanning by calling .stop() if you wish to pause the stream.

    5. Clean up by calling .destroy() when the scanner is no longer needed to stop the camera stream and web worker.

  6. Use QrScanner with CommonJS (require)

    master

    If you are using a require-based bundler like Browserify, you can import the library using standard CommonJS syntax.

    const QrScanner = require('qr-scanner'); // if installed via package
    const QrScanner = require('path/to/qr-scanner.umd.min.js'); // if not installed via package
    // do something with QrScanner
  7. Build the qr-scanner project from source

    master

    If you need to modify the source code in the /src folder, you can build the project yourself. This requires Node.js. The project provides prebuilt files qr-scanner.min.js and qr-scanner-worker.min.js for standard use, but manual building is necessary for custom development.

    yarn
    yarn build
  8. Configure worker script location for non-bundlers

    master
    The library consists of two files: qr-scanner.min.js (the API) and qr-scanner-worker.min.js (the worker). If you are not using a bundler that handles dynamic imports automatically, you must ensure qr-scanner-worker.min.js is placed in your distribution folder next to qr-scanner.min.js or next to the script that imports it.
  9. Scan a Single Image

    master

    Use QrScanner.scanImage(image, [options]) to scan a static image source. This method returns a Promise that resolves with the scan result or throws if no QR code is found.

    Supported Sources: HTMLImageElement, SVGImageElement, HTMLVideoElement, HTMLCanvasElement, ImageBitmap, OffscreenCanvas, File/Blob, Data URIs, or URLs (if same-origin or CORS enabled).

    QrScanner.scanImage(image)
        .then(result => console.log(result))
        .catch(error => console.log(error || 'No QR code found.'));
  10. Control Camera Flashlight

    master

    On supported browsers and devices, you can control the camera's flashlight.

    Note: It is recommended to call hasFlash() after the scanner has successfully started to avoid unnecessary permission prompts.

  11. Manage Available Cameras

    master

    The library provides utility methods to check for camera availability and list specific devices.

    • Check availability: Use QrScanner.hasCamera() to see if a camera exists.
    • List cameras: Use QrScanner.listCameras() to get an array of cameras with id and label.
    • Request labels: Pass true to listCameras(true) to request camera labels (this may prompt the user for permission).
    • Switch camera: Use qrScanner.setCamera(facingModeOrDeviceId) to change the active camera using a device ID or a facing mode ('user' or 'environment').