uniapp, vue, h5 页面通过zxing.js实现扫描二维码或条形码

1、引入zxing-js

在uniapp中使用npm i @zxing/library --save这个方式引入依赖的话会存在报错 require is not defined,所以这里选择直接引入js文件;
js地址:地址
js引入

2、注意video标签

注意uniapp中的video标签是经过二次封装的,以至于在decodeFromVideoDevice方法中输入video标签中的id会一直报错 element with id'video' must be an video element(id为“video”的元素必须是视频元素),原因在于uniapp的video标签是二次封装组件
video标签
操作DOM

3、具体实现

listVideoInputDevices方法中的res代表视频通道,比如手机有前后两个摄像头,这里的res[1]代表的就是默认打开后置摄像头, res[0]为前置摄像头,res[1]为后置摄像头,大多数都是使用后置摄像头:
具体实现
结果
注意:该插件必须基于https才能调起来摄像功能。

完整代码

<template>
    <view class="content">
        <video id="video-canvas" autoplay :controls="false"></video>
    </view>
</template>
 
<script>
    import {
        BrowserMultiFormatReader
    } from '@/utils/zxing.js'
    export default {
        data() {
            return {
                codeReader: null,
                deviceId: null,
                videoInputDevice: []
            }
        },
        onLoad() {
            this.initEvent();
        },
        mounted() {
            const video = document.getElementById('video-canvas').getElementsByTagName('video')[0]
            video.setAttribute('id', 'video_id')
            video.setAttribute('class', 'video_class')
        },
        methods: {
            initEvent() {
                this.codeReader = new BrowserMultiFormatReader();
                this.codeReader.listVideoInputDevices().then(res => {
                    if (res.length) {
                        this.videoInputDevice = res
                        this.deviceId = res[1].deviceId
                    }
                    this.scanCode();
                }).catch((err) => {
                    uni.showModal({
                        title: '提示',
                        content: '当前浏览器环境不支持获取视频通道',
                        showCancel: false
                    });
                })
            },
            scanCode() {
                try {
                    this.codeReader.decodeFromVideoDevice(this.deviceId, 'video_id', (res, err) => {
                        if (res) {
                            uni.showModal({
                                title: '扫码结果',
                                content: JSON.stringify(res)
                            })
                        }
                        if (err) {
                            uni.showModal({
                                title: '扫码失败结果',
                                content: JSON.stringify(err)
                            })
                        }
                    })
                } catch (e) {
                    uni.showToast({
                        title: `初始化失败${e}`,
                        icon: 'none'
                    })
                }
            }
        }
    }
</script>
  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个调用摄像头扫描条形码并在文本框中显示的WinForm应用程序的完整代码和所需组件: 1. 在项目中添加一个PictureBox控件和一个TextBox控件。 2. 在NuGet包管理器中搜索并安装ZXing.Net包。这是一个用于解码条形码的开源库。 3. 将以下代码添加到Form1.cs文件中: ```csharp using System; using System.Drawing; using System.Windows.Forms; using ZXing; namespace BarcodeScanner { public partial class Form1 : Form { private readonly Webcam webcam; public Form1() { InitializeComponent(); // 初始化摄像头 webcam = new Webcam(pictureBox1); webcam.OnCaptured += Webcam_OnCaptured; } private void Form1_Load(object sender, EventArgs e) { // 启动摄像头 webcam.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // 停用摄像头 webcam.Stop(); } private void Webcam_OnCaptured(object source, WebcamEventArgs e) { // 解码条形码 var barcodeReader = new BarcodeReader(); var result = barcodeReader.Decode((Bitmap)e.Frame.Clone()); if (result != null) { // 在文本框中显示条形码内容 textBox1.Invoke(new Action(() => textBox1.Text = result.Text)); } } } } ``` 4. 添加一个名为“Webcam”的新类: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace BarcodeScanner { public class Webcam { private readonly PictureBox pictureBox; private readonly Timer timer; public event EventHandler<WebcamEventArgs> OnCaptured; public Webcam(PictureBox pictureBox) { this.pictureBox = pictureBox; this.timer = new Timer(); this.timer.Tick += Timer_Tick; this.timer.Interval = 100; } public void Start() { var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count == 0) { throw new Exception("No webcam found!"); } var device = devices[0]; device.VideoResolution = device.SupportedVideoResolutions[0]; device.Start(); timer.Start(); } public void Stop() { timer.Stop(); var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count > 0) { devices[0].Stop(); } } private void Timer_Tick(object sender, EventArgs e) { var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count > 0) { var device = devices[0]; var frame = device.GetCurrentFrame(); pictureBox.Image = frame; OnCaptured?.Invoke(this, new WebcamEventArgs(frame)); } } } public class WebcamEventArgs : EventArgs { public Bitmap Frame { get; } public WebcamEventArgs(Bitmap frame) { Frame = frame; } } } ``` 5. 运行应用程序,将摄像头对准条形码扫描条形码内容将显示在文本框中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值