65.9K
CodeProject 正在变化。 阅读更多。
Home

Fabrika LAB:网络摄像头视频

starIconstarIconstarIconstarIconstarIcon

5.00/5 (20投票s)

2013年3月11日

CPOL

3分钟阅读

viewsIcon

118960

downloadIcon

12587

使用 AForge 库访问网络摄像头的一种简单易行的方法。

引言

网络上有很多文章解释如何访问网络摄像头。 它们中的大多数都将 DirectShow 作为推荐方法。 由于从 .NET 语言处理 DirectShow 并不是一件容易的事,所以我决定使用 Aforge 库。 Aforge 是一个很棒的开源库,用于处理图像和许多其他东西,包括 DirectShow 视频。

在本文中,我将向您展示连接到网络摄像头并显示一些用户生成的内容在视频顶部是多么容易。 这种方法可能对增强现实应用程序有用。

背景

当我在为国家文件捕获符合 ICAO 标准的面部图像的生物识别注册软件工作时,我想使用网络摄像头作为来源。 在评估了不同的方法后,我选择了一个免费且易于使用的。

使用代码

我已将源代码发布为 Visual Studio 2012 项目。 示例中使用的语言是 VB.NET。 我相信它很容易理解,并且可以轻松移植到任何其他 .NET 语言。

导入 AForge 库

您可以访问 AForge 网站并下载该组件的当前版本,并将以下程序集包含到项目中(解决方案)中

  • AForge.dll (核心)
  • AForge.Math.dll
  • AForge.Imaging.dll
  • AForge.Video.dll
  • AForge.Video.DirectShow
  • AForge.Controls

最简单的方法可能是使用 NuGet 管理器(Visual Studio > 工具 > 库包管理器 > 管理解决方案的 NuGet 包)并搜索在线包,如下图所示。

枚举视频源和视频模式

在引用 AForge 库后,您需要枚举视频源(可用的网络摄像头列表)及其视频模式(分辨率)。

Private Sub EnumerateVideoDevices()
    ' enumerate video devices
    videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
    If videoDevices.Count <> 0 Then
        ' add all devices to combo
        For Each device As FilterInfo In videoDevices
            ComboBoxSources.Items.Add(device.Name)
        Next
    Else
        ComboBoxSources.Items.Add("No DirectShow devices found")
    End If
    ComboBoxSources.SelectedIndex = 0
End Sub

Private Sub EnumerateVideoModes(device As VideoCaptureDevice)
    ' get resolutions for selected video source
    Me.Cursor = Cursors.WaitCursor
    ComboBoxModes.Items.Clear()
    Try
        videoCapabilities = videoDevice.VideoCapabilities
        For Each capabilty As VideoCapabilities In videoCapabilities
            If Not ComboBoxModes.Items.Contains(capabilty.FrameSize) Then
                ComboBoxModes.Items.Add(capabilty.FrameSize)
            End If
        Next
        If videoCapabilities.Length = 0 Then
            ComboBoxModes.Items.Add("Not supported")
        End If
        ComboBoxModes.SelectedIndex = 0
    Finally
        Me.Cursor = Cursors.[Default]
    End Try
End Sub

EnumerateVideoDevices 将检查计算机上所有可用的 DirectShow 设备,并在 videoDevices 变量中枚举它们。 在获取所有可用设备后,用户可以选择使用哪个摄像头。 这在新设备中是必需的,其中存在多个摄像头 - 例如带有前后摄像头的新 Microsoft 平板电脑设备。

选择设备后,我们必须为用户提供一个选择,以选择摄像头的分辨率和摄像头向我们提供新帧的速度。 为了枚举用于选择摄像头的模式,我们使用 EnumerateVideoModes 函数。 由于此过程可能需要一些时间,建议更改默认光标,甚至最好使用 asyncBackgroundWorker 来完成这项工作。

启动和停止视频

使用 AForge.Controls 程序集将新控件添加到工具箱。 添加的工具之一称为 VideoSourcePlayer,它用于显示来自摄像头的实时视频。 以下代码段显示了如何在此控件中启动和结束显示实时视频流。

Private Sub CameraStart()
    If videoDevice IsNot Nothing Then
        If (videoCapabilities IsNot Nothing) AndAlso (videoCapabilities.Length <> 0) Then
            videoDevice.DesiredFrameSize = DirectCast(ComboBoxModes.SelectedItem, Size)
        End If
        VideoSourcePlayer1.VideoSource = videoDevice
        VideoSourcePlayer1.Start()
    End If
End Sub

Private Sub CameraStop()
    If VideoSourcePlayer1.VideoSource IsNot Nothing Then
        ' stop video device
        VideoSourcePlayer1.SignalToStop()
        VideoSourcePlayer1.WaitForStop()
        VideoSourcePlayer1.VideoSource = Nothing
    End If
End Sub

方法 CameraStart 将选定的视频源和视频模式分配给 Aforge 的控件 VideoSourcePlayer。 在调用 .Start 方法后,视频将显示在此组件上。 要停止显示视频,您需要向 VideoSourcePlayer 控件发出停止从摄像头源显示视频的信号。 请注意,视频线程与您的用户界面线程不同。

将您自己的内容添加到视频中

通过将您自己的内容添加到视频中来与之交互很有用。 下面的示例演示了如何将自定义字符串添加到视频中。 通过使用众所周知的 Graphics 对象,您可以与视频进行交互。

Private Sub VideoSourcePlayer1_NewFrame(sender As Object, _
        ByRef image As Bitmap) Handles VideoSourcePlayer1.NewFrame
    ' add overlay
    Dim g As Graphics = Graphics.FromImage(image)
    g.DrawString("Augmented reality?", _
       New Font("Arial", 16), Brushes.Black, New Rectangle(10, 10, 200, 50))
    g.Dispose()
End Sub

这可能是本文中最重要的部分,因为它允许开发人员访问视频源。 当前视频帧可以从参数变量 image 访问。 通过使用 Graphics 对象,可以将不同的图形信息添加到视频中,例如显示日期和时间。 一个人可以使用图像进行高级处理,例如在图像中查找对象或显示与当前图像相关的其他有用信息。

我希望我向您展示了在您的项目中使用网络摄像头是多么容易,我希望看到一些使用网络摄像头的优秀应用程序。

© . All rights reserved.