运行效果
调用摄像头
开发环境
- Visual Studio 2022
安装Aforge库
- 下载后续用到的库
- 下载后就可以以下地方找到这些了
关于AForge库
详见:http://www.aforgenet.com/framework/docs/html/077a7afb-c9bd-91b6-6870-61440e2f4060.htm
- VideoCaptureDevice的方法:
摄像头应用
窗体设计
- 2个Label控件,2个combox控件,4个button控件,2个videosuorceplayer控件
参数调整
//控件“参数调整”
if ((videodevice != null) && (videodevice is VideoCaptureDevice)) //检测是否有摄像头并判断它是否属于 VideoCaptureDevice
{
try
{
((VideoCaptureDevice)videodevice).DisplayPropertyPage(this.Handle); //DisplayPropertyPage:获得摄像头设备的属性窗口
}
catch (NotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
连接
- 该代码有点冗余,但可以通过下拉框控制具体的摄像头
if (videoSourcePlayer1.IsRunning == false) //判断播放框是否被占用
{
if(videodevice != null)
{
if ((capabilities != null) && (capabilities.Length != 0)) //检测对应摄像头是否有可以调整的像素大小
{
videodevice.VideoResolution = capabilities[comboBox2.SelectedIndex];
videoSourcePlayer1.VideoSource = videodevice;
videoSourcePlayer1.Start();
}
}
}
else if ((videoSourcePlayer2.IsRunning == false) && (videoSourcePlayer1.IsRunning == true))
{
if (videodevice != null)
{
if ((capabilities != null) && (capabilities.Length != 0))
{
videodevice.VideoResolution = capabilities[comboBox2.SelectedIndex];
videoSourcePlayer2.VideoSource = videodevice;
videoSourcePlayer2.Start();
}
}
}
关闭
videoSourcePlayer1.SignalToStop(); //停止播放的信号
videoSourcePlayer2.SignalToStop();
videoSourcePlayer1.WaitForStop(); //等待播放器关闭
videoSourcePlayer2.WaitForStop();
videodevice.Stop(); //结束进程
拍照
if(videodevices.Count != 0)
{
// “拍照”按键代码
DateTime now = DateTime.Now; //获取系统时间
string name = now.ToString("yyyy-MM-dd-HH-mm-ss-ff"); //转化所需时间格式
string path1 = "D:\\image\\" + name + "-A-" + ".bmp";
string path2 = "D:\\image\\" + name + "-B-" + ".bmp";
Bitmap img1 = videoSourcePlayer1.GetCurrentVideoFrame(); //保存视频流帧
Bitmap img2 = videoSourcePlayer2.GetCurrentVideoFrame(); //保存视频流帧
img1.Save(path1);
img2.Save(path2);
MessageBox.Show("图片保存成功!");
}
分辨率
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (videodevices.Count != 0)
{
videodevice = new VideoCaptureDevice(videodevices[comboBox1.SelectedIndex].MonikerString); //从combox1的下拉框选中摄像头
GetDeviceResolution(videodevice);
}
}
private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice) //获取摄像头对应的分辨率
{
comboBox2.Items.Clear();
capabilities = videoCaptureDevice.VideoCapabilities;
foreach (VideoCapabilities capabilty in capabilities)
{
comboBox2.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
comboBox2.SelectedIndex = 0;
}
完整代码
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace double_camera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection videodevices; //定义检索到的所有设备名
private VideoCaptureDevice videodevice; //定义设备源
private VideoCapabilities[] capabilities; //设置分辨率组
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //定义设备
//检测摄像头
if (videodevices.Count != 0)
{
foreach (FilterInfo device in videodevices) //循环从电脑上检测摄像头
comboBox1.Items.Add(device.Name);
}
else
comboBox1.Items.Add("没有找到摄像头");
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e) //“连接”控件功能
{
if (videoSourcePlayer1.IsRunning == false) //判断播放框是否被占用
{
if(videodevice != null)
{
if ((capabilities != null) && (capabilities.Length != 0)) //检测对应摄像头是否有可以调整的像素大小
{
videodevice.VideoResolution = capabilities[comboBox2.SelectedIndex];
videoSourcePlayer1.VideoSource = videodevice;
videoSourcePlayer1.Start();
}
}
}
else if ((videoSourcePlayer2.IsRunning == false) && (videoSourcePlayer1.IsRunning == true))
{
if (videodevice != null)
{
if ((capabilities != null) && (capabilities.Length != 0))
{
videodevice.VideoResolution = capabilities[comboBox2.SelectedIndex];
videoSourcePlayer2.VideoSource = videodevice;
videoSourcePlayer2.Start();
}
}
}
}
private void button2_Click(object sender, EventArgs e) //“关闭”控件功能
{
videoSourcePlayer1.SignalToStop(); //停止播放的信号
videoSourcePlayer2.SignalToStop();
videoSourcePlayer1.WaitForStop(); //等待播放器关闭
videoSourcePlayer2.WaitForStop();
videodevice.Stop(); //结束进程
}
private void button3_Click(object sender, EventArgs e) //“参数调整”控件功能
{
//控件“参数调整”
if ((videodevice != null) && (videodevice is VideoCaptureDevice)) //检测是否有摄像头并判断它是否属于 VideoCaptureDevice
{
try
{
((VideoCaptureDevice)videodevice).DisplayPropertyPage(this.Handle); //DisplayPropertyPage:获得摄像头设备的属性窗口
}
catch (NotSupportedException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (videodevices.Count != 0)
{
videodevice = new VideoCaptureDevice(videodevices[comboBox1.SelectedIndex].MonikerString); //从combox1的下拉框选中摄像头
GetDeviceResolution(videodevice);
}
}
private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice) //获取摄像头对应的分辨率
{
comboBox2.Items.Clear();
capabilities = videoCaptureDevice.VideoCapabilities;
foreach (VideoCapabilities capabilty in capabilities)
{
comboBox2.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
comboBox2.SelectedIndex = 0;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videodevice.Stop();
}
private void button4_Click(object sender, EventArgs e) //“拍照”控件功能
{
if(videodevices.Count != 0)
{
// “拍照”按键代码
DateTime now = DateTime.Now; //获取系统时间
string name = now.ToString("yyyy-MM-dd-HH-mm-ss-ff"); //转化所需时间格式
string path1 = "D:\\image\\" + name + "-A-" + ".bmp";
string path2 = "D:\\image\\" + name + "-B-" + ".bmp";
Bitmap img1 = videoSourcePlayer1.GetCurrentVideoFrame(); //保存视频流帧
Bitmap img2 = videoSourcePlayer2.GetCurrentVideoFrame(); //保存视频流帧
img1.Save(path1);
img2.Save(path2);
MessageBox.Show("图片保存成功!");
}
}
}
}