C# 调用摄像头实时识别二维码

注释和winform控件自己做,源码需要下载。

// 功能:调用zxing生成和解析二维码,调用摄像头根据图片识别二维 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using AForge.Video;
using com.google.zxing.common;
using com.google.zxing;

namespace CameraQRCode
{
   
    public partial class FrmCamera : Form
    {
   
        #region 变量定义
        ///调用AForge使用摄像头
        FilterInfoCollection Use_Webcams = null;
        VideoCaptureDevice cam = null;

        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        #endregion

        public FrmCamera()
        {
   
            InitializeComponent();
        }

        #region 事件

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCamera_Load(object sender, EventArgs e)
        {
   
            ///初始化AForge内容
            Use_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (Use_Webcams.Count > 0)
            {
   
                //实例化对象
                cam = new VideoCaptureDevice(Use_Webcams[0].MonikerString);

                //绑定事件
                cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
            }

        }

        /// <summary>
        /// 生成二维码并保存图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQRCode_Click(object sender
  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
要在 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# 中调用手机摄像头并解析二维码。 希望这对你有帮助!如果你还有其他问题,请随时提问。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值