目录
准备工作
组件
源代码
程序压缩包
开发环境: Visual Studio 2022 .NET Freamwork 4.7.1(这个框架并无特殊要求,都行)
因为需要用到Aforge相关程序包,这个需要下载并添加到项目中
这样就好了
需要用到的组件如图所示,例如,链接按钮,命名为button1。
using AForge.Video.DirectShow;
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using AForge.Video;
using AForge.Controls;
using System.Threading.Tasks;namespace WindowsFormsApp1
{public partial class Form : System.Windows.Forms.Form{FolderBrowserDialog folderBrowser = new FolderBrowserDialog();private FilterInfoCollection videoDevices;//所有摄像设备private string savePath;private string folderPath;private Timer timer;private DateTime startTime;private DateTime endTime;private TimeSpan interval;private int photoCount=1;private VideoCaptureDevice videoDevice;//摄像设备private VideoCapabilities[] videoCapabilities;//摄像头分辨率public Form(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){timer = new Timer();timer.Tick += new EventHandler(Timer_Tick);videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//得到机器所有接入的摄像设备if (videoDevices.Count != 0)//读取到摄像设备{foreach (FilterInfo device in videoDevices){cboVideo.Items.Add(device.Name);//把摄像设备添加到摄像列表中}}else{cboVideo.Items.Add("没有找到摄像头");}cboVideo.SelectedIndex = 0;//默认选择第一个textBox4.Text += folderPath ;if (videoDevices.Count != 0)//读取到摄像设备{//获取摄像头videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);GetDeviceResolution(videoDevice);//获得摄像头的分辨率}}private void Timer_Tick(object sender, EventArgs e){// 拍摄照片Image photo = videoSourcePlayer1.GetCurrentVideoFrame();pictureBox1.Image = photo;// 保存照片到指定路径string savePath = Path.Combine(folderPath, "time lapse "+photoCount.ToString() + ".jpg");photo.Save(savePath);photoCount++;// 检查是否达到结束时间,如果是则停止拍摄if (DateTime.Now >= endTime){button2_Click(sender,e);timer.Stop();videoSourcePlayer1.SignalToStop();}}private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice){cboResolution.Items.Clear();//清空列表videoCapabilities = videoCaptureDevice.VideoCapabilities;//设备的摄像头分辨率数组foreach (VideoCapabilities capabilty in videoCapabilities){//把这个设备的所有分辨率添加到列表cboResolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");}cboResolution.SelectedIndex = 0;//默认选择第一个}private void button1_Click(object sender, EventArgs e){if (videoDevice != null)//如果摄像头不为空{videoDevice.VideoResolution = videoCapabilities[cboResolution.SelectedIndex];//摄像头分辨率videoSourcePlayer1.VideoSource = videoDevice;//把摄像头赋给控件videoSourcePlayer1.Start();//开启摄像头EnableControlStatus(false);}else{MessageBox.Show("没有找到摄像头");}}private void EnableControlStatus(bool status){cboVideo.Enabled = status;cboResolution.Enabled = status;button1.Enabled = status;button2.Enabled = !status;button3.Enabled = status;}private void button4_Click(object sender, EventArgs e){DialogResult result = folderBrowser.ShowDialog();// 检查用户是否选择了文件夹if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath)){// 用户选择的文件夹路径folderPath = folderBrowser.SelectedPath;// 构造保存路径// 显示保存成功的消息MessageBox.Show("路径设置成功!");textBox4.Text += folderPath;}}private void button2_Click(object sender, EventArgs e){DisConnect();//断开连接EnableControlStatus(true);}private void DisConnect(){if (videoSourcePlayer1.VideoSource != null){videoSourcePlayer1.SignalToStop();videoSourcePlayer1.WaitForStop();videoSourcePlayer1.VideoSource = null;}}private void Form1_FormClosing(object sender, FormClosingEventArgs e){EnableControlStatus(true);DisConnect();//关闭并释放if (timer != null){timer.Stop();}}private void textBox1_TextChanged(object sender, EventArgs e){}private void videoSourcePlayer1_Click(object sender, EventArgs e){}private async void button5_Click(object sender, EventArgs e){if (folderPath == null){MessageBox.Show("未设置保存路径!点击确定以设置");DialogResult result = folderBrowser.ShowDialog();// 检查用户是否选择了文件夹if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowser.SelectedPath)){// 用户选择的文件夹路径folderPath = folderBrowser.SelectedPath;// 构造保存路径textBox4.Text += folderPath;// 显示保存成功的消息MessageBox.Show("路径设置成功!");}}// 获取用户输入的开始时间、结束时间和时间间隔startTime = dateTimePicker2.Value;endTime = dateTimePicker3.Value;interval = TimeSpan.FromSeconds((double)numericUpDown1.Value*1000);// 启动定时器开始拍摄photoCount = 1;TimeSpan timeRemaining = startTime - DateTime.Now;if (timeRemaining.TotalMilliseconds <= 0){timer.Start(); // 启动计时器}else{// 等待至开始时间await Task.Delay((int)(startTime - DateTime.Now).TotalMilliseconds);timer.Start(); // 启动计时器}}private void label1_Click(object sender, EventArgs e){}private void timer1_Tick(object sender, EventArgs e){}private void numericUpDown1_ValueChanged(object sender, EventArgs e){timer.Interval = Convert.ToInt32(numericUpDown1.Value) * 1000;}private void button3_Click(object sender, EventArgs e){cboVideo.Items.Clear();//清空列表videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);if (videoDevices.Count != 0)//读取到摄像设备{foreach (FilterInfo device in videoDevices){cboVideo.Items.Add(device.Name);//把摄像设备添加到摄像列表中}}else{cboVideo.Items.Add("没有找到摄像头");}if (videoDevices.Count != 0)//读取到摄像设备{cboVideo.SelectedIndex = 0;//默认选择第一个//获取摄像头videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);GetDeviceResolution(videoDevice);//获得摄像头的分辨率}}private void cboVideo_SelectedIndexChanged(object sender, EventArgs e){// 获取选中的摄像设备videoDevice = new VideoCaptureDevice(videoDevices[cboVideo.SelectedIndex].MonikerString);// 更新cboResolution的选项GetDeviceResolution(videoDevice);// 默认选择第一个分辨率cboResolution.SelectedIndex = 0;}private void cboResolution_SelectedIndexChanged(object sender, EventArgs e){videoDevice.VideoResolution = videoCapabilities[cboResolution.SelectedIndex];}private void button6_Click(object sender, EventArgs e){DialogResult result = MessageBox.Show("未保存数据都将丢失。确定要继续吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);// 根据用户选择执行相应的操作if (result == DialogResult.OK){// 用户点击了确定按钮,执行相应的操作timer.Stop();videoSourcePlayer1.Stop();DisConnect();Application.Restart();}}}
}
软件界面
软件链接:=brhc
提取码:brhc
本文发布于:2024-01-29 03:06:40,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170646880312256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |