简体   繁体   English

AForge.NET:未触发 NewFrameEventHandler 参数方法

[英]AForge.NET: NewFrameEventHandler argument method not triggered

I am attempting to write a Windows Console program that is capable of acquiring imagery from a webcam or a virtual webcam, extracting a bitmap frame from the video source and saving the frame to the filesystem as a bitmap file.我正在尝试编写一个 Windows 控制台程序,该程序能够从网络摄像头或虚拟网络摄像头获取图像,从视频源中提取位图帧并将该帧作为位图文件保存到文件系统中。

Currently I use a program called OBS Studio to emulate a video source with the "Virtual Camera" option.目前我使用一个名为 OBS Studio 的程序来模拟带有“虚拟相机”选项的视频源。

Here is the code:这是代码:

using System;
using AForge.Video.DirectShow;
using AForge.Video;
using System.Drawing;

namespace webcamc
{
    class Program
    {
        FilterInfoCollection filterInfoCollection;
        VideoCaptureDevice videoCaptureDevice;
        Bitmap bitmap;

        static void Main(string[] args)
        {
            var program = new Program();
            program._init();
        }

        void _handleNewFrameEvent(object sender, NewFrameEventArgs eventArgs)
        {
            bitmap = new Bitmap(eventArgs.Frame);
        }

        void _init()
        {
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[0].MonikerString);
            videoCaptureDevice.Start();
            videoCaptureDevice.NewFrame += new NewFrameEventHandler(_handleNewFrameEvent);
           
            string ActiveDir = AppDomain.CurrentDomain.BaseDirectory;
            string filepath = System.IO.Path.Combine(ActiveDir, @"C://Pic");

            string fileName = System.IO.Path.Combine(filepath, @"name.bmp");
            bool fileNotExists = !System.IO.File.Exists(fileName);
            if (fileNotExists)
            {
               bitmap.Save(fileName);
            }

        }

    }
}

Unfortunately, I am unable to understand why the _handleNewFrameEvent method that I pass to the NewFrameEventHandler is not triggered;不幸的是,我无法理解为什么我传递给NewFrameEventHandler_handleNewFrameEvent方法没有被触发; i have inspected the code with a debugger and the breakpoint is not hit.我已经用调试器检查了代码,但没有命中断点。 I am quite sure that the device list and the device moniker is acquired correctly.我很确定正确获取了设备列表和设备名称。 However I doubt very much the videoCaptureDevice instance.但是我非常怀疑videoCaptureDevice实例。 I am not sure but it looks like it is not correctly instantiated since all the pertaining values appear to be 0.我不确定,但看起来它没有正确实例化,因为所有相关值似乎都是 0。

Here is an image of the debugger showing the ill-instantiated video capture device这是调试器的图像,显​​示了错误实例化的视频捕获设备

Could anyone please point me in the right direction?任何人都可以指出我正确的方向吗?

Thank you.谢谢你。

I would first move videoCaptureDevice.Start();我会先移动videoCaptureDevice.Start(); below videoCaptureDevice.NewFrame += new NewFrameEventHandler(_handleNewFrameEvent);下面videoCaptureDevice.NewFrame += new NewFrameEventHandler(_handleNewFrameEvent); as you only want to start running the camera feed once you can receive the new frames (but I am not confident that this will solve your problem).因为您只想在收到新帧后开始运行相机提要(但我不相信这会解决您的问题)。

Maybe there is an error being thrown by the starting of the webcam so adding a vDevIn.VideoSourceError += new VideoSourceErrorEventHandler(errorHandler) and see if there is any error thrown using function below:也许网络摄像头的启动会引发错误,因此添加vDevIn.VideoSourceError += new VideoSourceErrorEventHandler(errorHandler)并使用以下函数查看是否存在任何错误:

    private void errorHandler(object sender, VideoSourceErrorEventArgs eventArgs)
{
    Console.WriteLine("Video feed source error: " + eventArgs.Description);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM