C# 调用摄像头解析二维码

上次的二维码项目中,在解析的时候用的是扫码枪自动解析并以键盘形式输出,二次开发及通用性不高。本次项目使用AForge库调用摄像头采集二维码照片,再使用ZXing库解析二维码,较之前通用性更高。
在这里插入图片描述

一、调用摄像头

1.首先安装AForge库
在这里插入图片描述

2.在工具箱中将视频组件添加到winfrom界面界面中
在这里插入图片描述

3.代码如下所示,增加了一个comobox摄像头列表来加载不同的摄像头。

public partial class Form1 : Form
{
    FilterInfoCollection videoDevices;//摄像头设备集合
    VideoCaptureDevice videoSource;//捕获设备源
    const int delaytime = 500; //扫码间隔时间 ms
    int totalTime; //扫码最长时间

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        button1_Click(null, null);
    }

    // 获取视频输入设备
    private void button1_Click(object sender, EventArgs e)
    {
        comboBox1.Items.Clear();
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取拍照设备列表
        for (int i = 0; i < videoDevices.Count; i++)
            comboBox1.Items.Add(videoDevices[i].Name);
        comboBox1.Text = comboBox1.Items[0].ToString();
    }
    
	// 开始识读
    private void button2_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == null)
            return;
        ShutCamera();
        videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
        videoSourcePlayer1.VideoSource = videoSource;
        videoSourcePlayer1.Start();
        textBox1.Text = "开始识读";

        timer1.Interval = delaytime;
        timer1.Start();
        totalTime = 0;
    }

    // 停止识读
    private void button3_Click(object sender, EventArgs e)
    {
        ShutCamera();
        timer1.Stop();
    }

    // 关闭并释放摄像头
    public void ShutCamera()
    {
        if (videoSourcePlayer1.VideoSource != null)
        {
            videoSourcePlayer1.SignalToStop();
            videoSourcePlayer1.WaitForStop();
            videoSourcePlayer1.VideoSource = null;
        }
    }
}

二、解析二维码

为了方便快捷,二维码解析依然使用ZXing库提供的解析函数。主要的想法是新建一个timer,在timer中利用摄像头每隔一段时间采集一张图片,将图片导入到ZXing解析函数中,如果识别成功就停止timer。代码如下:

private void timer1_Tick(object sender, EventArgs e)
{
    if (totalTime >= 20000)// 最多读20秒
    {
        timer1.Stop();
        ShutCamera();
        textBox1.Text = "未识别到二维码";
        return;
    }

    Bitmap picture;
    picture = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄

    if (picture != null)
    {
        // 识读QR码
        BarcodeReader reader = new BarcodeReader();
        reader.Options.CharacterSet = "UTF-8";
        Result resultQR = reader.Decode(picture);
        if (resultQR != null)
        {
            textBox1.Text = "QR码:\r\n";
            string[] strList = resultQR.Text.Split(',');
            for (int i = 0; i < strList.Length; i++)
                textBox1.AppendText(i.ToString("d2") + ":" + strList[i] + "\r\n");
            timer1.Stop();
            ShutCamera();
            return;
        }
    }
    totalTime += delaytime;
    textBox1.Text = totalTime.ToString() + " ms";
    //if (totalTime == 3000)
    //    picture.Save("测试图片.bmp");
}
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C# 中调用手机摄像头并解析二维码,你可以使用第三方库 ZXing.Net.Mobile。ZXing.Net.Mobile 是一个基于 ZXing 库的跨平台二维码扫描库,它支持在多个移动平台上使用。 下面是一个示例代码,展示了如何在 C# 中使用 ZXing.Net.Mobile 来调用手机摄像头并解析二维码: ```csharp using Xamarin.Forms; using ZXing.Mobile; using ZXing.Net.Mobile.Forms; public partial class MainPage : ContentPage { ZXingScannerPage scanPage; public MainPage() { InitializeComponent(); } private async void ScanButton_Clicked(object sender, EventArgs e) { var options = new MobileBarcodeScanningOptions { PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.QR_CODE } }; scanPage = new ZXingScannerPage(options); scanPage.OnScanResult += (result) => { // 停止扫描 scanPage.IsScanning = false; // 处理扫描结果 Device.BeginInvokeOnMainThread(async () => { await Navigation.PopAsync(); await DisplayAlert("Scanned Barcode", result.Text, "OK"); }); }; // 显示扫描页面 await Navigation.PushAsync(scanPage); } } ``` 在上面的示例中,我们首先创建了一个按钮 `ScanButton`,当点击按钮时,会调用 `ScanButton_Clicked` 方法。在该方法中,我们创建一个 `ZXingScannerPage` 实例,并设置扫描的参数。然后,我们订阅了 `OnScanResult` 事件,当扫描到二维码时,会触发该事件,并在其中处理扫描结果。 最后,我们使用导航将扫描页面显示出来。 请确保在项目中安装了 ZXing.Net.Mobile 库,并在代码中引用了相应的命名空间。 通过以上代码,你可以在 C# 中调用手机摄像头并解析二维码。 希望这对你有帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值