C# WPF使用使用AForge类库操作类库操作USB摄像头拍照并保存摄像头拍照并保存
项目中用到 USB 摄像头,需要根据情况进行图像抓拍,查了半天资料,比较多的是使用 WPFMediaKit 和 AForge 。
但是由于项目要求不显示 USB 摄像头拍摄的画面,最终确定使用 AForge 解决。
下面用一个测试程序记录一下。
一、无预览拍照一、无预览拍照
首先建立一个 WPF 项目,我的就叫 AForgeTest,你们随意就好:
然后在 NuGet 包管理器中安装 AForge 库:
我只安装了图中打勾的几个库,这个根据自己项目需要安装就好。
不过用 USB 摄像头拍照必须安装:
AForge.Video
AForge.Control
AForge.Video.DirectShow
这三个库文件。
不习惯使用 NuGet 也可以到 AForge 的 .NET lib 下载页面下载。
在 MainWindow.xaml 文件中添加两个按钮:
<Window x:Class="AForgeTest.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:local="clr-namespace:AForgeTest"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300"
Closing="Window_Closing">
<StackPanel>
<Button Name="btnCapture" Click="btnCapture_Click">拍照</Button>
<Button Name="btnOpenCamera" Click="btnOpenCamera_Click">打开</Button>
</StackPanel>
</Window>
后台交互逻辑如下:
using System;
using System.Windows;
namespace AForgeTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenCamera_Click(object sender, EventArgs e)
{
CameraHelper.UpdateCameraDevices();
if (CameraHelper.CameraDevices.Count > 0)
{
CameraHelper.SetCameraDevice(0);
}
}
private void btnCapture_Click(object sender, EventArgs e)
{
CameraHelper.CaptureImage(@"E:\1");
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
CameraHelper.CloseDevice();
}
}
}
CameraHelper 类代码如下:
using System;
using AForge.Video.DirectShow;
using AForge.Controls;
using System.Windows;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
namespace AForgeTest
{
public static class CameraHelper
{
private static FilterInfoCollection _cameraDevices;
private static VideoCaptureDevice div = null;
private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();
private static bool _isDisplay = false;
//指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false
private static bool isSet = false;
/// <summary>
/// 获取或设置摄像头设备,无设备为null
/// </summary>
public static FilterInfoCollection CameraDevices
{
get
{
return _cameraDevices;
}
set
{
_cameraDevices = value;
}
}
/// <summary>
/// 指示是否显示摄像头视频画面
/// 默认false
/// </summary>
public static bool IsDisplay
{
get { return _isDisplay; }
set { _isDisplay = value; }
}
/// <summary>
/// 获取或设置VideoSourcePlayer控件,
/// 只有当IsDisplay设置为true时,该属性才可以设置成功
/// </summary>
public static VideoSourcePlayer SourcePlayer
{
get { return sourcePlayer; }
set
{
if (_isDisplay)
{
sourcePlayer = value;
isSet = true;
}
}
}
/// <summary>
/// 更新摄像头设备信息
/// </summary>
public static void UpdateCameraDevices()
{
_cameraDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
}
/// <summary>
/// 设置使用的摄像头设备
/// </summary>
/// <param name="index">设备在CameraDevices中的索引</param>
/// <returns><see cref="bool"/></returns>
public static bool SetCameraDevice(int index)
{
if (!isSet) _isDisplay = false;
//无设备,返回false
if (_cameraDevices.Count <= 0 || index < 0) return false;
if (index > _cameraDevices.Count - 1) return false;