/// <summary> /// Uses item picker for choosing camera /// Initializes a JpegVideoSource with that camera /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonPickCamera_Click(object sender, EventArgs e) { if (_cameraBitmap != null) { _cameraBitmap.Dispose(); } _cameraBitmap = null; ItemPickerForm form = new ItemPickerForm(); form.KindFilter = Kind.Camera; form.AutoAccept = true; form.Init(Configuration.Instance.GetItems()); if (form.ShowDialog() == DialogResult.OK) { _selectItem = form.SelectedItem; buttonPickCamera.Text = _selectItem.Name; _jpegVideoSource = new JPEGVideoSource(_selectItem); _jpegVideoSource.Init(); ShowImageFrom(DateTime.UtcNow); buttonClear.Enabled = true; buttonSearch.Enabled = true; } }
/// <summary> /// Get the resolution of the latest recorded image /// </summary> static void GetRes() { var jpegSource = new JPEGVideoSource(_camera); jpegSource.Init(); jpegSource.Height = 0; jpegSource.Width = 0; Console.WriteLine("Asking for the last recorded image from camera {0} ", _camera.Name); var jpegData = jpegSource.GetEnd(); if (jpegData == null) { Console.WriteLine("No recorded image from " + _camera.Name); } else { Console.WriteLine("The resolution of the last recorded image " + "from camera {0} is ({1},{2})" + Environment.NewLine, _camera.Name, jpegData.Width, jpegData.Height); } }
private void LoadItems(Item item) { foreach (var child in item.GetChildren()) { if (child.FQID.Kind == Kind.Camera && child.HasChildren == VideoOS.Platform.HasChildren.No) { Cameras[child.FQID.ObjectId] = child; var status = 0; var jpegSource = new JPEGVideoSource(child); try { jpegSource.Init(); var jpegData = jpegSource.GetNearest(new DateTime()) as JPEGData; File.WriteAllBytes($"C:/Snapshots/{child.Name}.jpg", jpegData.Bytes); } catch (Exception e) { Console.WriteLine(e.Message); status = 1; } LstCameras.Items.Add(child.Name, status); } else { if (child.HasChildren != VideoOS.Platform.HasChildren.No) { LoadItems(child); } } } }
private void ShowReplay(Item camera, DateTime startDate, DateTime endDate) { Bitmap imageClear = new Bitmap(320, 240); Graphics g = Graphics.FromImage(imageClear); g.FillRectangle(Brushes.Black, 0, 0, 320, 240); g.Dispose(); SetImage(imageClear); JPEGVideoSource source = new JPEGVideoSource(camera); source.Init(); List <object> resultList = source.Get(startDate, new TimeSpan(endDate.Ticks), 150); //TODO remove ---TEST source.Close(); _stop = true; if (resultList != null) { this.UIThread(() => label2.Text = $"Number of frames: {resultList.Count}"); if (resultList.Count > 0) { _stop = false; btnStart.Enabled = _stop; btnStop.Enabled = !_stop; } } while (!_stop) { int avgInterval = 12000 / resultList.Count; foreach (JPEGData jpeg in resultList) { MemoryStream ms = new MemoryStream(jpeg.Bytes); Bitmap image = new Bitmap(ms); SetImage(image); ms.Close(); Thread.Sleep(avgInterval); if (_stop) { break; } } if (!_stop) { Thread.Sleep(1500); } } source.Close(); //TODO }
private void ShowReplay() { Image imageClear = new Bitmap(320, 240); Graphics g = Graphics.FromImage(imageClear); g.FillRectangle(Brushes.Black, 0, 0, 320, 240); g.Dispose(); Invoke(new SetImageDelegate(SetImage), new[] { imageClear }); JPEGVideoSource source = new JPEGVideoSource(_selectedItem); source.Width = pictureBox2.Width; source.Height = pictureBox2.Height; source.SetKeepAspectRatio(true, true); source.Init(); var interval = TimeSpan.FromSeconds(VideoIntervalSeconds); List <object> resultList = source.Get(DateTime.Now - interval, interval, 150); _stop = true; if (resultList != null) { Invoke((MethodInvoker) delegate() { label2.Text = "Number of frames: " + resultList.Count; }); if (resultList.Count > 0) { _stop = false; } } while (!_stop) { int avgInterval = 1000 * VideoIntervalSeconds / (ReplaySpeedFactor * resultList.Count); foreach (JPEGData jpeg in resultList) { MemoryStream ms = new MemoryStream(jpeg.Bytes); Image image = new Bitmap(ms); Invoke(new SetImageDelegate(SetImage), new[] { image }); ms.Close(); Thread.Sleep(avgInterval); if (_stop) { break; } } if (!_stop) { Thread.Sleep(1500); } } source.Close(); }
private void JPEGFetchThread() { bool errorRecovery = false; while (!_stop) { if (_performCloseVideoSource) { if (_jpegVideoSource != null) { _jpegVideoSource.Close(); _jpegVideoSource = null; } _performCloseVideoSource = false; } if (_newlySelectedItem != null) { _selectedItem = _newlySelectedItem; _jpegVideoSource = new JPEGVideoSource(_selectedItem); if (checkBoxAspect.Checked) { // Keeping aspect ratio can only work when the Media Toolkit knows the actual displayed area _jpegVideoSource.Width = pictureBox.Width; _jpegVideoSource.Height = pictureBox.Height; _jpegVideoSource.SetKeepAspectRatio(checkBoxAspect.Checked, checkBoxFill.Checked); // Must be done before Init } try { _jpegVideoSource.Init(); JPEGData jpegData = _currentShownTime == DateTime.MinValue ? _jpegVideoSource.GetBegin() : _jpegVideoSource.GetAtOrBefore(_currentShownTime) as JPEGData; if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } else { ShowError(""); // Clear any error messages } _newlySelectedItem = null; errorRecovery = false; } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to server ..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } if (errorRecovery) { Thread.Sleep(3000); continue; } if (_setNewResolution && _jpegVideoSource != null && _requestInProgress == false) { try { _jpegVideoSource.Width = _newWidth; _jpegVideoSource.Height = _newHeight; _jpegVideoSource.SetWidthHeight(); _setNewResolution = false; JPEGData jpegData; jpegData = _jpegVideoSource.GetAtOrBefore(_currentShownTime) as JPEGData; if (jpegData != null) { _requestInProgress = true; _currentShownTime = DateTime.MinValue; ShowJPEG(jpegData); } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to recorder..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } if (_requestInProgress == false && _jpegVideoSource != null && _nextCommand != MyPlayCommand.None) { JPEGData jpegData = null; try { switch (_nextCommand) { case MyPlayCommand.Start: jpegData = _jpegVideoSource.GetBegin(); break; case MyPlayCommand.NextFrame: jpegData = _jpegVideoSource.GetNext() as JPEGData; break; case MyPlayCommand.NextSequence: jpegData = _jpegVideoSource.GetNextSequence(); break; case MyPlayCommand.PrevFrame: jpegData = _jpegVideoSource.GetPrevious(); break; case MyPlayCommand.PrevSequence: jpegData = _jpegVideoSource.GetPreviousSequence(); break; case MyPlayCommand.End: jpegData = _jpegVideoSource.GetEnd(); break; } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to recorder..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } _nextCommand = MyPlayCommand.None; } if (_nextToFetchTime != DateTime.MinValue && _requestInProgress == false && _jpegVideoSource != null) { bool willResultInSameFrame = false; // Lets validate if we are just asking for the same frame if (_currentTimeInformation != null) { if (_currentTimeInformation.PreviousTime < _nextToFetchTime && _currentTimeInformation.NextTime > _nextToFetchTime) { willResultInSameFrame = true; } } if (willResultInSameFrame) { Debug.WriteLine("Now Fetch ignored: " + _nextToFetchTime.ToLongTimeString() + " - nextToFetch=" + _nextToFetchTime.ToLongTimeString()); // Same frame -> Ignore request _requestInProgress = false; _nextToFetchTime = DateTime.MinValue; } else { Debug.WriteLine("Now Fetch: " + _nextToFetchTime.ToLongTimeString()); DateTime time = _nextToFetchTime; _nextToFetchTime = DateTime.MinValue; try { DateTime localTime = time.Kind == DateTimeKind.Local ? time : time.ToLocalTime(); DateTime utcTime = time.Kind == DateTimeKind.Local ? time.ToUniversalTime() : time; BeginInvoke( new MethodInvoker(delegate() { textBoxAsked.Text = localTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); })); JPEGData jpegData; jpegData = _jpegVideoSource.GetAtOrBefore(utcTime) as JPEGData; if (jpegData == null && _mode == PlaybackPlayModeData.Stop) { jpegData = _jpegVideoSource.GetNearest(utcTime) as JPEGData; } if (_mode == PlaybackPlayModeData.Reverse) { while (jpegData != null && jpegData.DateTime > utcTime) { jpegData = _jpegVideoSource.GetPrevious(); } } else if (_mode == PlaybackPlayModeData.Forward) { if (jpegData != null && jpegData.DateTime < utcTime) { jpegData = _jpegVideoSource.Get(utcTime) as JPEGData; } } if (jpegData != null) { _requestInProgress = true; ShowJPEG(jpegData); } } catch (Exception ex) { if (ex is CommunicationMIPException) { ShowError("Connection lost to server ..."); } else { ShowError(ex.Message); } errorRecovery = true; _jpegVideoSource = null; _newlySelectedItem = _selectedItem; // Redo the Initialization } } } Thread.Sleep(5); } _fetchThread = null; }