程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> winform實例(3)-利用攝像頭進行拍照,winform實例

winform實例(3)-利用攝像頭進行拍照,winform實例

編輯:關於.NET

winform實例(3)-利用攝像頭進行拍照,winform實例


前言:

由於先前用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         }
View Code

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         }
View Code

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         }
View Code

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 }
View Code

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved