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

215 篇文章 2 订阅
25 篇文章 5 订阅

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/6619762


前言:

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
  2. 在XAML中添加System.Windows.Forms.Integration命名空间
    1. xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"  
  3. 在XAML中添加AForge.Controls命名空间
    1. xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"  
  4. 在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
[csharp] view plain copy
  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. {  
  9.     /// <summary>  
  10.     /// MainWindow.xaml 的交互逻辑  
  11.     /// </summary>  
  12.     public partial class MainWindow : Window  
  13.     {  
  14.         BitmapSource ImagePlay;  
  15.         BitmapSource ImageStop;  
  16.   
  17.         public MainWindow()  
  18.         {  
  19.             InitializeComponent();  
  20.   
  21.             // 设置窗体图标  
  22.             this.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(  
  23.                 Properties.Resources.FingerPictureBox.Handle,  
  24.                 Int32Rect.Empty,  
  25.                 BitmapSizeOptions.FromEmptyOptions());  
  26.   
  27.             // 图像源初始化  
  28.             ImagePlay = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
  29.                 Properties.Resources.Button_Play_icon2.GetHbitmap(),  
  30.                 IntPtr.Zero,  
  31.                 Int32Rect.Empty,  
  32.                 BitmapSizeOptions.FromEmptyOptions());  
  33.   
  34.             ImageStop = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
  35.                 Properties.Resources.Button_Stop_icon.GetHbitmap(),  
  36.                 IntPtr.Zero,  
  37.                 Int32Rect.Empty,  
  38.                 BitmapSizeOptions.FromEmptyOptions());  
  39.   
  40.             // 设置按钮图像  
  41.             image_Play.Source = ImagePlay;  
  42.             image_Capture.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
  43.                 Properties.Resources.capture.GetHbitmap(),  
  44.                 IntPtr.Zero,  
  45.                 Int32Rect.Empty,  
  46.                 BitmapSizeOptions.FromEmptyOptions());              
  47.   
  48.             // 设置窗体装载后事件处理器  
  49.             this.Loaded += new RoutedEventHandler(MainWindow_Loaded);  
  50.         }  
  51.   
  52.         private void MainWindow_Loaded(object sender, RoutedEventArgs e)  
  53.         {  
  54.             // 设定初始视频设备  
  55.             FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);  
  56.             if (videoDevices.Count > 0)  
  57.             {   // 默认设备  
  58.                 sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);                  
  59.             }  
  60.             else  
  61.             {  
  62.                 button_Play.IsEnabled = false;  
  63.                 button_Capture.IsEnabled = false;  
  64.             }  
  65.   
  66.             // 设置图片框初始图像  
  67.             BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
  68.                 Properties.Resources.noimage.GetHbitmap(),  
  69.                 IntPtr.Zero,  
  70.                 Int32Rect.Empty,  
  71.                 BitmapSizeOptions.FromEmptyOptions());  
  72.   
  73.             fingerPictureBox1.InitialImage = bs;  
  74.             fingerPictureBox2.InitialImage = bs;  
  75.             fingerPictureBox3.InitialImage = bs;  
  76.             fingerPictureBox4.InitialImage = bs;  
  77.         }  
  78.   
  79.         private void button_Play_Click(object sender, RoutedEventArgs e)  
  80.         {              
  81.             if (image_Play.Source == ImagePlay)  
  82.             {   // 开启视频  
  83.                 sourcePlayer.Start();  
  84.                 if (sourcePlayer.IsRunning)  
  85.                 {  
  86.                     // 改变按钮为“停止”状态  
  87.                     image_Play.Source = ImageStop;  
  88.                     label_Play.Content = "停止";  
  89.   
  90.                     // 允许拍照  
  91.                     button_Capture.IsEnabled = true;  
  92.                 }  
  93.             }    
  94.             else  
  95.             {  
  96.                 if (sourcePlayer.IsRunning)  
  97.                 {   // 停止视频  
  98.                     sourcePlayer.SignalToStop();  
  99.                     sourcePlayer.WaitForStop();  
  100.   
  101.                     // 改变按钮为“开始”状态  
  102.                     image_Play.Source = ImagePlay;  
  103.                     label_Play.Content = "开启摄像头"; ;  
  104.   
  105.                     // 关闭拍照  
  106.                     button_Capture.IsEnabled = false;  
  107.                 }                  
  108.             }  
  109.         }  
  110.   
  111.         private void button_Capture_Click(object sender, RoutedEventArgs e)  
  112.         {  
  113.             // 判断视频设备是否开启  
  114.             if (sourcePlayer.IsRunning)  
  115.             {   // 进行拍照  
  116.                 for (Int32 i = 1; i <= 4; i++)  
  117.                 {  
  118.                     object box = this.FindName("fingerPictureBox" + i);  
  119.                     if(box is FingerPictureBox)  
  120.                     {  
  121.                         if ((box as FingerPictureBox).ActiveImage == (box as FingerPictureBox).InitialImage)  
  122.                         {   // 更新图像  
  123.                             (box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(  
  124.                                 sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),  
  125.                                 IntPtr.Zero,  
  126.                                 Int32Rect.Empty,  
  127.                                 BitmapSizeOptions.FromEmptyOptions());  
  128.                             break;  
  129.                         }                      
  130.                     }  
  131.                 }  
  132.             }  
  133.         }  
  134.   
  135.         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)  
  136.         {  
  137.             if (sourcePlayer.IsRunning)  
  138.             {   // 停止视频  
  139.                 sourcePlayer.SignalToStop();  
  140.                 sourcePlayer.WaitForStop();  
  141.             }  
  142.         }  
  143.     }  
  144. }  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值