-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
170 lines (154 loc) · 5.44 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Xml.Serialization;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private VideoCaptureDevice VCD;
private string ImagePath;
private int Interval = 60000;
private string HomePath = Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().Location);
private Dictionary<string, string> MonikerMap = new Dictionary<string, string>();
//system.timers.timer code
private bool GrabImage = true;
System.Timers.Timer ImageCaptureTimer;
public Form1()
{
InitializeComponent();
//default image path
ImagePath = HomePath + @"\images\";
textBox1.Text = ImagePath;
//load webcam devices
FilterInfoCollection devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in devices)
{
MonikerMap.Add(device.Name, device.MonikerString);
comboBox1.Items.Add(device.Name);
}
if(devices.Count > 0)
comboBox1.SelectedIndex = 0;
LoadConfig();
}
private void StartCapture()
{
string monikerString = MonikerMap[comboBox1.Text];
VCD = new VideoCaptureDevice(monikerString);
VCD.DesiredFrameRate = 10;
VCD.DesiredFrameSize = new Size(640, 480);
VCD.NewFrame += new NewFrameEventHandler(VCD_NewFrame);
videoSourcePlayer1.VideoSource = VCD;
VCD.Start();
GrabImage = true;
ImageCaptureTimer = new System.Timers.Timer(Interval);
ImageCaptureTimer.Elapsed += new System.Timers.ElapsedEventHandler(ImageCaptureTimer_Elapsed);
ImageCaptureTimer.Start();
}
void ImageCaptureTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
GrabImage = true;
}
void VCD_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (GrabImage)
{
GrabImage = false;
Bitmap img = eventArgs.Frame;
if (!Directory.Exists(ImagePath))
Directory.CreateDirectory(ImagePath);
string filename = ImagePath + "\\" + DateTime.Now.ToString("MMMM_dd_yyyy-HH.mm.ss") + ".jpg"; ;
//Console.WriteLine(filename);
img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StopCapture();
SaveConfig();
}
private void ChooseFolder()
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = textBox1.Text;
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox1.Text = fbd.SelectedPath;
ImagePath = fbd.SelectedPath;
}
}
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
ChooseFolder();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "start")
{
StartCapture();
button1.Text = "stop";
}
else
{
StopCapture();
button1.Text = "start";
}
}
private void StopCapture()
{
try
{
VCD.Stop();
ImageCaptureTimer.Stop();
}
catch { }
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
Interval = (int)numericUpDown1.Value * 60 * 1000;
}
private void SaveConfig()
{
try
{
Config currentConfig = new Config();
currentConfig.ImagePath = ImagePath;
currentConfig.Interval = Interval / 60000;
XmlSerializer xmlConfig = new XmlSerializer(typeof(Config));
TextWriter configFile = new StreamWriter(HomePath + @"\config");
xmlConfig.Serialize(configFile, currentConfig);
configFile.Close();
}
catch { }
}
private void LoadConfig()
{
try
{
TextReader configReader = new StreamReader(HomePath + @"\config");
XmlSerializer xmlConfig = new XmlSerializer(typeof(Config));
Config savedConfig = (Config)xmlConfig.Deserialize(configReader);
Interval = savedConfig.Interval * 60000;
numericUpDown1.Value = savedConfig.Interval;
ImagePath = savedConfig.ImagePath;
numericUpDown1.Value = savedConfig.Interval;
configReader.Close();
textBox1.Text = ImagePath;
}
catch { }
}
}
public class Config
{
public string ImagePath;
public int Interval;
}
}