博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)
阅读量:5308 次
发布时间:2019-06-14

本文共 5339 字,大约阅读时间需要 17 分钟。

 

前言:

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

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命名空间

    [html]

    1. xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
    xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
  3. 在XAML中添加AForge.Controls命名空间

    [html]

    1. xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
    xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
  4. 在XAML中加入VideoSourcePlayer可视控件

    [html]

    1. <wfi:WindowsFormsHost Grid.Row="0" Margin="5">
    2. <aforge:VideoSourcePlayer x:Name="sourcePlayer" Width="300" Height="360">
    3. </aforge:VideoSourcePlayer>
    4. </wfi:WindowsFormsHost>

演示程序界面:

开发工具:

  • Visual Studio v2010

  • .NET Framework 4 Client Profile

工程文件下载:

源程序:

MainWindow.xaml

 
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Splash;assembly=FingerPictureBox"
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">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

MainWindow.xaml.cs

using System;
using System.Windows;
using System.Windows.Media.Imaging;
using AForge.Video.DirectShow;
using Splash;
 
namespace FaceCapture
{
/// 
/// MainWindow.xaml 的交互逻辑
/// 
public partial class MainWindow : Window
{
BitmapSource ImagePlay;
BitmapSource ImageStop;
 
public MainWindow()
{
InitializeComponent();
 
// 设置窗体图标
this.Icon = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
Properties.Resources.FingerPictureBox.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
 
// 图像源初始化
ImagePlay = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.Button_Play_icon2.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
 
ImageStop = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.Button_Stop_icon.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
 
// 设置按钮图像
image_Play.Source = ImagePlay;
image_Capture.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.capture.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
 
// 设置窗体装载后事件处理器
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
 
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
// 设定初始视频设备
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0)
{   // 默认设备
sourcePlayer.VideoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
}
else
{
button_Play.IsEnabled = false;
button_Capture.IsEnabled = false;
}
 
// 设置图片框初始图像
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
Properties.Resources.noimage.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
 
fingerPictureBox1.InitialImage = bs;
fingerPictureBox2.InitialImage = bs;
fingerPictureBox3.InitialImage = bs;
fingerPictureBox4.InitialImage = bs;
}
 
private void button_Play_Click(object sender, RoutedEventArgs e)
{
if (image_Play.Source == ImagePlay)
{   // 开启视频
sourcePlayer.Start();
if (sourcePlayer.IsRunning)
{
// 改变按钮为“停止”状态
image_Play.Source = ImageStop;
label_Play.Content = "停止";
 
// 允许拍照
button_Capture.IsEnabled = true;
}
}
else
{
if (sourcePlayer.IsRunning)
{   // 停止视频
sourcePlayer.SignalToStop();
sourcePlayer.WaitForStop();
 
// 改变按钮为“开始”状态
image_Play.Source = ImagePlay;
label_Play.Content = "开启摄像头"; ;
 
// 关闭拍照
button_Capture.IsEnabled = false;
}
}
}
 
private void button_Capture_Click(object sender, RoutedEventArgs e)
{
// 判断视频设备是否开启
if (sourcePlayer.IsRunning)
{   // 进行拍照
for (Int32 i = 1; i <= 4; i++)
{
object box = this.FindName("fingerPictureBox" + i);
if(box is FingerPictureBox)
{
if ((box as FingerPictureBox).ActiveImage == (box as FingerPictureBox).InitialImage)
{   // 更新图像
(box as FingerPictureBox).ActiveImage = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
break;
}
}
}
}
}
 
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (sourcePlayer.IsRunning)
{   // 停止视频
sourcePlayer.SignalToStop();
sourcePlayer.WaitForStop();
}
}
}
}

转载于:https://www.cnblogs.com/sczw-maqing/p/3396491.html

你可能感兴趣的文章
web前端之路,js的一些好书(摘自聂微东 )
查看>>
【模板】对拍程序
查看>>
【转】redo与undo
查看>>
解决升级系统导致的 curl: (48) An unknown option was passed in to libcurl
查看>>
Java Session 介绍;
查看>>
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>
Extjs6 经典版 combo下拉框数据的使用及动态传参
查看>>
【NodeJS】http-server.cmd
查看>>
研磨JavaScript系列(五):奇妙的对象
查看>>
面试题2
查看>>
selenium+java iframe定位
查看>>
P2P综述
查看>>
第五章 如何使用Burp Target
查看>>
Sprint阶段测试评分总结
查看>>
sqlite3经常使用命令&amp;语法
查看>>
linux下编译openjdk8
查看>>
【python】--迭代器生成器装饰器
查看>>
Pow(x, n)
查看>>