在WPF程序中使用摄像头兼谈如何使用AForge.NET控件

前言:

AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理、神经网络、遗传算法和机器学习等。在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库。本文讲解如何在WPF程序中调用AForge.NET控件实现视频和抓拍功能。

WPF与WinForm控件交互:

要实现视频功能,需要使用AForge.Controls命名空间中的VideoSourcePlayer控件。这是一个WinForm控件,要在WPF程序中使用,我们需要做如下4步:

    1. 添加引用:
在.NET选项卡中选择WindowsFormsIntegration
在浏览选项卡中添加3个AForge.NET类库
AForge.Controls.dll
AForge.Video.dll
AForge.Video.DirectShow.dll
    1. 在XAML中添加System.Windows.Forms.Integration命名空间
  1. xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
    1. 在XAML中添加AForge.Controls命名空间
  1. xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls" 
    1. 在XAML中加入VideoSourcePlayer可视控件
  1. <wfi:WindowsFormsHost Grid.Row="0" Margin="5"> 
  2.     <aforge:VideoSourcePlayer x:Name="sourcePlayer" Width="300" Height="360">                     
  3.     </aforge:VideoSourcePlayer>                 
  4. </wfi:WindowsFormsHost> 
演示程序界面:
开发工具:
工程文件下载:
源程序: MainWindow.xaml
  1. <Window x:Class="FaceCapture.MainWindow" 
  2.         xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" 
  3.         xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls" 
  4.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  5.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  6.         xmlns:my="clr-namespace:Splash;assembly=FingerPictureBox" 
  7.         Title="FaceCapture(WPF)" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="480" d:DesignWidth="902" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" AllowDrop="True" Closing="Window_Closing"> 
  8.     <Grid AllowDrop="True">      
  9.         <Grid.ColumnDefinitions> 
  10.             <ColumnDefinition /> 
  11.             <ColumnDefinition /> 
  12.         </Grid.ColumnDefinitions> 
  13.         <Grid Grid.Column="0" AllowDrop="False"> 
  14.             <Grid.RowDefinitions> 
  15.                 <RowDefinition /> 
  16.                 <RowDefinition /> 
  17.             </Grid.RowDefinitions>             
  18.             <wfi:WindowsFormsHost Grid.Row="0" Margin="5"> 
  19.                 <aforge:VideoSourcePlayer x:Name="sourcePlayer" Width="300" Height="360">                     
  20.                 </aforge:VideoSourcePlayer>                 
  21.             </wfi:WindowsFormsHost> 
  22.             <StackPanel Grid.Row="1" Orientation="Horizontal" Height="60" HorizontalAlignment="Stretch" > 
  23.                 <Button Name="button_Play" Height="40" Width="120" Margin="40,10,20,10" Click="button_Play_Click"> 
  24.                     <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
  25.                         <Image Name="image_Play" Width="32" Height="32" /> 
  26.                         <Label Name="label_Play" Content="开启摄像头" VerticalContentAlignment="Center" FontSize="14" />                      
  27.                     </StackPanel>                     
  28.                 </Button> 
  29.                 <Button Name="button_Capture" Height="40" Width="120" Margin="40,10,40,10" Click="button_Capture_Click"> 
  30.                     <StackPanel Orientation="Horizontal" VerticalAlignment="Center"> 
  31.                         <Image Name="image_Capture" Width="32" Height="32" /> 
  32.                         <Label Content="抓拍图像" VerticalContentAlignment="Center" FontSize="14" /> 
  33.                     </StackPanel> 
  34.                 </Button>  
  35.             </StackPanel>             
  36.         </Grid>    
  37.         <Grid Grid.Column="1"> 
  38.             <Grid.RowDefinitions> 
  39.                 <RowDefinition /> 
  40.                 <RowDefinition /> 
  41.             </Grid.RowDefinitions> 
  42.             <StackPanel Grid.Row="0" Orientation="Horizontal"> 
  43.                 <my:FingerPictureBox Height="210" Name="fingerPictureBox1" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5" /> 
  44.                 <my:FingerPictureBox Height="210" Name="fingerPictureBox2" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5" /> 
  45.             </StackPanel> 
  46.             <StackPanel Grid.Row="1" Orientation="Horizontal"> 
  47.                 <my:FingerPictureBox Height="210" Name="fingerPictureBox3" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5" /> 
  48.                 <my:FingerPictureBox Height="210" Name="fingerPictureBox4" Width="210" BorderThickness="5" BorderBrush="DarkGreen" Margin="5" /> 
  49.             </StackPanel> 
  50.         </Grid>         
  51.     </Grid> 
  52. </Window> 
MainWindow.xaml.cs
  1. using System; 
  2. using System.Windows; 
  3. using System.Windows.Media.Imaging; 
  4. using AForge.Video.DirectShow; 
  5. using Splash; 
  6.  
  7. namespace FaceCapture 
  8.     /// <summary> 
  9.     /// MainWindow.xaml 的交互逻辑 
  10.     /// </summary> 
  11.     public partial class MainWindow : Window 
  12.     { 
  13.         BitmapSource ImagePlay; 
  14.         BitmapSource ImageStop; 
  15.  
  16.         public MainWindow() 
  17.         { 
  18.             InitializeComponent(); 
  19.  
  20.             // 设置窗体图标 
  21.             this.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( 
  22.                 Properties.Resources.FingerPictureBox.Handle, 
  23.                 Int32Rect.Empty, 
  24.                 BitmapSizeOptions.FromEmptyOptions()); 
  25.  
  26.             // 图像源初始化 
  27.             ImagePlay = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 
  28.                 Properties.Resources.Button_Play_icon2.GetHbitmap(), 
  29.                 IntPtr.Zero, 
  30.                 Int32Rect.Empty, 
  31.                 BitmapSizeOptions.FromEmptyOptions()); 
  32.  
  33.             ImageStop = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 
  34.                 Properties.Resources.Button_Stop_icon.GetHbitmap(), 
  35.                 IntPtr.Zero, 
  36.                 Int32Rect.Empty, 
  37.                 BitmapSizeOptions.FromEmptyOptions()); 
  38.  
  39.             // 设置按钮图像 
  40.             image_Play.Source = ImagePlay; 
  41.             image_Capture.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 
  42.                 Properties.Resources.capture.GetHbitmap(), 
  43.                 IntPtr.Zero, 
  44.                 Int32Rect.Empty, 
  45.                 BitmapSizeOptions.FromEmptyOptions());             
  46.  
  47.             // 设置窗体装载后事件处理器 
  48.             this.Loaded += new RoutedEventHandler(MainWindow_Loaded); 
  49.         } 
  50.  
  51.         private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
  52.         { 
  53.             // 设定初始视频设备 
  54.             FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 
  55.             if (videoDevices.Count > 0) 
  56.             {   // 默认设备 
  57.                 sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);                 
  58.             } 
  59.             else 
  60.             { 
  61.                 button_Play.IsEnabled = false
  62.                 button_Capture.IsEnabled = false
  63.             } 
  64.  
  65.             // 设置图片框初始图像 
  66.             BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 
  67.                 Properties.Resources.noimage.GetHbitmap(), 
  68.                 IntPtr.Zero, 
  69.                 Int32Rect.Empty, 
  70.                 BitmapSizeOptions.FromEmptyOptions()); 
  71.  
  72.             fingerPictureBox1.InitialImage = bs; 
  73.             fingerPictureBox2.InitialImage = bs; 
  74.             fingerPictureBox3.InitialImage = bs; 
  75.             fingerPictureBox4.InitialImage = bs; 
  76.         } 
  77.  
  78.         private void button_Play_Click(object sender, RoutedEventArgs e) 
  79.         {             
  80.             if (image_Play.Source == ImagePlay) 
  81.             {   // 开启视频 
  82.                 sourcePlayer.Start(); 
  83.                 if (sourcePlayer.IsRunning) 
  84.                 { 
  85.                     // 改变按钮为“停止”状态 
  86.                     image_Play.Source = ImageStop; 
  87.                     label_Play.Content = "停止"
  88.  
  89.                     // 允许拍照 
  90.                     button_Capture.IsEnabled = true
  91.                 } 
  92.             }   
  93.             else 
  94.             { 
  95.                 if (sourcePlayer.IsRunning) 
  96.                 {   // 停止视频 
  97.                     sourcePlayer.SignalToStop(); 
  98.                     sourcePlayer.WaitForStop(); 
  99.  
  100.                     // 改变按钮为“开始”状态 
  101.                     image_Play.Source = ImagePlay; 
  102.                     label_Play.Content = "开启摄像头"; ; 
  103.  
  104.                     // 关闭拍照 
  105.                     button_Capture.IsEnabled = false
  106.                 }                 
  107.             } 
  108.         } 
  109.  
  110.         private void button_Capture_Click(object sender, RoutedEventArgs e) 
  111.         { 
  112.             // 判断视频设备是否开启 
  113.             if (sourcePlayer.IsRunning) 
  114.             {   // 进行拍照 
  115.                 for (Int32 i = 1; i <= 4; i++) 
  116.                 { 
  117.                     object box = this.FindName("fingerPictureBox" + i); 
  118.                     if(box is FingerPictureBox) 
  119.                     { 
  120.                         if ((box as FingerPictureBox).ActiveImage == (box as FingerPictureBox).InitialImage) 
  121.                         {   // 更新图像 
  122.                             (box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( 
  123.                                 sourcePlayer.GetCurrentVideoFrame().GetHbitmap(), 
  124.                                 IntPtr.Zero, 
  125.                                 Int32Rect.Empty, 
  126.                                 BitmapSizeOptions.FromEmptyOptions()); 
  127.                             break
  128.                         }                     
  129.                     } 
  130.                 } 
  131.             } 
  132.         } 
  133.  
  134.         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
  135.         { 
  136.             if (sourcePlayer.IsRunning) 
  137.             {   // 停止视频 
  138.                 sourcePlayer.SignalToStop(); 
  139.                 sourcePlayer.WaitForStop(); 
  140.             } 
  141.         } 
  142.     } 
原文地址:https://www.cnblogs.com/andyzhao365/p/2224695.html