public async Task StartCameraAsync(int delay = 0) { _running = true; _capture = new VideoCapture(0); _lastFrame = DateTime.Now; while (_running) { lock (_sync) { if (_capture?.IsDisposed ?? true) { return; } using (var mat = new Mat()) { _capture.Read(mat); OnNewImage?.Invoke(this, mat.Clone()); } FramesPerSecond = 1 / (DateTime.Now - _lastFrame).TotalSeconds; _lastFrame = DateTime.Now; } if (_running && delay > 0) { await Task.Delay(delay); } } }
// You can get at least 2 notifications for each new image file. One when it is // created and on when it is written to. The client (main UI) must be able to handle that. private void FileChanged(object sender, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed) { if (null != OnNewImage) { OnNewImage.Invoke(cameraData, e.FullPath); } } }