我制作了一个简单的程序,可以显示网络摄像头的视频,并可以检测提要中的运动,但我在理解检测到运动时如何从网络摄像头进行录制时遇到了问题。我已经在Aforge.Net网站上搜索过了,但我发现很难理解我需要做什么。当检测到运动时,我想开始录制到预先确定的目的地,当没有检测到运动,我想继续录制一段时间,然后停止。这是我到目前为止的计划的一部分。。。
请你帮我解释一下,如果我需要提供更多信息或代码,请告诉我。感谢
$//打开视频源
private void OpenVideoSource(IVideoSource source)
{
// set busy cursor
//this.Cursor = Cursors.WaitCursor;
// close previous video source
CloseVideoSource();
//motionDetectionType = 1;
//SetMotionDetectionAlgorithm(new TwoFramesDifferenceDetector());
//motionProcessingType = 1;
//SetMotionProcessingAlgorithm(new MotionAreaHighlighting());
// start new video source
videoSourcePlayer.VideoSource = new AsyncVideoSource(source);
videoSourcePlayer.NewFrame +=new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
//videoSourcePlayer.DesiredFrameRate = 30;
//webcam's default frame rate will be used instead of above code that has an error
// create new video file
writer.Open("test.avi", width, height, 25, VideoCodec.MPEG4);
// create a bitmap to save into the video file
Bitmap image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// write 1000 video frames
for (int i = 0; i < 1000; i++)
{
image.SetPixel(i % width, i % height, System.Drawing.Color.Red);
writer.WriteVideoFrame(image);
}
writer.Close();
try
{
videoSourcePlayer.Start();
//motionAlarmLevel = sldMotionSensitivity.Value / 100;
}
catch (Exception x)
{
System.Windows.MessageBox.Show(x.ToString());
}
//videoSource.DesiredFrameSize = new System.Drawing.Size(800,600);
videoSourcePlayer.BorderColor = System.Drawing.Color.Blue;
// reset statistics
statIndex = statReady = 0;
// start timers
clock.Start();
//alarm.Start();
videoSource = source;
//this.Cursor = Cursors.Default;
}
// Close current video source
private void CloseVideoSource()
{
// set busy cursor
//this.Cursor = Cursors.WaitCursor;
// stop current video source
videoSourcePlayer.SignalToStop();
// wait 2 seconds until camera stops
for (int i = 0; (i < 50) && (videoSourcePlayer.IsRunning); i++)
{
Thread.Sleep(100);
}
if (videoSourcePlayer.IsRunning)
videoSourcePlayer.Stop();
// stop timers
clock.Stop();
//alarm.Stop();
motionHistory.Clear();
// reset motion detector
if (detector != null)
detector.Reset();
//videoSourcePlayer.BorderColor = System.Drawing.Color.Red;
//this.Cursor = Cursors.Default;
}
// New frame received by the player
void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
lock (this)
{
if (detector != null)
{
//motion detected
if (detector.ProcessFrame(image) > motionAlarmLevel)
{
//float motionLevel = detector.ProcessFrame(image);
videoSourcePlayer.BorderColor = System.Drawing.Color.Red;
Dispatcher.BeginInvoke(new ThreadStart(delegate { lblMotionDetected.Content = "Motion Detected"; }));
//lblMotionDetected.Content = "Motion Detected";
//flash = (int)(2 * (1000 / alarm.Interval));
}
// no motion detected
if (detector.ProcessFrame(image) < motionAlarmLevel)
{
videoSourcePlayer.BorderColor = System.Drawing.Color.Black;
Dispatcher.BeginInvoke(new ThreadStart(delegate { lblMotionDetected.Content = "No Motion Detected"; }));
}
// if (motionLevel > motionAlarmLevel)
// {
// // flash for 2 seconds
// timer.Start();
// }
}
}
}
有一个开源项目叫做ispy-http://www.ispyconnect.com正是这样。源文件可供下载。