首页 > 解决方案 > 通过 C# + EmguCV 拍摄网络摄像头照片不起作用

问题描述

通过 C# + EmguCV 拍摄网络摄像头照片是行不通的。EmguCV 版本 3.1.0.1(由于 Visual Studio 2015、.NET Framework 4.5.2)。操作系统 Windows 10。我的代码(可以肯定的是 dispose() 太多):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using ZXing;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Capture capture;
        Bitmap image;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (capture != null)
                {
                    capture.QueryFrame().Dispose();
                    capture.Dispose();
                }

                if (image != null)
                {
                    image.Dispose();
                }

                capture = new Capture();
                image = capture.QueryFrame().Bitmap;
                image.Save(Application.StartupPath + "\\img.jpg");
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                if (capture != null)
                {
                    capture.QueryFrame().Dispose();
                    capture.Dispose();
                }

                if (image != null)
                {
                    image.Dispose();
                }
            }
        }
    }
}

奇怪的是,这段代码只工作一次,我得到了一张照片。但是,该应用程序会冻结所有后续尝试,而不会出现任何异常消息。重新启动计算机后,该应用程序仅再次正常工作一次。通过这种行为,似乎某些操作系统资源在代码执行后没有被释放。

标签: c#winformswebcamemgucv

解决方案


我用@sunside 的建议修改了我的代码,它可以工作了!在询问 stackoverflow 之前,我尝试了许多修改,包括不处理,但它们没有工作。我不知道哪里出错了。工作代码在这里:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using ZXing;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        Capture capture;
        Mat mat;
        Bitmap image;

        public Form1()
        {
            InitializeComponent();
            capture = new Capture();
            this.FormClosing += Form1_FormClosing;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (image != null) image.Dispose();
                if (mat != null) mat.Dispose();

                mat = capture.QueryFrame();
                image = mat.Bitmap;
                image.Save(Application.StartupPath + "\\img.jpg");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (image != null) image.Dispose();
                if (mat != null) mat.Dispose();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(capture != null) capture.Dispose();
        }
    }
}


推荐阅读