需要有关按钮的帮助以打开基于 C# 的二维码项目的网络摄像头 [英] need help on button to switch on webcam for C# based QR code project

查看:29
本文介绍了需要有关按钮的帮助以打开基于 C# 的二维码项目的网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习二维码网络摄像头解码器.我从 https://zxingnet.svn.codeplex.com/举了一个例子svn/trunk/Clients/AForgeDemo/ 并成功构建它而没有错误.但是,当我在连接网络摄像头的情况下运行它时,没有输入或它不会打开网络摄像头.根据我的理解,当用户在组合框中选择它时,网络摄像头将打开.好吧,由于构建时没有错误,我无法确定出了什么问题.我还看了一个项目,当用户按下按钮时会打开网络摄像头,我计划将它实施到当前项目中.我已经插入了按钮,但我不知道我应该在按钮上编程什么来打开网络摄像头,而不必在组合框中选择

i am currently learning on QR code webcam decoder. i have taken an example from https://zxingnet.svn.codeplex.com/svn/trunk/Clients/AForgeDemo/ and have succesfully build it without error. however when i run it with my webcam connected, no input or it wont switch on the webcam. based on my understanding, the webcam would be switch on when user select it at combobox. well, since there is no error at build, i cant pinpoint what went wrong. i have also taken a look at a project which switch on the webcam when user press a button and i plan to implement it to the current project. i have already insert the button but i do not know what should i program at the button to switch on the webcam instead on having to choose at combobox

有人会建议或指导我完成它.下面是主程序,还有2个类

would someone kindly advise or guide me through it. below is the main program, and 2 class

    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;
    using ZXing;
    using System.Threading;

命名空间 AForgeDemo{公共部分类 AForgeDemoForm : 表单{

namespace AForgeDemo { public partial class AForgeDemoForm : Form {

    private struct Device
    {
        public int Index;
        public string Name;
        public override string ToString()
        {
            return Name;
        }
    }

    private readonly CameraDevices camDevices;
    private Bitmap currentBitmapForDecoding;
    private readonly Thread decodingThread;
    private Result currentResult;
    private readonly Pen resultRectPen;

    public AForgeDemoForm()
    {
        InitializeComponent();
        camDevices = new CameraDevices();

        decodingThread = new Thread(DecodeBarcode);
        decodingThread.Start();

        pictureBox1.Paint += pictureBox1_Paint;
        resultRectPen = new Pen(Color.Green, 10);
    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
  {
     if (currentResult == null)
        return;

     if (currentResult.ResultPoints != null && currentResult.ResultPoints.Length > 0)
     {
        var resultPoints = currentResult.ResultPoints;
        var rect = new Rectangle((int)resultPoints[0].X, (int)resultPoints[0].Y, 1, 1);
        foreach (var point in resultPoints)
        {
           if (point.X < rect.Left)
              rect = new Rectangle((int)point.X, rect.Y, rect.Width + rect.X -            (int)point.X, rect.Height);
           if (point.X > rect.Right)
              rect = new Rectangle(rect.X, rect.Y, rect.Width + (int)point.X - rect.X, rect.Height);
           if (point.Y < rect.Top)
              rect = new Rectangle(rect.X, (int)point.Y, rect.Width, rect.Height + rect.Y - (int)point.Y);
           if (point.Y > rect.Bottom)
              rect = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height + (int)point.Y - rect.Y);
        }
        using (var g = pictureBox1.CreateGraphics())
        {
           g.DrawRectangle(resultRectPen, rect);
        }
}

}protected override void OnLoad(EventArgs e){base.OnLoad(e);LoadDevicesToCombobox();}

} protected override void OnLoad(EventArgs e) { base.OnLoad(e); LoadDevicesToCombobox(); }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        base.OnClosing(e);
        if (!e.Cancel)
        {
            decodingThread.Abort();
            if (camDevices.Current != null)
            {
                camDevices.Current.NewFrame -= Current_NewFrame;
                if (camDevices.Current.IsRunning)
                {
                    camDevices.Current.SignalToStop();
                }
            }
        }
    }

    private void LoadDevicesToCombobox()
    {
        cmbDevice.Items.Clear();
        for (var index = 0; index < camDevices.Devices.Count; index++)
        {
            cmbDevice.Items.Add(new Device { Index = index, Name = camDevices.Devices[index].Name });
        }
    }

    private void cmbDevice_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (camDevices.Current != null)
        {
            camDevices.Current.NewFrame -= Current_NewFrame;
            if (camDevices.Current.IsRunning)
            {
                camDevices.Current.SignalToStop();
            }
        }

        camDevices.SelectCamera(((Device)(cmbDevice.SelectedItem)).Index);
        camDevices.Current.NewFrame += Current_NewFrame;
        camDevices.Current.Start();
    }

    private void Current_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        if (IsDisposed)
        {
            return;
        }

        try
        {
            if (currentBitmapForDecoding == null)
            {
                currentBitmapForDecoding = (Bitmap)eventArgs.Frame.Clone();
            }
            Invoke(new Action<Bitmap>(ShowFrame), eventArgs.Frame.Clone());
        }
        catch (ObjectDisposedException)
        {
            // not sure, why....
        }
    }

    private void ShowFrame(Bitmap frame)
    {
        if (pictureBox1.Width < frame.Width)
        {
            pictureBox1.Width = frame.Width;
        }
        if (pictureBox1.Height < frame.Height)
        {
            pictureBox1.Height = frame.Height;
        }
        pictureBox1.Image = frame;
    }

    private void DecodeBarcode()
    {
        var reader = new BarcodeReader();
        while (true)
        {
            if (currentBitmapForDecoding != null)
            {
                var result = reader.Decode(currentBitmapForDecoding);
                if (result != null)
                {
                    Invoke(new Action<Result>(ShowResult), result);
                }
                currentBitmapForDecoding.Dispose();
                currentBitmapForDecoding = null;
            }
            Thread.Sleep(200);
        }
    }

    private void ShowResult(Result result)
    {
        currentResult = result;
        txtBarcodeFormat.Text = result.BarcodeFormat.ToString();
        txtContent.Text = result.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
}

}

cameradevice 类

the class for cameradevice

  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using AForge.Video.DirectShow;

命名空间 AForgeDemo{

namespace AForgeDemo {

    internal class CameraDevices
    {
        public FilterInfoCollection Devices { get; private set; }
        public VideoCaptureDevice Current { get; private set; }

        public CameraDevices()
        {
            Devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        }

        public void SelectCamera(int index)
        {
            if (index >= Devices.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            Current = new VideoCaptureDevice(Devices[index].MonikerString);
        }
    }
}

再次,我恳请任何人帮助我将在按钮命令中放置的内容直接激活网络摄像头,而不是从组合框中选择

again, i kindly ask for anybody help on what i shall put at the button command to activate the webcam directly instead of choosing from combobox

感谢一百万

推荐答案

按钮背后的事件处理程序所需的代码类似于 cmbDevice_SelectedIndexChanged 方法中的代码.我认为它应该如下所示

The code which you need for your event handler behind the button is similar to the code within the method cmbDevice_SelectedIndexChanged. I think it should look like the following

     // select the first available camera and start capturing
     camDevices.SelectCamera(0);
     camDevices.Current.NewFrame += Current_NewFrame;
     camDevices.Current.Start();

但我认为主要的挑战是找出原始示例无法按预期工作的原因.在组合框的处理程序或按钮的处理程序中调用相同的代码没有区别.

But I think the main challenge is to find out why the original example doesn't work as expected. It makes no difference if the same code is called in the handler of the combobox or in the handler of the button.

这篇关于需要有关按钮的帮助以打开基于 C# 的二维码项目的网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆