C#使用Aforge调用摄像头拍照的方法-创新互联
本文实例为大家分享了C#使用Aforge调用摄像头拍照的具体代码,供大家参考,具体内容如下
创新互联建站长期为近千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为延安企业提供专业的成都网站制作、成都网站建设,延安网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。一、新建一个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 != 0)
{
foreach (FilterInfo device in videoDevices)
{
cmbCamera.Items.Add(device.Name);
}
}
else
{
cmbCamera.Items.Add("没有找到摄像头");
}
cmbCamera.SelectedIndex = 0;
}
private void cmbCamera_SelectedIndexChanged(object sender, EventArgs e)
{
if (videoDevices.Count != 0)
{
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 = 0;
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (videoDevice != null)
{
if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
{
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();
}
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:C#使用Aforge调用摄像头拍照的方法-创新互联
网页网址:http://www.jxjierui.cn/article/cecgds.html


咨询
建站咨询
