zxing-js/library

View on GitHub
docs/examples/multi-image/index.html

Summary

Maintainability
Test Coverage
<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="author" content="ZXing for JS">

    <title>ZXing TypeScript | Decoding from image file</title>

    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="https://unpkg.com/normalize.css@8.0.0/normalize.css">
    <link rel="stylesheet" rel="preload" as="style" onload="this.rel='stylesheet';this.onload=null" href="https://unpkg.com/milligram@1.3.0/dist/milligram.min.css">
</head>

<body>

    <main class="wrapper" style="padding-top:2em">

        <section class="container" id="demo-content">
            <h1 class="title">Scan 1D/2D Code from Image</h1>

            <p>
                <a class="button-small button-outline" href="../../index.html">HOME 🏡</a>
            </p>

            <p>
                This example shows how to scan any supported 1D/2D code with ZXing javascript library from an image.
                The example decodes from the
                <code>src</code> in
                <code>img</code> tag, however is also possible to decode directly from an url without an
                <code>img</code> tag.
            </p>

            <div>
                <a class="button" id="decodeButton">Decode</a>
            </div>

            <div>
                <img id="img" src="../../resources/blackbox/qrcode-3/01.png" style="border: 1px solid gray"></img>
            </div>

            <label>Result:</label>
            <pre><code id="result"></code></pre>

            <p>See the <a href="https://github.com/zxing-js/library/tree/master/docs/examples/multi-image/">source code</a> for this example.</p>

        </section>

        <footer class="footer">
            <section class="container">
                <p>ZXing TypeScript Demo. Licensed under the <a target="_blank" href="https://github.com/zxing-js/library#license" title="MIT">MIT</a>.</p>
            </section>
        </footer>

    </main>

    <script type="text/javascript" src="https://unpkg.com/@zxing/library@latest"></script>
    <script type="text/javascript">
        window.addEventListener('load', function () {
            const codeReader = new ZXing.BrowserMultiFormatReader()
            console.log('ZXing code reader initialized')

            document.getElementById('decodeButton').addEventListener('click', () => {
                const img = document.getElementById('img')
                codeReader.decodeFromImage(img).then((result) => {
                    console.log(result)
                    document.getElementById('result').textContent = result.text
                }).catch((err) => {
                    console.error(err)
                    document.getElementById('result').textContent = err
                })
                console.log(`Started decode for image from ${img.src}`)
            })

        })
    </script>

</body>

</html>