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

时间:2011-10-13 07:37:58

标签: c# .net webcam directshow.net

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

3 个答案:

答案 0 :(得分:11)

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

以下是一些示例代码:Access to USB cameras and video files using DirectShow

答案 1 :(得分:4)

你可以自己建一个。如果你查看windows sdk 7.0~文件夹,你可以去样本>多媒体> directshow>并且应该有一个过滤器文件夹,向您展示如何制作通用过滤器并使用您想要的

答案 2 :(得分:0)

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

Click this link to see how the WinForm looks

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

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

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

该代码已使用Microsoft测试:

  • Windows 10-X64(Intel计算机)
  • Visual Studio 2017社区
  • .NET Framework 4.5。

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

  • 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);
        }
    }
}
相关问题