/// <summary> /// Service start /// </summary> protected override void Start() { // // Add service specific initialization here // base.Start(); #region Run WinForms and Activate Timers _stopWatch.Start(); var webcamPorts = new[] { _firstWebcamPort, _secondWebcamPort }; for (int idx = 0; idx < _webcamForms.Length; ++idx) { int i = idx; WinFormsServicePort.Post(new RunForm(() => { _webcamForms[i] = new ImageProcessingResultForm(); _webcamForms[i].Show(); Activate(Arbiter.Receive(false, _timers[i], dateTime => UpdateWebcam(webcamPorts[i], _webcamForms[i], _timers[i]))); TaskQueue.EnqueueTimer(TimeSpan.FromMilliseconds(60), _timers[i]); return(_webcamForms[i]); })); } #endregion }
IEnumerator <ITask> UpdateImage(webcam.WebCamOperations webcamPort, ImageProcessingResultForm webcamForm) { long timestamp = _stopWatch.ElapsedMilliseconds; byte[] rgbData = null; Size size = new Size(0, 0); yield return(Arbiter.Choice(webcamPort.QueryFrame(), success => { rgbData = success.Frame; size = success.Size; }, failure => { LogError(failure.ToException()); })); TaskQueue.Enqueue(new Task(() => { if (rgbData != null) { ComputeGradient(ref rgbData, size); UpdateBitmap(rgbData, size, webcamForm, timestamp); } })); }
IEnumerator <ITask> UpdateWebcamHelper(webcam.WebCamOperations webcamPort, ImageProcessingResultForm webcamForm, Port <DateTime> timerPort) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); yield return(new IterativeTask(() => UpdateImage(webcamPort, webcamForm))); Activate(Arbiter.Receive(false, timerPort, dateTime => UpdateWebcam(webcamPort, webcamForm, timerPort))); TaskQueue.EnqueueTimer(TimeSpan.FromMilliseconds(Math.Max(1, 60 - stopwatch.ElapsedMilliseconds)), timerPort); yield break; }
private void UpdateBitmap(byte[] rgbData, Size size, ImageProcessingResultForm webcamForm, long timestamp) { if (webcamForm == null) { return; } WinFormsServicePort.Post(new FormInvoke(() => { if (timestamp > webcamForm.TimeStamp) { webcamForm.TimeStamp = timestamp; Bitmap bmp = webcamForm.ImageResultBitmap; CopyBytesToBitmap(rgbData, size.Width, size.Height, ref bmp); if (bmp != webcamForm.ImageResultBitmap) { webcamForm.UpdateForm(bmp); } webcamForm.Invalidate(true); } })); }
void UpdateWebcam(webcam.WebCamOperations webcamPort, ImageProcessingResultForm webcamForm, Port <DateTime> timerPort) { SpawnIterator <webcam.WebCamOperations, ImageProcessingResultForm, Port <DateTime> >( webcamPort, webcamForm, timerPort, UpdateWebcamHelper); }