DEV Community

Cover image for Simple qr/barcode scanning with svelte and Html5Qrcode
myleftshoe
myleftshoe

Posted on

14 6 1 1

Simple qr/barcode scanning with svelte and Html5Qrcode

Ever wanted to scan qr and barcodes using your mobile but didn't want to write a full blown native app just to get camera access?

Use your mobile browser! Html5 now supports it.

Open the demo in your mobile browser - chrome, safari, firefox all worked for me!

Honestly, I couldn't believe how easy it was! Check out the code:

<script>
    import { Html5Qrcode } from 'html5-qrcode'
    import { onMount } from 'svelte'

    let scanning = false

    let html5Qrcode

    onMount(init)

    function init() {
        html5Qrcode = new Html5Qrcode('reader')
    }

    function start() {
        html5Qrcode.start(
            { facingMode: 'environment' },
            {
                fps: 10,
                qrbox: { width: 250, height: 250 },
            },
            onScanSuccess,
            onScanFailure
        )
        scanning = true
    }

    async function stop() {
        await html5Qrcode.stop()
        scanning = false
    }

    function onScanSuccess(decodedText, decodedResult) {
        alert(`Code matched = ${decodedText}`)
        console.log(decodedResult)
    }

    function onScanFailure(error) {
        console.warn(`Code scan error = ${error}`)
    }
</script>

<style>
    main {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 20px;
    }
    reader {
        width: 100%;
        min-height: 500px;
        background-color: black;
    }
</style>

<main>
    <reader id="reader"/>
    {#if scanning}
        <button on:click={stop}>stop</button>
    {:else}
        <button on:click={start}>start</button>
    {/if}
</main>
Enter fullscreen mode Exit fullscreen mode

Special thanks to the author of the wonderful Html5Qrcode lib.

Honorable mention to quagga. No longer actively maintained, but there is an active fork quagga2.

asaf! thanks for reading🥰

[update]: the demo is not a pwa, but the scanner lib and camera access will work if you decide to make a pwa for your own project.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (2)

Collapse
 
julie0000 profile image
julie0000 •

Thanks for saving my day, superhero!

Collapse
 
myleftshoe profile image
myleftshoe •

Thanks for the thanks! :D

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay