Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2484 - 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Windows;
6
using System.Windows.Controls;
7
using System.Windows.Data;
8
using System.Windows.Documents;
9
using System.Windows.Input;
10
using System.Windows.Interop;
11
using System.Windows.Media;
12
using System.Windows.Media.Imaging;
13
using System.Windows.Navigation;
14
using System.Windows.Shapes;
15
using System.ComponentModel;
16
using Touchless.Shared.Extensions;
17
 
18
namespace Touchless.Vision.Camera.Configuration
19
{
20
    /// <summary>
21
    /// Interaction logic for CameraFrameSourceConfigurationElement.xaml
22
    /// </summary>
23
    public partial class CameraFrameSourceConfigurationElement : UserControl
24
    {
25
        private readonly CameraFrameSource _cameraFrameSource;
26
 
27
        public CameraFrameSourceConfigurationElement()
28
            : this(null)
29
        {
30
 
31
        }
32
 
33
        public CameraFrameSourceConfigurationElement(CameraFrameSource cameraFrameSource)
34
        {
35
            _cameraFrameSource = cameraFrameSource;
36
            InitializeComponent();
37
 
38
            if (!DesignerProperties.GetIsInDesignMode(this) && _cameraFrameSource != null)
39
            {
40
                _cameraFrameSource.NewFrame += NewFrame;
41
 
42
                var cameras = CameraService.AvailableCameras.ToList();
43
                comboBoxCameras.ItemsSource = cameras;
44
                comboBoxCameras.SelectedItem = _cameraFrameSource.Camera;
45
            }
46
        }
47
 
48
        private readonly object _frameSync = new object();
49
        private bool _frameWaiting = false;
50
        private void NewFrame(Touchless.Vision.Contracts.IFrameSource frameSource, Touchless.Vision.Contracts.Frame frame, double fps)
51
        {
52
            //We want to ignore frames we can't render fast enough
53
            lock (_frameSync)
54
            {
55
                if (!_frameWaiting)
56
                {
57
                    _frameWaiting = true;
58
                    Action workAction = delegate
59
                    {
60
                        this.labelCameraFPSValue.Content = fps.ToString();
61
                        this.imgPreview.Source = frame.OriginalImage.ToBitmapSource();
62
 
63
                        lock (_frameSync)
64
                        {
65
                            _frameWaiting = false;
66
                        }
67
                    };
68
                    Dispatcher.BeginInvoke(workAction);
69
                }
70
            }
71
        }
72
 
73
        private void comboBoxCameras_SelectionChanged(object sender, SelectionChangedEventArgs e)
74
        {
75
            _cameraFrameSource.Camera = comboBoxCameras.SelectedItem as Camera;
76
            panelCameraInfo.Visibility = comboBoxCameras.SelectedItem != null
77
                                                ? Visibility.Visible
78
                                                : Visibility.Collapsed;
79
        }
80
 
81
        private void buttonCameraProperties_Click(object sender, RoutedEventArgs e)
82
        {
83
            _cameraFrameSource.Camera.ShowPropertiesDialog();
84
        }
85
 
86
        private void chkLimitFps_Checked(object sender, RoutedEventArgs e)
87
        {
88
            this.txtLimitFps.IsEnabled = true;
89
            updateFPS();
90
        }
91
 
92
        private void chkLimitFps_Unchecked(object sender, RoutedEventArgs e)
93
        {
94
            //this.txtLimitFps.Text = "-1";
95
            this.txtLimitFps.Background = Brushes.White;
96
            _cameraFrameSource.Camera.Fps = -1;
97
            this.txtLimitFps.IsEnabled = false;
98
        }
99
 
100
        private void txtLimitFps_TextChanged(object sender, TextChangedEventArgs e)
101
        {
102
            updateFPS();
103
        }
104
 
105
        private void updateFPS()
106
        {
107
            int fps;
108
            if (int.TryParse(this.txtLimitFps.Text, out fps))
109
            {
110
                _cameraFrameSource.Camera.Fps = fps;
111
                this.txtLimitFps.Background = Brushes.LightGreen;
112
            }
113
            else
114
            {
115
                this.txtLimitFps.Background = Brushes.Red;
116
            }
117
        }
118
    }
119
}