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