Lightweight QR Code Scanner

1.4.2 · active · verified Tue Apr 21

qr-scanner is a JavaScript library designed for efficient QR code scanning from both continuous video streams (webcams) and static images. Currently at version 1.4.2, it appears to be actively maintained, supported by Nimiq, and offers significant performance and accuracy advantages over older libraries like LazarSoft/jsqrcode, boasting a 2-8x higher detection rate and fewer misreads according to benchmarks. Key differentiators include its lightweight footprint (as low as 5.6 kB gzipped when `BarcodeDetector` is available), automatic utilization of the browser's native `BarcodeDetector` API for optimal performance, and its design to run in a WebWorker to keep the main UI thread responsive. The library is built upon Cosmo Wolfe's JavaScript port of Google's ZXing library, with several improvements geared towards modern web environments and optimized for colored QR codes.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up `qr-scanner` to read QR codes from a live webcam feed in a browser environment, displaying the result on the page. It highlights the essential worker script configuration.

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QR Scanner Demo</title>
</head>
<body>
    <h1>Scan QR Code</h1>
    <video id="qr-video" style="width: 100%; max-width: 400px;"></video>
    <div id="qr-result"></div>

    <script type="module">
        import QrScanner from 'qr-scanner'; // Adjust path if not using a bundler

        // You need to copy qr-scanner-worker.min.js to a public path or configure your bundler
        // If qr-scanner-worker.min.js is in the same directory as this script:
        QrScanner.workerScript = './qr-scanner-worker.min.js';
        // If using npm and a bundler, it might be resolved automatically or you might need to copy it.
        // If deployed to a 'dist' folder, and worker is in 'dist/':
        // QrScanner.workerScript = '/dist/qr-scanner-worker.min.js';

        const videoElement = document.getElementById('qr-video');
        const resultElement = document.getElementById('qr-result');

        const qrScanner = new QrScanner(
            videoElement,
            result => {
                resultElement.textContent = `QR Code detected: ${result.data}`;
                console.log('QR Code detected:', result.data);
                // To stop scanning after first detection:
                // qrScanner.stop();
            },
            { 
                /* your options or pass nothing */ 
                highlightScanRegion: true,
                highlightCodeOutline: true,
                returnDetailedScanResult: true
            }
        );

        // Start scanning immediately when the page loads
        qrScanner.start().then(() => {
            console.log('QR Scanner started');
        }).catch(err => {
            console.error('Failed to start QR Scanner:', err);
            resultElement.textContent = `Error starting scanner: ${err.message || err}`;
        });

        // Optional: Stop scanner when component unmounts or page navigates
        // window.addEventListener('beforeunload', () => {
        //     qrScanner.stop();
        // });

    </script>
</body>
</html>

view raw JSON →