need help on button to switch on webcam for C# bas

2019-08-23 10:48发布

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

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;

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 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)
    {

    }
}

}

the class for cameradevice

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

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

thanks a million

1条回答
相关推荐>>
2楼-- · 2019-08-23 11:34

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.

查看更多
登录 后发表回答