C#使用Aforge调用摄像头拍照
一、 新建一个Winform项目
二、使用Nuget添加引用

安装下图中红色框住的两个程序包

安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖。

三、编写程序
1. 窗体设计,摄像头是下拉列表(cmbCamera,控件命名,下同),虽然示例只用到一个摄像头,但是该Demo可用于多个摄像头间切换场景,分辨率是下拉列表(cmbResolution),列出摄像头所支持的分辨率,一个VideoSourcePlayer控件(vispShoot),一个PictureBox控件(picbPreview)。

2. 编写代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video.DirectShow; namespace AforgeDemo
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoDevice;
private VideoCapabilities[] videoCapabilities;
private VideoCapabilities[] snapshotCapabilities;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != )
{
foreach (FilterInfo device in videoDevices)
{
cmbCamera.Items.Add(device.Name);
}
}
else
{
cmbCamera.Items.Add("没有找到摄像头");
} cmbCamera.SelectedIndex = ;
} private void cmbCamera_SelectedIndexChanged(object sender, EventArgs e)
{
if (videoDevices.Count != )
{
videoDevice = new VideoCaptureDevice(videoDevices[cmbCamera.SelectedIndex].MonikerString);
GetDeviceResolution(videoDevice);
}
} private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
{
cmbResolution.Items.Clear();
videoCapabilities = videoCaptureDevice.VideoCapabilities;
foreach (VideoCapabilities capabilty in videoCapabilities)
{
cmbResolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
cmbResolution.SelectedIndex = ;
} private void btnConnect_Click(object sender, EventArgs e)
{
if (videoDevice != null)
{
if ((videoCapabilities != null) && (videoCapabilities.Length != ))
{
videoDevice.VideoResolution = videoCapabilities[cmbResolution.SelectedIndex]; vispShoot.VideoSource = videoDevice;
vispShoot.Start();
EnableControlStatus(false);
}
}
} private void EnableControlStatus(bool status)
{
cmbCamera.Enabled = status;
cmbResolution.Enabled = status;
btnConnect.Enabled = status;
btnShoot.Enabled = !status;
btnDisconnect.Enabled = !status;
} private void btnDisconnect_Click(object sender, EventArgs e)
{
DisConnect();
EnableControlStatus(true);
} private void DisConnect()
{
if (vispShoot.VideoSource != null)
{
vispShoot.SignalToStop();
vispShoot.WaitForStop();
vispShoot.VideoSource = null;
}
} private void btnShoot_Click(object sender, EventArgs e)
{
Bitmap img = vispShoot.GetCurrentVideoFrame();
picbPreview.Image = img;
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DisConnect();
}
}
}
3. 测试

附上窗体设计代码:
namespace AforgeDemo
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.cmbCamera = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.cmbResolution = new System.Windows.Forms.ComboBox();
this.vispShoot = new AForge.Controls.VideoSourcePlayer();
this.picbPreview = new System.Windows.Forms.PictureBox();
this.btnConnect = new System.Windows.Forms.Button();
this.btnDisconnect = new System.Windows.Forms.Button();
this.btnShoot = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.picbPreview)).BeginInit();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "摄像头:";
//
// cmbCamera
//
this.cmbCamera.FormattingEnabled = true;
this.cmbCamera.Location = new System.Drawing.Point(, );
this.cmbCamera.Name = "cmbCamera";
this.cmbCamera.Size = new System.Drawing.Size(, );
this.cmbCamera.TabIndex = ;
this.cmbCamera.SelectedIndexChanged += new System.EventHandler(this.cmbCamera_SelectedIndexChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "分辨率:";
//
// cmbResolution
//
this.cmbResolution.FormattingEnabled = true;
this.cmbResolution.Location = new System.Drawing.Point(, );
this.cmbResolution.Name = "cmbResolution";
this.cmbResolution.Size = new System.Drawing.Size(, );
this.cmbResolution.TabIndex = ;
//
// vispShoot
//
this.vispShoot.Location = new System.Drawing.Point(, );
this.vispShoot.Name = "vispShoot";
this.vispShoot.Size = new System.Drawing.Size(, );
this.vispShoot.TabIndex = ;
this.vispShoot.Text = "videoSourcePlayer1";
this.vispShoot.VideoSource = null;
//
// picbPreview
//
this.picbPreview.Location = new System.Drawing.Point(, );
this.picbPreview.Name = "picbPreview";
this.picbPreview.Size = new System.Drawing.Size(, );
this.picbPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.picbPreview.TabIndex = ;
this.picbPreview.TabStop = false;
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(, );
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(, );
this.btnConnect.TabIndex = ;
this.btnConnect.Text = "连接";
this.btnConnect.UseVisualStyleBackColor = true;
this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
//
// btnDisconnect
//
this.btnDisconnect.Enabled = false;
this.btnDisconnect.Location = new System.Drawing.Point(, );
this.btnDisconnect.Name = "btnDisconnect";
this.btnDisconnect.Size = new System.Drawing.Size(, );
this.btnDisconnect.TabIndex = ;
this.btnDisconnect.Text = "断开";
this.btnDisconnect.UseVisualStyleBackColor = true;
this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);
//
// btnShoot
//
this.btnShoot.Enabled = false;
this.btnShoot.Location = new System.Drawing.Point(, );
this.btnShoot.Name = "btnShoot";
this.btnShoot.Size = new System.Drawing.Size(, );
this.btnShoot.TabIndex = ;
this.btnShoot.Text = "拍照";
this.btnShoot.UseVisualStyleBackColor = true;
this.btnShoot.Click += new System.EventHandler(this.btnShoot_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.btnShoot);
this.Controls.Add(this.btnDisconnect);
this.Controls.Add(this.btnConnect);
this.Controls.Add(this.picbPreview);
this.Controls.Add(this.vispShoot);
this.Controls.Add(this.cmbResolution);
this.Controls.Add(this.cmbCamera);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.picbPreview)).EndInit();
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox cmbCamera;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cmbResolution;
private AForge.Controls.VideoSourcePlayer vispShoot;
private System.Windows.Forms.PictureBox picbPreview;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Button btnDisconnect;
private System.Windows.Forms.Button btnShoot;
}
}
C#使用Aforge调用摄像头拍照的更多相关文章
- AForge调用摄像头拍照时设置分辨率
简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...
- C# - VS2019调用AForge库实现调用摄像头拍照功能
前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...
- android: 调用摄像头拍照
很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...
- vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式
进入正题 1. PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template> <div> &l ...
- Cordova - 使用Cordova开发iOS应用实战4(调用摄像头拍照,并编辑)
使用Cordova可以很方便的通过js代码来使用设备摄像头拍照,只需把camera插件添加进来即可. 一,添加camera插件 首先我们要在“终端”中进入工程所在的目录,然后运行如下命令: 1 cor ...
- 【MediaKit】WPF项目中 调用摄像头拍照的开发包
今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相.如何解决这个问题呢? 介绍一个开发包给你,MediaKit.论坛里头的人都说好,但是黑兔觉得大家好才是真的好.你不妨试试~ 第一步:添加WPF ...
- 使用js调用摄像头拍照
在一些浏览器里已经可以使用web api调用摄像头功能了. 基于此可以经行拍照摄像功能,网上找了些资料,然后实现了简单的拍照功能 演示地址 bingxl.cn/webrtc.html 代码 <! ...
- html5中调用摄像头拍照
方法: getCamera: 获取摄像头管理对象 对象: Camera: 摄像头对象 CameraOption: JSON对象.调用摄像头的參数 PopPosition: JSON对象,弹出拍照或摄像 ...
- android ——调用摄像头拍照和相册
先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
随机推荐
- 解决Eclipse中DDMS一直打印输出Connection attempts的问题
Eclipse/MyEclipse出现以下错误的解决方案: [2015-01-25 16:10:29 - DeviceMonitor] Adb connection Error:远程主机强迫关闭了一个 ...
- 从零开始学 Web 之 BOM(一)BOM的概念,一些BOM对象
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...
- Kafka实战-Kafka到Storm
1.概述 在<Kafka实战-Flume到Kafka>一文中给大家分享了Kafka的数据源生产,今天为大家介绍如何去实时消费Kafka中的数据.这里使用实时计算的模型——Storm.下面是 ...
- Java中锁分类
锁的分类大致如下:公平锁/非公平锁可重入锁/不可重入锁独享锁/共享锁乐观锁/悲观锁分段锁 1.公平锁/非公平锁公平锁就是严格按照线程启动的顺序来执行的,不允许其他线程插队执行的:而非公平锁是允许插队的 ...
- GTest的安装与使用
安装GTest 1.安装源代码 下载gtest,release-1.8.0 git clone https://github.com/google/googletest gtest编译 cd goog ...
- 利用channel在goroutins之间控制同步和传递数据
在java等代码中,我们查询数据库的操作: sql = "select * from ...."; result = db.query(sql) for(item in resul ...
- jenkins+Android+gradle持续集成
本文Android自动化打包采用jenkins+gradle+upload to pyger的方式来实现,job执行完后只需要打开链接扫描二维码即可下载apk. 一.环境准备 1.下载Android ...
- SVN多项目并行版本管理解决方案
1.背景 随着公司业务拓展,各业务部门频繁的需求变更,导致系统集成冲突的问题日益突出. 2.现状 基于SVN版本管理模式,多分支版本并行,分支合并主干交付.多分支开发存在依赖关系且有交付的先后顺序, ...
- kafka安装与简单使用
一.kafka安装 安装是非常简单的,现在推荐安装0.8的版本,这个版本是非常稳定的,而且公司里面也多用此版本. 简单的安装: 这个是我使用的版本,kafka_2.11-0.8.2.2.tgz 直接t ...
- JavaScript复杂判断的更优雅写法
摘要: 写代码是一门艺术. 原文:JavaScript 复杂判断的更优雅写法 作者:Think. 公众号:大转转fe Fundebug经授权转载,版权归原作者所有. 前提 我们编写js代码时经常遇到复 ...