/
CameraController.cs
49 lines (41 loc) · 1.46 KB
/
CameraController.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
using AForge.Video;
using AForge.Video.DirectShow;
public class CameraController
{
private FilterInfoCollection filterInfoCollection;
private VideoCaptureDevice videoCaptureDevice;
private PictureBox pictureBox;
private ComboBox comboBox;
public CameraController(PictureBox pictureBox, ComboBox comboBox)
{
this.pictureBox = pictureBox;
this.comboBox = comboBox;
PopulateCameraComboBox();
}
public void StartCameraCapture() {
videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox.SelectedIndex].MonikerString);
videoCaptureDevice.NewFrame += FinalFrame_NewFrame;
videoCaptureDevice.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void PopulateCameraComboBox()
{
filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in filterInfoCollection)
comboBox.Items.Add(Device.Name);
comboBox.SelectedIndex = 0;
videoCaptureDevice = new VideoCaptureDevice();
}
public void StopCameraCapture()
{
if (videoCaptureDevice != null && videoCaptureDevice.IsRunning)
{
videoCaptureDevice.SignalToStop();
videoCaptureDevice.NewFrame -= FinalFrame_NewFrame;
videoCaptureDevice = null;
}
}
}