首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用DirectShow.NET从摄像头捕捉帧

用DirectShow.NET从摄像头捕捉帧
EN

Stack Overflow用户
提问于 2011-10-13 07:37:58
回答 3查看 29.9K关注 0票数 7

我是DirectShow的新手,所以这个图书馆的一些地方我不太懂。我已经看到了示例DxSnap,但我需要捕获帧而不预览它,以便进行进一步的处理。我该怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-14 17:10:21

你可以自己造一个。如果您查看WindowsSDK7.0~文件夹,您可以转到示例>多媒体> directshow >,并且应该有一个筛选器文件夹,向您展示如何制作通用过滤器并执行所需的w/e操作。

票数 4
EN

Stack Overflow用户

发布于 2011-10-13 08:43:51

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

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

票数 12
EN

Stack Overflow用户

发布于 2019-01-23 19:16:09

下面是一个例子。构造如图所示的Windows窗体。

单击此链接查看WinForm的外观

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

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

如果程序已成功构建并运行,请使用combobox选择可用的源。点击"Start“查看视频提要。单击“复制”将图像复制到剪贴板上。单击"Stop“关闭图像提要。

使用Microsoft对代码进行了测试:

  • Windows 10 - X64 (英特尔机器)
  • Visual 2017社区
  • .NET框架4.5。

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

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

NuGet可以将这些包拖到项目中。在中:

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

搜索"AForge“并安装相应的软件包。

代码:

代码语言:javascript
复制
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);
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7750879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档