📜  aforge wpf 网络摄像头 - C# (1)

📅  最后修改于: 2023-12-03 14:59:12.152000             🧑  作者: Mango

AForge WPF网络摄像头 - C#

简介

AForge是一组开源的计算机视觉和人工智能库。其中,AForge.Video.DirectShow是一个用于使用DirectShow接口捕获视频的库, 该库提供了一个方便的接口,用于从桌面上的摄像头捕获视频。通过AForge Video DirectShow类库中的VideoCaptureDevice类,可以轻松地连接本地Webcam或IP摄像头,并获取视频流。

本文将介绍如何使用AForge WPF网络摄像头库来连接和捕获视频流,并进行一些简单的图像处理。

准备工作

首先,我们需要在Visual Studio中创建一个新的WPF应用程序。在项目中右键单击,选择“管理NuGet程序包”,在NuGet窗口中搜索“AForge.Video.DirectShow”。并安装该库。

捕获视频

使用AForge.WPF网络摄像头库可以轻松地捕获视频。使用VideoCaptureDevice类的构造函数,可以指定要使用的设备,并指定回调函数以处理捕获的帧。回调函数可以是任何方法,并将帧作为参数,允许我们执行任何处理。

下面是一个使用AForge.WPF网络摄像头库的简单示例:

using AForge.Video;
using AForge.Video.DirectShow;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        private VideoCaptureDevice _videoCaptureDevice;

        public MainWindow()
        {
            InitializeComponent();

            var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo device in videoDevices)
            {
               _cameraSource.Items.Add(device.Name); //将设备名称添加到combobox中
            }

            if (_cameraSource.Items.Count > 0)
            {
                _cameraSource.SelectedIndex = 0;
            }
        }
       //连接设备
        private void _connectButton_Click(object sender, RoutedEventArgs e)
        {
            _videoCaptureDevice = new VideoCaptureDevice(((FilterInfo) _cameraSource.SelectedItem).MonikerString);
            _videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
            _videoCaptureDevice.Start();
        }
       
       //捕获视频流
        private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            var bitmap = (Bitmap)eventArgs.Frame.Clone();
            _videoPlayer.Source = AForgeWpfHelper.ToBitmapSource(bitmap); //更新UI元素
        }
        //断开设备
        private void _disconnectButton_Click(object sender, RoutedEventArgs e)
        {
            _videoCaptureDevice.Stop();
            _videoCaptureDevice.NewFrame -= VideoCaptureDevice_NewFrame;
            _videoPlayer.Source = null;
        }
    }
}
显示视频

WPF用户界面通常使用XAML编写。此处我们可以使用WPF Image元素以及AForge.WPF工具库中的AForgeWpfHelper.ToBitmapSource()方法来显示摄像头捕获的视频流。

    <Window x:Class="WpfApp1.MainWindow"
        xmlns:aforgeWpf="clr-namespace:AForge.Wpf;assembly=AForge.Wpf"
        ...
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <ComboBox Name="_cameraSource" Grid.Row="0"/>
        <StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Name="_connectButton" Click="_connectButton_Click" Content="Connect"/>
            <Button Name="_disconnectButton" Click="_disconnectButton_Click" Content="Disconnect"/>
        </StackPanel>

        <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1">
            <Image Name="_videoPlayer" Stretch="UniformToFill"/>
        </Border>
    </Grid>
</Window>
总结

在本文中,我们介绍了如何使用AForge WPF网络摄像头库来连接和捕获视频流,并将视频流显示在WPF中。此外,还可以对视频流进行一些简单的处理。