网络摄像头未显示在图片框中

时间:2021-03-22 13:52:15

标签: c# winforms

我正在尝试使用设备网络摄像头扫描二维码并正确显示内容,但是它能够成功运行,但网络摄像头没有显示。

下面是outpage页面的截图,其中应该显示网络摄像头的图片框是空的。 enter image description here

我的整个窗体的代码如下:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        FilterInfoCollection filterInfoCollection;
        VideoCaptureDevice captureDevice;

        private void Form1_Load(object sender, EventArgs e)
        {
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo filterInfo in filterInfoCollection)
                cboDevice.Items.Add(filterInfo.Name);
            cboDevice.SelectedIndex = 0;
        }
    
    private void btnRead_Click(object sender, EventArgs e)
    {
        captureDevice = new VideoCaptureDevice(filterInfoCollection[cboDevice.SelectedIndex].MonikerString);
        captureDevice.NewFrame += CaptureDevice_NewFrame;
        captureDevice.Start();
        timer1.Start();
    }

    private void CaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {

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

        //throw new NotImplementedException(); 
    }

    private void UserControl1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (captureDevice.IsRunning)
            captureDevice.Stop();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (pictureBox1.Image != null)
        {
            BarcodeReader barcodeReader = new BarcodeReader();
            Result result = barcodeReader.Decode((Bitmap)pictureBox1.Image);
            if (result != null)
            {
                txtContent.Text = result.ToString();
                timer1.Stop();
                if (captureDevice.IsRunning)
                    captureDevice.Stop();
            }
        }
    }
}
}

我不确定我的代码现在是否有错误,或者是否需要从我的设备授予任何权限,因为我已经授予了对此文件的完全读写访问权限。

0 个答案:

没有答案
相关问题