Guest User

Untitled

a guest
Nov 28th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. /*using Ozeki.Media.IPCamera;
  17. using Ozeki.Media.MediaHandlers;
  18. using Ozeki.Media.MediaHandlers.Video;
  19. using Ozeki.Media.MJPEGStreaming;
  20. using Ozeki.Media.Video.Controls;*/
  21. using Ozeki.Media;
  22. using Ozeki.Camera;
  23.  
  24. namespace Basic_CameraViewer
  25. {
  26. /// <summary>
  27. /// Interaction logic for MainWindow.xaml
  28. /// </summary>
  29. public partial class MainWindow : Window
  30. {
  31. private VideoViewerWPF _videoViewerWpf;
  32.  
  33. private DrawingImageProvider _provider;
  34.  
  35. private IIPCamera _ipCamera;
  36.  
  37. private WebCamera _webCamera;
  38.  
  39. private MediaConnector _connector;
  40.  
  41. private MotionDetector _detector;
  42.  
  43. public MainWindow()
  44. {
  45. InitializeComponent();
  46.  
  47. _connector = new MediaConnector();
  48.  
  49. _provider = new DrawingImageProvider();
  50.  
  51. _detector = new MotionDetector();
  52.  
  53. SetVideoViewer();
  54. }
  55.  
  56. private void SetVideoViewer()
  57. {
  58. _videoViewerWpf = new VideoViewerWPF
  59. {
  60. HorizontalAlignment = HorizontalAlignment.Stretch,
  61. VerticalAlignment = VerticalAlignment.Stretch,
  62. Background = Brushes.Black
  63. };
  64. CameraBox.Children.Add(_videoViewerWpf);
  65.  
  66. _videoViewerWpf.SetImageProvider(_provider);
  67. }
  68.  
  69. #region USB Camera Connect/Disconnect
  70.  
  71. private void ConnectUSBCamera_Click(object sender, RoutedEventArgs e)
  72. {
  73. _webCamera = WebCamera.GetDefaultDevice();
  74. if (_webCamera == null) return;
  75. _connector.Connect(_webCamera, _detector);
  76. _connector.Connect(_detector, _provider);
  77.  
  78. _webCamera.Start();
  79. _videoViewerWpf.Start();
  80. }
  81.  
  82. private void DisconnectUSBCamera_Click(object sender, RoutedEventArgs e)
  83. {
  84. _videoViewerWpf.Stop();
  85.  
  86. _webCamera.Stop();
  87. _webCamera.Dispose();
  88.  
  89. _connector.Disconnect(_webCamera, _detector);
  90. _connector.Disconnect(_detector, _provider);
  91. }
  92. #endregion
  93.  
  94. #region IP Camera Connect/Disconnect
  95.  
  96. private void ConnectIPCamera_Click(object sender, RoutedEventArgs e)
  97. {
  98. var host = HostTextBox.Text;
  99. var user = UserTextBox.Text;
  100. var pass = Password.Password;
  101.  
  102. _ipCamera = IPCameraFactory.GetCamera(host, user, pass);
  103. if (_ipCamera == null) return;
  104. _connector.Connect(_ipCamera.VideoChannel, _detector);
  105. _connector.Connect(_detector, _provider);
  106.  
  107. _ipCamera.Start();
  108. _videoViewerWpf.Start();
  109. }
  110.  
  111. private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e)
  112. {
  113. _videoViewerWpf.Stop();
  114.  
  115. _ipCamera.Disconnect();
  116. _ipCamera.Dispose();
  117.  
  118. _connector.Disconnect(_ipCamera.VideoChannel, _detector);
  119. _connector.Disconnect(_detector, _provider);
  120. }
  121. #endregion
  122.  
  123. void GuiThread(Action action)
  124. {
  125. Dispatcher.BeginInvoke(action);
  126. }
  127.  
  128. private void StartMotionDetection()
  129. {
  130. _detector.HighlightMotion = HighlightMotion.Highlight;
  131. _detector.MotionColor = MotionColor.Red;
  132. _detector.MotionDetection += detector_MotionDetection;
  133. _detector.Start();
  134. }
  135.  
  136. void detector_MotionDetection(object sender, MotionDetectionEvent e)
  137. {
  138. GuiThread(() =>
  139. {
  140. if (e.Detection)
  141. MotionEventLabel.Content = "Motion Detected";
  142. else
  143. MotionEventLabel.Content = "Motion Stopped";
  144. });
  145. }
  146.  
  147. private void StopMotionDetection()
  148. {
  149. _detector.Stop();
  150. _detector.MotionDetection -= detector_MotionDetection;
  151. _detector.Dispose();
  152. }
  153.  
  154. private void MotionChecked(object sender, RoutedEventArgs e)
  155. {
  156. MotionEventLabel.Content = String.Empty;
  157. var check = sender as CheckBox;
  158. if (check != null)
  159. {
  160. if ((bool)check.IsChecked)
  161. StartMotionDetection();
  162. else
  163. StopMotionDetection();
  164. }
  165. }
  166. }
  167. }
Add Comment
Please, Sign In to add comment