Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2484 | - | 1 | using System; |
2 | using System.ComponentModel.Composition; |
||
3 | using Touchless.Vision.Camera.Configuration; |
||
4 | using Touchless.Vision.Contracts; |
||
5 | |||
6 | namespace Touchless.Vision.Camera |
||
7 | { |
||
8 | [Export(typeof(IFrameSource))] |
||
9 | public class CameraFrameSource : IFrameSource |
||
10 | { |
||
11 | public event Action<IFrameSource, Frame, double> NewFrame; |
||
12 | |||
13 | public bool IsCapturing { get; private set; } |
||
14 | |||
15 | private Camera _camera; |
||
16 | public Camera Camera |
||
17 | { |
||
18 | get { return _camera; } |
||
19 | |||
20 | internal set |
||
21 | { |
||
22 | if (_camera != value) |
||
23 | { |
||
24 | bool restart = IsCapturing; |
||
25 | if (IsCapturing) |
||
26 | { |
||
27 | StopFrameCapture(); |
||
28 | } |
||
29 | |||
30 | _camera = value; |
||
31 | |||
32 | if (restart) |
||
33 | { |
||
34 | StartFrameCapture(); |
||
35 | } |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | |||
40 | [ImportingConstructor] |
||
41 | public CameraFrameSource([Import(ExportInterfaceNames.DefaultCamera)] Camera camera) |
||
42 | { |
||
43 | if (camera == null) throw new ArgumentNullException("camera"); |
||
44 | |||
45 | this.Camera = camera; |
||
46 | } |
||
47 | |||
48 | public bool StartFrameCapture() |
||
49 | { |
||
50 | bool result; |
||
51 | |||
52 | if (result = !IsCapturing && this.Camera != null) |
||
53 | { |
||
54 | this.Camera.OnImageCaptured += OnImageCaptured; |
||
55 | IsCapturing = result = this.Camera.StartCapture(); |
||
56 | } |
||
57 | |||
58 | return result; |
||
59 | } |
||
60 | |||
61 | public void StopFrameCapture() |
||
62 | { |
||
63 | if (IsCapturing) |
||
64 | { |
||
65 | this.Camera.StopCapture(); |
||
66 | this.Camera.OnImageCaptured -= OnImageCaptured; |
||
67 | this.IsCapturing = false; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | private void OnImageCaptured(object sender, CameraEventArgs e) |
||
72 | { |
||
73 | var handler = this.NewFrame; |
||
74 | if (IsCapturing && handler != null) |
||
75 | { |
||
76 | var frame = new Frame(e.Image); |
||
77 | handler(this, frame, e.CameraFps); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | public string Name |
||
82 | { |
||
83 | get { return "Touchless Camera Frame Source"; } |
||
84 | } |
||
85 | |||
86 | public string Description |
||
87 | { |
||
88 | get { return "Retrieves frames from Camera"; } |
||
89 | } |
||
90 | |||
91 | public bool HasConfiguration |
||
92 | { |
||
93 | get { return true; } |
||
94 | } |
||
95 | |||
96 | |||
97 | public System.Windows.UIElement ConfigurationElement |
||
98 | { |
||
99 | get |
||
100 | { |
||
101 | return new CameraFrameSourceConfigurationElement(this); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |