html5-qrcode前端打开摄像头扫描二维码功能

实现的效果如图所示,全屏打开并且扫描到二维码后弹窗提醒,主要就是使用html5-qrcode这个依赖库,html5-qrcode开源地址:GitHub - mebjas/html5-qrcode: A cross platform HTML5 QR code reader. See end to end implementation at: https://scanapp.org

使用文档:Getting started | ScanApp

安装依赖:

复制代码
pnpm install --save-dev html5-qrcode

弹窗提示我用的vant这个ui库,开源地址:GitHub - youzan/vant: A lightweight, customizable Vue UI library for mobile web apps.

然后在项目中使用,我使用的是vue:

javascript 复制代码
<template>
    <div class="scanCode">
        <div id="reader"></div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { showToast } from 'vant'
import { Html5Qrcode } from 'html5-qrcode'

const codeContent = ref('1024')

// This method will trigger user permissions
Html5Qrcode.getCameras()
    .then((devices) => {
        // console.log(devices)
        const html5QrCode = new Html5Qrcode('reader')
        const width = window.innerWidth
        const height = window.innerHeight
        const aspectRatio = width / height
        const reverseAspectRatio = height / width

        const mobileAspectRatio =
            reverseAspectRatio > 1.5
                ? reverseAspectRatio + (reverseAspectRatio * 12) / 100
                : reverseAspectRatio
        if (devices && devices.length) {
            // .. use this to start scanning.
            html5QrCode
                .start(
                    { facingMode: { exact: 'environment' } },
                    {
                        fps: 10, // Optional, frame per seconds for qr code scanning
                        aspectRatio: aspectRatio + 1,
                        qrbox: { width: 250, height: 250 }, // Optional, if you want bounded box UI
                        videoConstraints: {
                            facingMode: 'environment',
                            aspectRatio:
                                width < 600 ? mobileAspectRatio : aspectRatio,
                        },
                    },
                    (decodedText, decodedResult) => {
                        // do something when code is read
                        console.log('decodedText', decodedText)
                        // console.log('decodedResult', decodedResult)
                        codeContent.value = decodedText
                        showToast(decodedText)
                    },
                    (errorMessage) => {
                        // parse error, ignore it.
                        // console.log('parse error, ignore it.', errorMessage)
                    }
                )
                .catch((err) => {
                    // Start failed, handle it.
                    console.log('Start failed, handle it.')
                })
        }
    })
    .catch((err) => {
        // handle err
        console.log(err)
    })
</script>

<style lang="scss" scoped>
.scanCode {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: rgba(0, 0, 0);

    #reader {
        top: 50%;
        left: 0;
        transform: translateY(-50%);
    }
}
</style>
相关推荐
spring29979212 分钟前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
木斯佳18 分钟前
前端八股文面经大全:正泰电气前端实习一面(2026-04-19)·面经深度解析
前端·面试·笔试·校招·面经
用户693717500138424 分钟前
你每天用的 AI,可能真的被“投毒”了
前端·后端·ai编程
吴声子夜歌26 分钟前
Vue3——Vuex状态管理
前端·vue.js·vue·es6
qq_120840937127 分钟前
Three.js 工程向:Frustum Culling 与场景分块优化实战
前端·javascript
漫游的渔夫32 分钟前
从 Fetch 到 RAG:为什么你的 AI 知识库总是“胡言乱语”?
前端·人工智能
Amos_Web34 分钟前
谷歌浏览器插件Brower-Books: 把整个浏览器变成你的云端书架
前端·chrome·产品
豹哥学前端38 分钟前
前端快速上手保姆级教程day5: 响应式布局
前端·响应式设计
ZC跨境爬虫41 分钟前
3D地球卫星轨道可视化平台开发 Day14(彻底移除多余阴影)
前端·javascript·3d·信息可视化·json