public void Test()
        {
            var source = new DirectoryFilesCamera(@"z:\", "*.jpg");
            source.Initialize();

            var motionFrameLogger = new FrameWritter();

            var portraitWriter = new PortraitsLogger(@".\Portrait");
            portraitWriter.Initialize();

            var asyncPortraitWriter = new AsyncPortraitLogger(@".\AsyncPortrait1");
            asyncPortraitWriter.Stopped += (o, e) => System.Diagnostics.Debug.WriteLine(e.Value.Message);
            asyncPortraitWriter.Initialize();

            var asyncWriter1 = new AsyncPortraitLogger(@".\asyncport2");
            asyncWriter1.Initialize();

            var portraitFinder = new PortraitFinder();
            portraitFinder.AddListener(asyncPortraitWriter);
            //             portraitFinder.AddListener(portraitWriter);
            //             portraitFinder.AddListener(asyncWriter1);

            asyncPortraitWriter.Start();

            bool running = true;
            for (int i = 0; i < 25 && running;++i )
            {
                var frame = source.RetrieveFrame();

                if (i %10 ==0 )
                {
                    asyncPortraitWriter.Stop();
                    portraitFinder.AddListener(asyncPortraitWriter);
                    asyncPortraitWriter.Start();
                }
            }
        }
        public void StartWith(CameraInfo cameraInfo, System.Windows.Forms.Control displayControl)
        {
            _displayControl = displayControl;

            if (_jpegStream == null)
            {
                switch (cameraInfo.Provider)
                {
                    case CameraProvider.LocalDirectory:
                        var dir = new Damany.Cameras.DirectoryFilesCamera(cameraInfo.Location.LocalPath, "*.*");
                        dir.Repeat = false;
                        dir.FrameIntervalMs = cameraInfo.Interval;

                        _jpegStream = dir;
                        break;

                    case CameraProvider.Sanyo:
                        var sanyo = new JPEGExtendStream(cameraInfo.Location.ToString());
                        sanyo.Login = cameraInfo.LoginUserName ?? "guest";
                        sanyo.Password = cameraInfo.LoginPassword ?? "guest";
                        sanyo.FrameInterval = cameraInfo.Interval;

                        sanyo.RequireCookie = cameraInfo.Provider == CameraProvider.Sanyo;
                        _portraitFinder.PostFilters = _facePostFilters;

                        _jpegStream = sanyo;
                        break;
                    case CameraProvider.AipStar:

                        var hwnd = displayControl != null ? displayControl.Handle : IntPtr.Zero;
                        var aip = CreateAipCamera(cameraInfo, hwnd);
                        aip.Camera.StreamId = 0;

                        Mediator.Instance.RegisterHandler(
                            "liveViewResize", new Action<object>(LiveViewResized));
                        _jpegStream = aip;

                        if (displayControl != null)
                        {
                            //aip.Camera.DisplayPos = displayControl.ClientRectangle;
                        }

                        var videoRecorder = CreateAipCamera(cameraInfo, IntPtr.Zero);
                        videoRecorder.Camera.PathFunc = d =>
                                                            {
                                                                var utc = d.ToUniversalTime();
                                                                var rp = string.Format(
                                                                    "{0:d2}\\NORMAL\\{1:d4}{2:d2}{3:d2}\\{4:d2}\\{5:d2}.m4v",
                                                                    cameraInfo.Id,
                                                                    utc.Year, utc.Month, utc.Day, utc.Hour, utc.Minute);

                                                                var ap = Path.Combine(Util.GetVideoOutputPath(), rp);
                                                                var directory = Path.GetDirectoryName(ap);
                                                                if (!Directory.Exists(directory))
                                                                {
                                                                    Directory.CreateDirectory(directory);
                                                                }

                                                                return ap;
                                                            };
                        videoRecorder.Camera.StreamId = 1;
                        videoRecorder.Start();
                        videoRecorder.Camera.StartRecord();

                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                }

                _cameraInfo = cameraInfo;

                _jpegStream.NewFrame += JpegStreamNewFrame;
                _jpegStream.Start();

                if (_faceSearchTask == null)
                {
                    _faceSearchTask =
                        Task.Factory.StartNew(
                            FaceSearchWorkerThread,
                            _tokenSource.Token,
                            TaskCreationOptions.LongRunning,
                            TaskScheduler.Default
                            );
                }

                _faceComparer.InitializeAsync();
                _faceComparer.Start();

                _faceComparer.Threshold = Properties.Settings.Default.RealTimeFaceCompareSensitivity;
                _faceComparer.Sensitivity = Properties.Settings.Default.LbpThreshold;

            }
        }
        public void StartWith(CameraInfo cameraInfo, System.Windows.Forms.Control displayControl)
        {
            _displayControl = displayControl;

            if (_jpegStream == null)
            {
                switch (cameraInfo.Provider)
                {
                case CameraProvider.LocalDirectory:
                    var dir = new Damany.Cameras.DirectoryFilesCamera(cameraInfo.Location.LocalPath, "*.*");
                    dir.Repeat          = false;
                    dir.FrameIntervalMs = cameraInfo.Interval;

                    _jpegStream = dir;
                    break;

                case CameraProvider.Sanyo:
                    var sanyo = new JPEGExtendStream(cameraInfo.Location.ToString());
                    sanyo.Login         = cameraInfo.LoginUserName ?? "guest";
                    sanyo.Password      = cameraInfo.LoginPassword ?? "guest";
                    sanyo.FrameInterval = cameraInfo.Interval;

                    sanyo.RequireCookie         = cameraInfo.Provider == CameraProvider.Sanyo;
                    _portraitFinder.PostFilters = _facePostFilters;

                    _jpegStream = sanyo;
                    break;

                case CameraProvider.AipStar:

                    var hwnd = displayControl != null ? displayControl.Handle : IntPtr.Zero;
                    var aip  = CreateAipCamera(cameraInfo, hwnd);
                    aip.Camera.StreamId = 0;


                    Mediator.Instance.RegisterHandler(
                        "liveViewResize", new Action <object>(LiveViewResized));
                    _jpegStream = aip;

                    if (displayControl != null)
                    {
                        //aip.Camera.DisplayPos = displayControl.ClientRectangle;
                    }

                    var videoRecorder = CreateAipCamera(cameraInfo, IntPtr.Zero);
                    videoRecorder.Camera.PathFunc = d =>
                    {
                        var utc = d.ToUniversalTime();
                        var rp  = string.Format(
                            "{0:d2}\\NORMAL\\{1:d4}{2:d2}{3:d2}\\{4:d2}\\{5:d2}.m4v",
                            cameraInfo.Id,
                            utc.Year, utc.Month, utc.Day, utc.Hour, utc.Minute);

                        var ap        = Path.Combine(Util.GetVideoOutputPath(), rp);
                        var directory = Path.GetDirectoryName(ap);
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }

                        return(ap);
                    };
                    videoRecorder.Camera.StreamId = 1;
                    videoRecorder.Start();
                    videoRecorder.Camera.StartRecord();

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                _cameraInfo = cameraInfo;

                _jpegStream.NewFrame += JpegStreamNewFrame;
                _jpegStream.Start();



                if (_faceSearchTask == null)
                {
                    _faceSearchTask =
                        Task.Factory.StartNew(
                            FaceSearchWorkerThread,
                            _tokenSource.Token,
                            TaskCreationOptions.LongRunning,
                            TaskScheduler.Default
                            );
                }



                _faceComparer.InitializeAsync();
                _faceComparer.Start();

                _faceComparer.Threshold   = Properties.Settings.Default.RealTimeFaceCompareSensitivity;
                _faceComparer.Sensitivity = Properties.Settings.Default.LbpThreshold;
            }
        }