本文简略地介绍一下如果使用AForge来实现前置/后置摄像头的预览功能。

要使用AForge,就需要添加AForge NuGet相关包的引用,这些包依赖的其他包会自动安装。

  • AForge.Controls
  • AForge.Video.DirectShow

接下来需要添加另外两个引用,主要是为了使用VideoSourcePlayer Windows Forms 控件。

  • System.Windows.Forms
  • WindowsFormsIntergration

UI界面比较简单,Xaml code 如下:

<Window x:Class="WebcamPreview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:AForge.Controls;assembly=AForge.Controls"
mc:Ignorable="d"
Title="Webcam" Height="240" Width="640" MinHeight="240" MinWidth="640" ResizeMode="CanMinimize">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<WindowsFormsHost Background="Transparent">
<controls:VideoSourcePlayer x:Name="VideoSourcePlayer1" />
</WindowsFormsHost>
<WindowsFormsHost Background="Transparent" Grid.Column="1" >
<controls:VideoSourcePlayer x:Name="VideoSourcePlayer2" />
</WindowsFormsHost>
</Grid>
</Window>

定义一个描述video device的类。

    /// <summary>
/// Represents class that contains media information for video device source.
/// </summary>
public sealed class MediaInformation
{
/// <summary>
/// Gets or sets the display name of the video device source.
/// </summary>
public string DisplayName
{
get;
set;
} /// <summary>
/// Gets or sets the Moniker string of the video device source.
/// </summary>
public string MonikerString
{
get;
set;
}
}

实现一个WebcamDevice类,主要是用来初始化前置/后置摄像头的。

using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Linq; namespace WebcamPreview
{
public class WebcamDevice
{
// <summary>
/// Instance of video capture device.
/// </summary>
private VideoCaptureDevice videoCaptureDevice;
private VideoSourcePlayer videoPlayer;
private string deviceMoniker; public WebcamDevice(VideoSourcePlayer player, string deviceMoniker)
{
this.videoPlayer = player;
this.deviceMoniker = deviceMoniker;
} public void Init()
{
try
{
this.videoCaptureDevice = new VideoCaptureDevice(deviceMoniker);
this.videoPlayer.VideoSource = this.videoCaptureDevice;
this.videoPlayer.Start();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
} /// <summary>
/// Gets video device source collection current available.
/// </summary>
public static IReadOnlyList<MediaInformation> GetVideoDevices()
{
var filterVideoDeviceCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
return (from FilterInfo filterInfo
in filterVideoDeviceCollection
select new MediaInformation
{
DisplayName = filterInfo.Name,
MonikerString = filterInfo.MonikerString
}).ToList(); }
}
}

GetVideoDevices方法用来获取所有的摄像头。

Init方法,制定VideoSourcePlayer的VideoCaptureDevice是前置,还是后置摄像头。然后调用VideoSourcePlayer.Start方法就可以实现预览效果了。

最后就可以在MainWindow调用这个类了。

public partial class MainWindow : Window
{
private IReadOnlyList<MediaInformation> mediaDeviceList; public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
this.Closed += MainWindow_Closed;
this.Deactivated += MainWindow_Deactivated;
this.Topmost = true;
} private void MainWindow_Closed(object sender, EventArgs e)
{
//防止视频关闭时画面延迟闪烁
this.Height = ;
this.Width = ;
if (!this.VideoSourcePlayer1.IsDisposed)
{
this.VideoSourcePlayer1.SignalToStop();
this.VideoSourcePlayer1.WaitForStop();
this.VideoSourcePlayer1.Stop();
this.VideoSourcePlayer1.VideoSource = null;
this.VideoSourcePlayer1.Dispose();
} if (!this.VideoSourcePlayer2.IsDisposed)
{
this.VideoSourcePlayer2.SignalToStop();
this.VideoSourcePlayer2.WaitForStop();
this.VideoSourcePlayer2.Stop();
this.VideoSourcePlayer2.VideoSource = null;
this.VideoSourcePlayer2.Dispose();
}
} private void MainWindow_Deactivated(object sender, EventArgs e)
{
this.Topmost = true;
} private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.mediaDeviceList = WebcamDevice.GetVideoDevices();
InitFront();
InitBack();
} private void InitFront()
{
if (mediaDeviceList.Count() == )
return; var webCamDevice = new WebcamDevice(this.VideoSourcePlayer1, mediaDeviceList.First().MonikerString);
webCamDevice.Init();
} private void InitBack()
{
if (mediaDeviceList.Count() <= )
return; var webCamDevice = new WebcamDevice(this.VideoSourcePlayer2, mediaDeviceList[].MonikerString);
webCamDevice.Init();
}
}

在Window的Closed事件,需要销毁VideoSourcePlayer对象。

另外指定Topmost=true,可以使这个窗口始终在最前面。

WPF使用AForge实现Webcam预览(一)的更多相关文章

  1. WPF使用AForge实现Webcam预览(二)

    本文主要介绍如何让摄像头预览界面的宽高比始终在16:9. 首先我们需要修改一下上一篇随笔实现的UI界面,让Grid变成一个3*3的九宫格,预览界面位于正中间.Xaml示例代码如下: <Windo ...

  2. 用WPF实现打印及打印预览

    原文:用WPF实现打印及打印预览 应该说,WPF极大地简化了我们的打印输出工作,想过去使用VC++做开发的时候,打印及预览可是一件极麻烦的事情,而现在我不会再使用C++来做Windows的桌面应用了- ...

  3. WPF的路由事件、冒泡事件、隧道事件(预览事件)

    本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所 ...

  4. [Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]

    原文:[Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]  我的文章一定要做到对读者负责,否则就是失败的文章  -- ...

  5. WPF中的事件及冒泡事件和隧道事件(预览事件)的区别

    WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别   WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别 本文摘要: 1:什么是路由事件: 2:中断事件路 ...

  6. WPF图片预览之移动、旋转、缩放

    原文:WPF图片预览之移动.旋转.缩放 RT,这个功能比较常见,但凡涉及到图片预览的都跑不了,在说自己的实现方式前,介绍一个好用的控件:Extended.Toolkit中的Zoombox,感兴趣的同学 ...

  7. WPF实现可视化控件打印及打印预览

    打印预览XAML代码: <controls:WindowEx x:Class="SunCreate.Vipf.Client.UI.MapPrintPreview" xmlns ...

  8. WPF 标签预览可以显示图片运行后不显示

    使用<Image HorizontalAlignment="Left" Height="100" Margin="106,111,0,0&quo ...

  9. winForm 打印预览

    自己很少写技术博客,虽然已经干程序员两年多了,winform开发,web开发都干过,不论项目大小对于.net的相关技术也是了解的,如mvc,wcf,wpf,silverlight,socekt通讯,n ...

随机推荐

  1. javaScript实现图片滚动及一个普通图片轮播的代码

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Html5实现手机九宫格password解锁功能

    HTML5真的是非常强大,前端时间看到一个canvas实现九宫格的password解锁. 今天抽空模仿了一个,特定分享一下. 效果截图例如以下: 效果看起来还不错吧! 源代码例如以下: <!DO ...

  3. 关于hexo的SEO的好文章

    1.hexo高阶教程:想让你的博客被更多的人在搜索引擎中搜到吗? 2.Hexo Seo优化让你的博客在google搜索排名第一 3.hexo 博客 seo 优化 4.HEXO SEO 高级优化 5.H ...

  4. android studio的模拟器waiting for target device to come online原因

    android studio的模拟器一直waiting for target device to come online,demo也运行不上去 如图所示: 你很可能运行的android 6.0 (AP ...

  5. win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息

    原文:win10 支持默认把触摸提升鼠标事件 打开 Pointer 消息 在 WPF 经常需要重写一套触摸事件,没有UWP的Pointer那么好用. 如果一直都觉得 WPF 的触摸做的不好,或想解决 ...

  6. webpack单独构建scss文件与.vue组件里构建scss的一个坑

    在入口main.js里构建scss是通过引入模块的方式 import './assets/_reset.scss'; import './assets/_flex.scss'; import './a ...

  7. 他们实际上控制的定义很easy5/12

    尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 近期龙体欠安.非常多任务都堆着,虽 ...

  8. LVS实现负载均衡原理及安装配置

    LVS实现负载均衡原理及安装配置 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F ...

  9. 微信小程序 获取用户信息 encryptData解密 C#版本

    最近学习小程序开发,需要对encryptData解密,获取用户信息,官方源码没有C#版本,网上的资料比较杂,有的使用还有问题,下面贴一下自己亲试可以使用的一个源码 1.code 换取 session_ ...

  10. WPF中,怎样将XAML代码加载为相应的对象?

    原文:WPF中,怎样将XAML代码加载为相应的对象? 在前面"在WPF中,如何得到任何Object对象的XAML代码?"一文中,我介绍了使用System.Windows.Marku ...