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 ...
随机推荐
- VS Code:让你工作效率翻倍的23个插件和23个编辑技巧
VS Code:让你工作效率翻倍的23个插件和23个编辑技巧 总结了一些平时常用且好用的 VS Code 的插件和编辑技巧分享出来. 文章详情可查阅我的博客:lishaoy.net ,欢迎大家访问. ...
- Python内置常量
引言 Python内置的常量不多,只有6个,分别是True.False.None.NotImplemented.Ellipsis.__debug__. 一. True 1. True是bool类型用来 ...
- Kotlin 最佳实践
为什么写此文 Kotlin很烦,Gralde很烦,还都是升级狂,加一块更烦.几个月不接触Kotlin,再次上手时便一片迷茫.所以记录此文,以便再次上手时查阅. 使用Gradle创建Kotlin项目 m ...
- [java初探09]__关于java的包装类
前言 在Java语言的学习过程中,我们逐渐的理解了Java面向对象的思想,与类和对象的应用.但是在基本数据类型的使用上,我们无法将其定义为一个对象,通过使用对象的方法来使用它们,但是Java语言的思想 ...
- Shell脚本 | 健壮性测试之空指针检查
通过 "adb shell am start" 遍历安卓应用所有的 Activity,可以检查是否存在空指针的情况. 以下为梳理后的测试流程: 通过 apktool 反编译 apk ...
- 以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ
前言 首先我们在java环境中使用了ActiveMQ,然后我们又在Spring中使用了ActiveMQ 本来这样已经可以了,但是最近SpringBoot也来了.所以在其中也需要使用试试. 可以提前透露 ...
- Spring Boot + Spring Cloud 构建微服务系统(三):服务消费和负载(Feign)
Spring Cloud Feign Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口 ...
- Eureka多机高可用
线上Eureka高可用集群,至少三个节点组成一个集群,推荐部署在不同的服务器上,IP用域名绑定,端口保持一致. 10.1.22.26:876210.1.22.27:876210.1.22.28:876 ...
- leetcode — substring-with-concatenation-of-all-words
import java.util.*; /** * Source : https://oj.leetcode.com/problems/substring-with-concatenation-of- ...
- Netty精粹之玩转NIO缓冲区
摘要: 在JAVA NIO相关的组件中,ByteBuffer是除了Selector.Channel之外的另一个很重要的组件,它是直接和Channel打交道的缓冲区,通常场景或是从ByteBuffer写 ...