AForge第一次学习用c#打开摄像头,读取摄像头拍照保存
由于网课需要上传照片和视频,电脑拍摄的视频通常不是mp4格式。线上作业提交视频时间又很紧张,如何能快速拍照,录像快速上传是我想要寻找的方法。开始用codeblock 配置opencv,两天才配置成功。方法是:看B站视频( https://www.bilibili.com/video/BV124411X7e8)配制方法,并下载了别人博客网盘的文件(网盘:https://pan.baidu.com/s/1va25YLPBkpK6QF4xwqMbuw 提取码:sju2)已解决opencv4.2国外网站下载不了的那三个问题。结果没找到opencv 处理视频的学习资料,只学了些处理图片的资料,于是放弃。
网上发现了AForge捕获视频的代码于是改为c#
在VS2019资源管理器—>项目右键—>管理Nuge程序包中寻找项目AForge。基本方法按照B站视频做的(https://www.bilibili.com/video/BV1hE411M7LV?from=search&seid=7116930515414706113)。
和视频中代码差不多,改了一小部分,代码如下:
代码片
.
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;
namespace test_video1
{
public partial class Form_video_player : Form
{
private FilterInfoCollection videoDevices; //定义设备列表
private VideoCaptureDevice videoSouse; //定义数据源
private VideoCapabilities[] videoCapabilities; //摄像头分辨率
public Form_video_player()
{
InitializeComponent();
}
/*
初始化窗口和摄像头设备
/
private void Form_video_player_Load(object sender, EventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //读取设备列表
if (videoDevices.Count != 0) //如果设备数量不为零
{
foreach (FilterInfo device in videoDevices)
{
comboBox_list_device.Items.Add(device.Name); //把摄像设备添加到摄像列表中
}
}
else
{
comboBox_list_device.Items.Add(“没有找到摄像头”);
}
comboBox_list_device.SelectedIndex = 0;//默认选择第一个
EnableControlStatus(true);
}
/*
连接视频
/
private void button_openvideo_Click(object sender, EventArgs e)
{
if (videoSouse != null)//如果摄像头不为空
{
if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
{
videoSouse.VideoResolution = videoCapabilities[comboBox_resolution.SelectedIndex];//摄像头分辨率
videoSourcePlayer1.VideoSource = videoSouse;//把摄像头赋给控件
videoSourcePlayer1.Start();//开启摄像头
EnableControlStatus(false);
this.button_select_photo.Enabled = false;
}
}
}
private void button_closevideo_Click(object sender, EventArgs e)
{
DisConnect();//断开连接,后边有函数实现
EnableControlStatus(true); //后边有控件判断逻辑实现
}
private void button_take_a_picture_Click(object sender, EventArgs e)
{
Bitmap imgleft = videoSourcePlayer1.GetCurrentVideoFrame();
this.pictureBox.Image = imgleft;
this.button_select_photo.Enabled = true;
}
/**选择并保存照片*/
private void button_select_photo_Click(object sender, EventArgs e)
{
string photo_time_name = DateTime.Now.ToString("yyyyMMddHHmmss");
string path_name = @"C:\Users\Administrator\Desktop\" + "照片" + photo_time_name + ".jpg";
this.pictureBox.Image.Save(path_name);
}
private void DisConnect() //断开连接函数。
{
if (videoSourcePlayer1.VideoSource != null)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.VideoSource = null;
}
}
private void EnableControlStatus(bool status) //控件激活逻辑判断函数。
{
this.comboBox_list_device.Enabled = status;
this.comboBox_resolution.Enabled = status;
this.button_openvideo.Enabled = status;
this.button_closevideo.Enabled = !status;
this.button_take_a_picture.Enabled = !status;
this.button_select_photo.Enabled = !status;
}
/**读取分辨率列表*/
private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
{
comboBox_resolution.Items.Clear();//清空列表
videoCapabilities = videoCaptureDevice.VideoCapabilities;//设备的摄像头分辨率数组
foreach (VideoCapabilities capabilty in videoCapabilities)
{
//把这个设备的所有分辨率添加到列表
comboBox_resolution.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
}
comboBox_resolution.SelectedIndex = 0;//默认选择第一个
}
private void comboBox_resolution_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox_list_device_SelectedIndexChanged(object sender, EventArgs e)
{
if (videoDevices.Count != 0)
{
//获取摄像头
videoSouse = new VideoCaptureDevice(videoDevices[comboBox_list_device.SelectedIndex].MonikerString);
GetDeviceResolution(videoSouse);//获得摄像头的分辨率
}
}
/*
关闭时释放资源
/
private void Form_video_player_FormClosing(object sender, FormClosingEventArgs e)
{
DisConnect();
}
}
}