Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- /*using Ozeki.Media.IPCamera;
- using Ozeki.Media.MediaHandlers;
- using Ozeki.Media.MediaHandlers.Video;
- using Ozeki.Media.MJPEGStreaming;
- using Ozeki.Media.Video.Controls;*/
- using Ozeki.Media;
- using Ozeki.Camera;
- namespace Basic_CameraViewer
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private VideoViewerWPF _videoViewerWpf;
- private DrawingImageProvider _provider;
- private IIPCamera _ipCamera;
- private WebCamera _webCamera;
- private MediaConnector _connector;
- private MotionDetector _detector;
- public MainWindow()
- {
- InitializeComponent();
- _connector = new MediaConnector();
- _provider = new DrawingImageProvider();
- _detector = new MotionDetector();
- SetVideoViewer();
- }
- private void SetVideoViewer()
- {
- _videoViewerWpf = new VideoViewerWPF
- {
- HorizontalAlignment = HorizontalAlignment.Stretch,
- VerticalAlignment = VerticalAlignment.Stretch,
- Background = Brushes.Black
- };
- CameraBox.Children.Add(_videoViewerWpf);
- _videoViewerWpf.SetImageProvider(_provider);
- }
- #region USB Camera Connect/Disconnect
- private void ConnectUSBCamera_Click(object sender, RoutedEventArgs e)
- {
- _webCamera = WebCamera.GetDefaultDevice();
- if (_webCamera == null) return;
- _connector.Connect(_webCamera, _detector);
- _connector.Connect(_detector, _provider);
- _webCamera.Start();
- _videoViewerWpf.Start();
- }
- private void DisconnectUSBCamera_Click(object sender, RoutedEventArgs e)
- {
- _videoViewerWpf.Stop();
- _webCamera.Stop();
- _webCamera.Dispose();
- _connector.Disconnect(_webCamera, _detector);
- _connector.Disconnect(_detector, _provider);
- }
- #endregion
- #region IP Camera Connect/Disconnect
- private void ConnectIPCamera_Click(object sender, RoutedEventArgs e)
- {
- var host = HostTextBox.Text;
- var user = UserTextBox.Text;
- var pass = Password.Password;
- _ipCamera = IPCameraFactory.GetCamera(host, user, pass);
- if (_ipCamera == null) return;
- _connector.Connect(_ipCamera.VideoChannel, _detector);
- _connector.Connect(_detector, _provider);
- _ipCamera.Start();
- _videoViewerWpf.Start();
- }
- private void DisconnectIPCamera_Click(object sender, RoutedEventArgs e)
- {
- _videoViewerWpf.Stop();
- _ipCamera.Disconnect();
- _ipCamera.Dispose();
- _connector.Disconnect(_ipCamera.VideoChannel, _detector);
- _connector.Disconnect(_detector, _provider);
- }
- #endregion
- void GuiThread(Action action)
- {
- Dispatcher.BeginInvoke(action);
- }
- private void StartMotionDetection()
- {
- _detector.HighlightMotion = HighlightMotion.Highlight;
- _detector.MotionColor = MotionColor.Red;
- _detector.MotionDetection += detector_MotionDetection;
- _detector.Start();
- }
- void detector_MotionDetection(object sender, MotionDetectionEvent e)
- {
- GuiThread(() =>
- {
- if (e.Detection)
- MotionEventLabel.Content = "Motion Detected";
- else
- MotionEventLabel.Content = "Motion Stopped";
- });
- }
- private void StopMotionDetection()
- {
- _detector.Stop();
- _detector.MotionDetection -= detector_MotionDetection;
- _detector.Dispose();
- }
- private void MotionChecked(object sender, RoutedEventArgs e)
- {
- MotionEventLabel.Content = String.Empty;
- var check = sender as CheckBox;
- if (check != null)
- {
- if ((bool)check.IsChecked)
- StartMotionDetection();
- else
- StopMotionDetection();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment