c# 二维码识别 摄像头使用 windows

使用AForge 和 ZXing   nuget 全安上了,这个还要灰度处理,代码中没有涉及

 

usinnugetg AForge.Video;
using AForge.Video.DirectShow;
using ZXing;

using ZXing.Windows.Compatibility;//重要,这个得引入

load

public partial class Form1 : Form
    {

        private static NewFrameEventHandler videoSource_NewFrame;
        FilterInfoCollection videoDevices;
        VideoCaptureDevice videoSource;
        int selectedDeviceID = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count == 0)
            {
                MessageBox.Show("灭有摄像头"); ;
            }

            else
            {

                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);


                }

                //初始化第一个
                comboBox1.SelectedIndex = 0;
                //videoSource.DesiredFrameSize = new Size(640, 480);
                // videoSource.DesiredFrameRate = 1;
                videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceID].MonikerString);
                videoSource.NewFrame += new NewFrameEventHandler(NewFrameEventHandler);
                videoSource.Start();
                Thread.Sleep(2000);

            }
   void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
            {
                Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
            }
             void NewFrameEventHandler(object sender, NewFrameEventArgs eventArgs)
            {

                pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();

            }

这样 combobox里面就有摄像头了,再加上切换

 void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {

                selectedDeviceID = this.comboBox1.SelectedIndex;
                //videoSource.DesiredFrameSize = new Size(640, 480);
                // videoSource.DesiredFrameRate = 1;
                videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceID].MonikerString);
                videoSource.NewFrame += new NewFrameEventHandler(NewFrameEventHandler);

                videoSource.Start();
                Thread.Sleep(2000);
                timer1.Enabled = true;
            }

解码的代码

private void DecodeBarcode()
        {
            



            var reader = new BarcodeReader();
            
                if (pictureBox2.Image != null)
                {
                    var result = reader.Decode((Bitmap)pictureBox2.Image);
                if (result != null)
                {

                    System.Console.WriteLine(result);
                    label1.Text = result + " ";
                    timer1.Enabled =false;

                    // Invoke(new Action<Result>(ShowResult), result);
                }
                else
                {

                    label1.Text =  " 没事别出来";

                }

                pictureBox1.Image = null;
                }
                Thread.Sleep(200);
           
            
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值