-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
154 lines (138 loc) · 4.41 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 Emgu;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;
using Emgu.CV.UI;
using DirectShowLib;
namespace Web_camera
{
public partial class Form1 : Form
{
private VideoCapture capture = null;
private DsDevice[] webCams = null;
private int selevtedCameraid = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webCams = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
for(int i=0; i<webCams.Length;i++)
{
toolStripTextBox1.Items.Add(webCams[i].Name);
}
}
private void toolStripTextBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selevtedCameraid = toolStripTextBox1.SelectedIndex;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{
if(webCams.Length ==0)
{
throw new Exception("Нет доступных камер!");
}
else if(toolStripTextBox1.SelectedItem ==null)
{
throw new Exception("Необходимо выбрать камеру!");
}
else if(capture !=null)
{
capture.Start();
}
else
{
capture = new VideoCapture(selevtedCameraid);
capture.ImageGrabbed += Capture_ImageGrabbed;
capture.Start();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Capture_ImageGrabbed(object sender, EventArgs e)
{
try
{
Mat m = new Mat();
capture.Retrieve(m);
pictureBox1.Image = m.ToImage<Bgr, byte>().Flip(Emgu.CV.CvEnum.FlipType.Horizontal).AsBitmap<Bgr,byte>();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
try
{
if (capture != null)
capture.Pause();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
try
{
if (capture != null)
{
capture.Pause();
capture.Dispose();
capture = null;
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
selevtedCameraid = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
if(capture ==null)
{
MessageBox.Show("Запустите камеру!");
return;
}
try
{
Mat m = new Mat();
capture.Retrieve(m);
MakeScreen makeScreenForm = new MakeScreen(m.ToImage<Bgr, byte>().Flip(Emgu.CV.CvEnum.FlipType.Horizontal));
makeScreenForm.ShowDialog();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
Form3 Form3 = new Form3();
Form3.ShowDialog();
}
}
}