1、前言:
C#调用摄像头是为了完成一个项目( 工厂物资管理系统. )中的人脸识别功能。
对人脸识别感兴趣的小伙伴可以移步至:https://blog.csdn.net/qq_41971768/article/details/103823308
在 C # 调用摄像头的实现过程中,我曾在博客上借鉴其他人的做法。在这里给出相应的博客地址如下:
- C#打开摄像头抓取照片然后退出 : 该作者转载的其他人的方法,帮助不大
- .net中捕获摄像头视频的方式及对比 : 该作者介绍了几种不同的实现方式,并对其作了对比。
- C#通过引用AForge获取摄像头数据 : 该作者提供了AForge的下载和如何加入到VS中,但未给出代码实现。
- Aforge.net类库调用摄像头拍照(C#) : 该作者提供了AForge的下载以及加载,并附有代码。
本文也是借鉴于第四篇博客。
2、正文:
2.1 下载 AForge 库
进入AForge官网:http://www.aforgenet.com/framework/downloads.html下载AForge库:
2.2 通过VS添加引用
- AForge.Imaging ——图像处理和过滤器
- AForge.Video ——视频处理类库
- AForge.Controls—— 图像显示控件
- 进入引用管理器如下图:
- 点击浏览:
找到AForge.NET Framework-2.2.5-(libs only)\Release目录
添加以下文件:
"AForge.Video.dll"
"AForge.Controls.dll"
"AForge.dll"
"AForge.Imaging.dll"
"AForge.Video.DirectShow.dll"
- 添加结果如下:
2.3 工具箱添加AForge的组件
- 打开工具箱>>右键>>添加选项卡>>输入名称AForge
- 选中AForge>>右键>>选择项>>浏览进入AForge.NET Framework-2.2.5-(libs only)\Release目录>>选中 “AForge.Controls.dll”>>打开确认
2.4 添加组件
- 创建窗体>>添加图中组件;
- 其中的videoSorcePlayer位于刚添加的工具集AForge中。
- 我的设计效果
2.5 代码:
可以同时参考前言中第四位作者的代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using System.Drawing;
using AForge.Video.DirectShow;
using 工厂物资管理.Face;
using Newtonsoft.Json;
using System.Data.SqlClient;
namespace 工厂物资管理
{
public partial class FaceLoginForm : Form
{
private FilterInfoCollection videoDevices = null;
private VideoCaptureDevice videoSource = null;
private string EmployeeID = null;
private int Indexof = 0;
private SqlConnection sqlConnection = null;
private SqlCommand sqlCommand = null;
public FaceLoginForm()
{
InitializeComponent()