前言:
由于先前用python+opencv做了一会儿人脸识别。(其实是别人做的,我是打酱油的)。
用winform做了当时用的一个功能界面。打开摄像头并进行拍照保存。
界面预览:
此次,利用的是winform+AForge框架。AForge是全过程都在用的,必须要有。
介绍一下制作过程:
1.创建项目-->引用下载的AForge,是.dll文件类型。
2.开始实现功能
1)显示视频输入设备:
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 try 4 { 5 // 枚举所有视频输入设备 6 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 7 8 if (videoDevices.Count == 0) 9 throw new ApplicationException(); 10 11 foreach (FilterInfo device in videoDevices) 12 { 13 tscbxCameras.Items.Add(device.Name); 14 } 15 16 tscbxCameras.SelectedIndex = 0; 17 18 } 19 catch (ApplicationException) 20 { 21 tscbxCameras.Items.Add("No local capture devices"); 22 videoDevices = null; 23 } 24 }
2)打开摄像头:
先对对象进行实例化,代码:
FilterInfoCollection videoDevices; VideoCaptureDevice videoSource; public int selectedDeviceIndex = 0;
然后功能:
1 //打开摄像头 2 private void button1_Click_1(object sender, EventArgs e) 3 { 4 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 5 selectedDeviceIndex = 0; 6 videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//连接摄像头。 7 videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex]; 8 videoSourcePlayer1.VideoSource = videoSource; 9 // set NewFrame event handler 10 videoSourcePlayer1.Start(); 11 }
3)关闭功能:
有两个关闭方式:
一个点击关闭按钮:
1 //关闭摄像头 2 private void button3_Click(object sender, EventArgs e) 3 { 4 videoSourcePlayer1.SignalToStop(); 5 videoSourcePlayer1.WaitForStop(); 6 }
一个关闭窗口时关闭
1 //关闭窗体时进行的事件 2 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 3 { 4 videoSourcePlayer1.SignalToStop(); 5 videoSourcePlayer1.WaitForStop(); 6 }
4)拍照
1 //拍照 2 private void button2_Click(object sender, EventArgs e) 3 { 4 if (videoSource == null) 5 return; 6 Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame(); 7 string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff") + ".jpg"; 8 bitmap.Save(@"C:" + fileName, ImageFormat.Jpeg); 9 bitmap.Dispose(); 10 }
其中,可以自定义保存路径和文件名称。 @是强制不转义的意思。
全部代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using AForge; 11 using AForge.Controls; 12 using AForge.Imaging; 13 using AForge.Video; 14 using AForge.Video.DirectShow; 15 using System.Drawing.Imaging; 16 17 18 namespace camera 19 { 20 public partial class Form1 : Form 21 { 22 public Form1() 23 { 24 InitializeComponent(); 25 } 26 //初始化 27 FilterInfoCollection videoDevices; 28 VideoCaptureDevice videoSource; 29 public int selectedDeviceIndex = 0; 30 31 32 //打开摄像头 33 private void button1_Click_1(object sender, EventArgs e) 34 { 35 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 36 selectedDeviceIndex = 0; 37 videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//连接摄像头。 38 videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex]; 39 videoSourcePlayer1.VideoSource = videoSource; 40 // set NewFrame event handler 41 videoSourcePlayer1.Start(); 42 } 43 //拍照 44 private void button2_Click(object sender, EventArgs e) 45 { 46 if (videoSource == null) 47 return; 48 Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame(); 49 string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff") + ".jpg"; 50 bitmap.Save(@"C:" + fileName, ImageFormat.Jpeg); 51 bitmap.Dispose(); 52 } 53 //关闭窗体时进行的事件 54 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 55 { 56 videoSourcePlayer1.SignalToStop(); 57 videoSourcePlayer1.WaitForStop(); 58 } 59 60 private void Form1_Load(object sender, EventArgs e) 61 { 62 try 63 { 64 // 枚举所有视频输入设备 65 videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); 66 67 if (videoDevices.Count == 0) 68 throw new ApplicationException(); 69 70 foreach (FilterInfo device in videoDevices) 71 { 72 tscbxCameras.Items.Add(device.Name); 73 } 74 75 tscbxCameras.SelectedIndex = 0; 76 77 } 78 catch (ApplicationException) 79 { 80 tscbxCameras.Items.Add("No local capture devices"); 81 videoDevices = null; 82 } 83 } 84 //关闭摄像头 85 private void button3_Click(object sender, EventArgs e) 86 { 87 videoSourcePlayer1.SignalToStop(); 88 videoSourcePlayer1.WaitForStop(); 89 } 90 91 92 } 93 }