使用 DirectShow.NET 从网络摄像头捕获帧

发布于 2024-12-09 11:35:27 字数 93 浏览 2 评论 0原文

我是 DirectShow 的新手,所以这个库的某些部分我不太理解。 我已经看到示例 DxSnap,但我需要捕获帧而不预览,以进行进一步处理。我该怎么做?

I am new at DirectShow, so some parts of this library i don't understand well.
I already see the example DxSnap, but i need to capture frames without previewing it, for futher processing. How can i do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉睡月亮 2024-12-16 11:35:27

如果您主要关心的是“访问网络摄像头”而不是“使用 DirectShow 访问网络摄像头”,那么我会看看 AForge.NET-Framework 。我用 DirectShow 尝试过一次,只是为了发现我可以用更少的代码在更短的时间内对多个视频源做同样的事情。

以下是一些示例代码:使用 DirectShow 访问 USB 摄像头和视频文件

If your main concern is "access webcam" and not "access webcam with DirectShow", then I would have a look at the AForge.NET-Framework. I tried it with DirectShow once just to find out that I could do the same thing with multiple video sources in less time with less code.

Here is some sample code: Access to USB cameras and video files using DirectShow

栖竹 2024-12-16 11:35:27

你可以自己建造一个。如果您查看 windows sdk 7.0~ 文件夹,您可以转到示例 >多媒体>直接显示>并且应该有一个过滤器文件夹,向您展示如何制作通用过滤器并执行您想要的操作

you could build one yourself. If you look into the windows sdk 7.0~ folders you can go to samples > multimedia > directshow > and there should be a filters folder that shows you how to make generic filters and do w/e you want

天生の放荡 2024-12-16 11:35:27

这是一个例子。构建一个 Windows 窗体,如图所示。

单击此链接查看 WinForm 的外观

  • WinForm 本身名为 Form1
  • “录制...”标签名为lblRecording
  • ComboBox 名为cbCameraDevices
  • “停止”按钮名称为button1
  • “复制”按钮名称为button2
  • “开始”按钮名称为btnStartVideo
  • 还有一个名为pictureBox1的pictureBox,其中将显示视频图像。

这些名称让我们将事件处理程序(下面的代码)与相应的控件关联起来。

如果程序成功构建并运行,请使用组合框选择可用的源。单击“开始”查看视频源。单击“复制”将图像克隆到剪贴板。单击“停止”关闭图像源。

.NET Framework 4.5进行测试

  • 该代码使用 Microsoft: Windows 10 - X64(Intel 机器)
  • Visual Studio 2017 Community

要构建代码,包含此代码的项目需要具有以下引用

  • AForge.Video.DirectShow
  • AForge.Video
  • AForge

这些包可以通过 NuGet 拉入项目中。在 Visual Studio IDE 中:

  1. 工具 ->
  2. NuGet 包管理器 ->
  3. 管理解决方案的 NuGet 包...

搜索“AForge”并安装相应的包。

代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using CameraDevice;
using AForge.Video.DirectShow;
using System.Threading;

namespace CameraCaptureTest3
{
    public partial class Form1 : Form
    {
        CameraImaging camImg;
        bool StopVideo = true;
        Thread thrVideo;
        object mImageLock;
        FilterInfoCollection videoDevices;

        public Form1()
        {
            InitializeComponent();
            camImg = new CameraImaging();
            mImageLock = new object();
            // enumerate video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            cbCameraDevices.Items.Clear();
            foreach(FilterInfo i in videoDevices) cbCameraDevices.Items.Add(i.Name);
        }

        //---------------------------------------------------------
        // VideoRecordin() is meant to be run on a separate thread
        //---------------------------------------------------------            
        private void VideoRecording()
        {
            camImg.videoSource.Start();

            while (!StopVideo)
            {
                lock (mImageLock)
                {
                    Bitmap tmp = (Bitmap)camImg.bitmap.Clone();

                    if (InvokeRequired)
                    {
                        BeginInvoke(new MethodInvoker(() =>
                        {
                            pictureBox1.Image = tmp;
                            pictureBox1.Invalidate();
                        }));
                    }
                    else
                    {
                        pictureBox1.Image = tmp;
                        pictureBox1.Invalidate();
                    }
                }
                Thread.Sleep(33);
            }
            camImg.videoSource.Stop();
        }

        private void btnStartVideo_Click(object sender, EventArgs e)
        {
            StopVideo = false;
            try
            {
                camImg.videoSource = new VideoCaptureDevice(camImg.videoDevices[cbCameraDevices.SelectedIndex].MonikerString);
                thrVideo = new Thread(VideoRecording);
                thrVideo.Start();
                Thread.Sleep(1000);
                lblRecording.Visible = true;
            }
            catch (Exception)
            {
                MessageBox.Show("No camera is chosen.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopVideo = true;
            if (thrVideo != null) thrVideo.Join();
            lblRecording.Visible = false;
            Application.DoEvents();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StopVideo = true;
            if (thrVideo != null)
                while (thrVideo.ThreadState == ThreadState.Running)
                    Application.DoEvents();
            pictureBox1.Image = null;
            lblRecording.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(pictureBox1.Image);
        }
    }
}

Here is an example. Construct a Windows Form as shown in the picture.

Click this link to see how the WinForm looks

  • The WinForm itself is named Form1
  • "Recording ..." label is named lblRecording
  • ComboBox is named cbCameraDevices
  • "Stop" button name is button1
  • "Copy" button name is button2
  • "Start" button name is btnStartVideo
  • There is also a pictureBox named pictureBox1 in which video image will be shown.

These names let us associate the event handlers (code below) with the respective control.

If the program is built successfully and run, use the combobox to select an available source. Click "Start" to see video feed. Click "Copy" to clone the image onto the clipboard. Click "Stop" to close the image feed.

The code was tested using Microsoft:

  • Windows 10 - X64 (Intel machine)
  • Visual Studio 2017 Community
  • .NET Framework 4.5.

To build the code, the project containing this code needs to have these References:

  • AForge.Video.DirectShow
  • AForge.Video
  • AForge

The packages can be pulled into the project by NuGet. In Visual Studio IDE:

  1. Tools ->
  2. NuGet Package Manager ->
  3. Manage NuGet Package for Solution...

Search for "AForge" and install the respective packages.

Code:

using System;
using System.Drawing;
using System.Windows.Forms;
using CameraDevice;
using AForge.Video.DirectShow;
using System.Threading;

namespace CameraCaptureTest3
{
    public partial class Form1 : Form
    {
        CameraImaging camImg;
        bool StopVideo = true;
        Thread thrVideo;
        object mImageLock;
        FilterInfoCollection videoDevices;

        public Form1()
        {
            InitializeComponent();
            camImg = new CameraImaging();
            mImageLock = new object();
            // enumerate video devices
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            cbCameraDevices.Items.Clear();
            foreach(FilterInfo i in videoDevices) cbCameraDevices.Items.Add(i.Name);
        }

        //---------------------------------------------------------
        // VideoRecordin() is meant to be run on a separate thread
        //---------------------------------------------------------            
        private void VideoRecording()
        {
            camImg.videoSource.Start();

            while (!StopVideo)
            {
                lock (mImageLock)
                {
                    Bitmap tmp = (Bitmap)camImg.bitmap.Clone();

                    if (InvokeRequired)
                    {
                        BeginInvoke(new MethodInvoker(() =>
                        {
                            pictureBox1.Image = tmp;
                            pictureBox1.Invalidate();
                        }));
                    }
                    else
                    {
                        pictureBox1.Image = tmp;
                        pictureBox1.Invalidate();
                    }
                }
                Thread.Sleep(33);
            }
            camImg.videoSource.Stop();
        }

        private void btnStartVideo_Click(object sender, EventArgs e)
        {
            StopVideo = false;
            try
            {
                camImg.videoSource = new VideoCaptureDevice(camImg.videoDevices[cbCameraDevices.SelectedIndex].MonikerString);
                thrVideo = new Thread(VideoRecording);
                thrVideo.Start();
                Thread.Sleep(1000);
                lblRecording.Visible = true;
            }
            catch (Exception)
            {
                MessageBox.Show("No camera is chosen.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopVideo = true;
            if (thrVideo != null) thrVideo.Join();
            lblRecording.Visible = false;
            Application.DoEvents();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StopVideo = true;
            if (thrVideo != null)
                while (thrVideo.ThreadState == ThreadState.Running)
                    Application.DoEvents();
            pictureBox1.Image = null;
            lblRecording.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(pictureBox1.Image);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文