本实例实现超市摄像头定时监控系统。运行本例后,在“定时监控设置”处设置监控的星期及时间,单击【保存】按钮,将“定时设置”参数数据保存到数据库中。系统在运行到定时时间后,程序将自动进行监控。如图所示。另外,监控的录像文件和图片文件保存在D盘根目录中,命名格式为系统当前日期。
图 超市摄像头定时监控
实现过程
(1)新建一个项目,命名为Ex13_10,默认窗体为Form1,添加一个类文件(.CS),用于编写视频类。
(2)在Form1窗体中,主要添加一个PictrueBox控件,用于显示视频。
(3)主要程序代码。
视频类中主要程序代码如下:
/// <summary>
/// 开始录像
/// </summary>
/// <param name="path">要保存录像的路径</param>
public void StarKinescope(string path)
{
IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0, hBmp.ToInt32());
SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
}
/// <summary>
/// 停止录像
/// </summary>
public void StopKinescope()
{
SendMessage(hWndC, WM_CAP_STOP, 0, 0);
}
Form1窗体主要代码如下:
private void timer1_Tick(object sender, EventArgs e)
{
string strTime="";
//星期一
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek)==1)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt1.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期二
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 2)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt2.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期三
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 3)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt3.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期四
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 4)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt4.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期五
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 5)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt5.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期六
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 6)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt6.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}
//星期日
if (chk1.Checked && Convert.ToInt32(DateTime.Now.DayOfWeek) == 7)
{
strTime = DateTime.Now.ToString("HH:mm");
DateTime date = Convert.ToDateTime(mtxt7.Text);
if (strTime == date.ToString("HH:mm"))
video.StarKinescope(@"d:/" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".avi");
}