コード例 #1
0
        private void ConfigureDevice()
        {
            const int FRAMERATE = 15;  // Depends on video device caps.  Generally 4-30.
            const int VIDEOWIDTH = 640; // Depends on video device caps
            const int VIDEOHEIGHT = 480; // Depends on video device caps

            int index = (int)deviceComboBox.Dispatcher.Invoke(new Func<int>(() => deviceComboBox.SelectedIndex));
            cam = new Webcam(index, FRAMERATE, VIDEOWIDTH, VIDEOHEIGHT);
        }
コード例 #2
0
        // Start serving up frames
        private void StartWebcam(Webcam cam, ConnectionManager conManager, StreamWriter sw, ImageCodecInfo myImageCodecInfo, EncoderParameters myEncoderParameters)
        {
            MemoryStream m = new MemoryStream(20000);
            Bitmap image = null;
            IntPtr ip = IntPtr.Zero;
            Font fontOverlay = new Font("Times New Roman", 14, System.Drawing.FontStyle.Bold,
                System.Drawing.GraphicsUnit.Point);
            MotionDetector motionDetector = new MotionDetector();
            DateTime lastMotion = DateTime.Now;
            int interval = 5;

            stopCondition = false;
            isRecording = false;
            player = new System.Media.SoundPlayer();

            motionDetector.MotionLevelCalculation = true;
            player.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler((obj, arg) => player.PlayLooping());

            cam.Start();

            while (!stopCondition)
            {
                try
                {
                    // capture image
                    ip = cam.GetBitMap();
                    image = new Bitmap(cam.Width, cam.Height, cam.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ip);
                    image.RotateFlip(RotateFlipType.RotateNoneFlipY);
                    
                    motionDetector.ProcessFrame(ref image);
                   
                    if (motionDetector.MotionLevel >= alarmLevel)
                    {
                        if (!isRecording && (DateTime.Now.Second % interval == 0))
                            conManager.SendMessage("record");

                        lastMotion = DateTime.Now;
                    }
                    else
                    {
                        if (DateTime.Now.Subtract(lastMotion).Seconds > interval)
                        {
                            if (isRecording)
                            {
                                conManager.SendMessage("stop-record");
                                isRecording = false;
                            }
                            if (isAlarming)
                            {
                                player.Stop();
                                isAlarming = false;
                            }
                        }
                    }

                    // add text that displays date time to the image
                    image.AddText(fontOverlay, 10, 10, DateTime.Now.ToString());

                    // save it to jpeg using quality options
                    image.Save(m, myImageCodecInfo, myEncoderParameters);
                    
                    // send the jpeg image if server requests it
                    if (requested)
                        conManager.SendImage(m);
                }
                catch (Exception ex)
                {
                    try
                    {
                        sw.WriteLine(DateTime.Now.ToString());
                        sw.WriteLine(ex);
                    }
                    catch { }
                }
                finally
                {
                    // Empty the stream
                    m.SetLength(0);

                    // remove the image from memory
                    if (image != null)
                    {
                        image.Dispose();
                        image = null;
                    }

                    if (ip != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(ip);
                        ip = IntPtr.Zero;
                    }
                }
            }
            
            cam.Pause();
            player.Stop();
            fontOverlay.Dispose();
        }