Click here to Skip to main content
15,881,281 members
Articles / Multimedia / DirectX

Cam Alarm 2.0 - Alarm system run from a web camera

Rate me:
Please Sign up or sign in to vote.
4.86/5 (33 votes)
16 Jun 2010CPOL3 min read 85.6K   9.5K   150  
Simple alarm system for those on a budget..
// Motion Detector
//
// Copyright � Andrew Kirillov, 2005
// andrew.kirillov@gmail.com
//
namespace CamAlarm
{
	using System;
	using System.Drawing;
	using System.Threading;

	/// <summary>
	/// Camera class
	/// </summary>
	public class Camera
	{
		private IVideoSource	videoSource = null;
		private IMotionDetector	motionDetector = null;
		private Bitmap			lastFrame = null;

		// image width and height
		private int		width = -1, height = -1;

		// alarm level
		private double	alarmLevel = 0.005;

		//
		public event EventHandler	NewFrame;
		public event EventHandler	Alarm;

		// LastFrame property
		public Bitmap LastFrame
		{
			get { return lastFrame; }
		}
        public double AlarmLevel
        {
            get { return alarmLevel; }
            set { alarmLevel = value; }
        }
		// Width property
		public int Width
		{
			get { return width; }
		}
		// Height property
		public int Height
		{
			get { return height; }
		}
		// FramesReceived property
		public int FramesReceived
		{
			get { return ( videoSource == null ) ? 0 : videoSource.FramesReceived; }
		}
		// BytesReceived property
		public int BytesReceived
		{
			get { return ( videoSource == null ) ? 0 : videoSource.BytesReceived; }
		}
		// Running property
		public bool Running
		{
			get { return ( videoSource == null ) ? false : videoSource.Running; }
		}
		// MotionDetector property
		public IMotionDetector MotionDetector
		{
			get { return motionDetector; }
			set { motionDetector = value; }
		}

		// Constructor
		public Camera( IVideoSource source ) : this( source, null )
		{ }
		public Camera( IVideoSource source, IMotionDetector detector )
		{
			this.videoSource = source;
			this.motionDetector = detector;
			videoSource.NewFrame += new CameraEventHandler( video_NewFrame );
		}

		// Start video source
		public void Start( )
		{
			if ( videoSource != null )
			{
				videoSource.Start( );
			}
		}

		// Siganl video source to stop
		public void SignalToStop( )
		{
			if ( videoSource != null )
			{
				videoSource.SignalToStop( );
			}
		}

		// Wait video source for stop
		public void WaitForStop( )
		{
			// lock
			Monitor.Enter( this );

			if ( videoSource != null )
			{
				videoSource.WaitForStop( );
			}
			// unlock
			Monitor.Exit( this );
		}

		// Abort camera
		public void Stop( )
		{
			// lock
			Monitor.Enter( this );

			if ( videoSource != null )
			{
				videoSource.Stop( );
			}
			// unlock
			Monitor.Exit( this );
		}

		// Lock it -ju changed from Enter to TryEnter
		public void Lock( )
		{
			Monitor.TryEnter( this, 5000 );
		}

		// Unlock it
		public void Unlock( )
		{
			Monitor.Exit( this );
		}

		// On new frame
		private void video_NewFrame( object sender, CameraEventArgs e )
		{
			try
			{
				// lock
				Monitor.Enter( this );

				// dispose old frame
				if ( lastFrame != null )
				{
					lastFrame.Dispose( );
				}

				lastFrame = (Bitmap) e.Bitmap.Clone( );

				// apply motion detector
				if ( motionDetector != null )
				{
					motionDetector.ProcessFrame( ref lastFrame );

					// check motion level
					if ((motionDetector.MotionLevel >= alarmLevel) && (Alarm != null))
					{
						Alarm(this, new EventArgs());
					}
				}

				// image dimension
				width = lastFrame.Width;
				height = lastFrame.Height;
			}
			catch ( Exception )
			{
			}
			finally
			{
				// unlock
				Monitor.Exit( this );
			}

			// notify client
			if ( NewFrame != null )
				NewFrame( this, new EventArgs( ) );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Network Administrator vtdev.com
Canada Canada
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.

Comments and Discussions