forked from Kinovea/Kinovea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnapshotRetriever.cs
273 lines (222 loc) · 9.02 KB
/
SnapshotRetriever.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#region License
/*
Copyright © Joan Charmant 2017.
This file is part of Kinovea.
Kinovea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
Kinovea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kinovea. If not, see http://www.gnu.org/licenses/.
*/
#endregion
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using Kinovea.Pipeline;
using Kinovea.Services;
using static uEye.Extensions;
namespace Kinovea.Camera.IDS
{
/// <summary>
/// Retrieve a single snapshot, simulating a synchronous function. Used for thumbnails.
/// We use whatever settings are currently configured in the camera.
/// </summary>
public class SnapshotRetriever
{
public event EventHandler<CameraThumbnailProducedEventArgs> CameraThumbnailProduced;
public string Identifier
{
get { return this.summary.Identifier; }
}
public string Alias
{
get { return summary.Alias; }
}
public Thread Thread
{
get { return snapperThread; }
}
#region Members
private static readonly int timeoutGrabbing = 1000;
private static readonly int timeoutOpening = 100;
private Bitmap image;
private ImageDescriptor imageDescriptor = ImageDescriptor.Invalid;
private CameraSummary summary;
private EventWaitHandle waitHandle = new AutoResetEvent(false);
private uEye.Camera camera = new uEye.Camera();
private bool cancelled;
private bool hadError;
private Thread snapperThread;
private object locker = new object();
private Stopwatch stopwatch = new Stopwatch();
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
public SnapshotRetriever(CameraSummary summary, long deviceId)
{
this.summary = summary;
camera.EventFrame += camera_EventFrame;
camera.EventDeviceRemove += camera_EventDeviceRemove;
camera.EventDeviceUnPlugged += camera_EventDeviceUnPlugged;
try
{
stopwatch.Start();
uEye.Defines.Status status = camera.Init((Int32)deviceId | (Int32)uEye.Defines.DeviceEnumeration.UseDeviceID);
if (status != uEye.Defines.Status.SUCCESS)
{
log.ErrorFormat("Camera {0} could not be opened for thumbnail capture.", summary.Alias);
return;
}
log.DebugFormat("{0} initialized in {1} ms.", summary.Alias, stopwatch.ElapsedMilliseconds);
stopwatch.Stop();
// We do not load the camera-specific profile for the thumbnail at the moment.
// For some reason the .ToBitmap method doesn't work well on the RGB32 format, so in order to at least have something we
// load the camera on the default profile for the thumbnail.
status = camera.Memory.Allocate();
if (status != uEye.Defines.Status.SUCCESS)
{
log.ErrorFormat("Camera {0} could not have its buffer allocated for thumbnail capture.", summary.Alias);
return;
}
}
catch (Exception e)
{
LogError(e, "");
}
}
public void Start()
{
snapperThread = new Thread(Run) { IsBackground = true };
snapperThread.Name = string.Format("{0} thumbnailer", summary.Alias);
snapperThread.Start();
}
/// <summary>
/// Start the device for a frame grab, wait a bit and then return the result.
/// This method MUST raise a CameraThumbnailProduced event, even in case of error.
/// </summary>
public void Run(object data)
{
log.DebugFormat("Starting {0} for thumbnail.", summary.Alias);
if (!camera.IsOpened)
Thread.Sleep(timeoutOpening);
if (!camera.IsOpened)
{
if (CameraThumbnailProduced != null)
CameraThumbnailProduced(this, new CameraThumbnailProducedEventArgs(summary, null, ImageDescriptor.Invalid, true, false));
return;
}
try
{
uEye.Defines.Status statusRet = camera.Acquisition.Freeze();
}
catch (Exception e)
{
LogError(e, "");
}
waitHandle.WaitOne(timeoutGrabbing, false);
lock (locker)
{
if (!cancelled)
{
camera.EventFrame -= camera_EventFrame;
camera.EventDeviceRemove -= camera_EventDeviceRemove;
camera.EventDeviceUnPlugged -= camera_EventDeviceUnPlugged;
Close();
}
}
if (CameraThumbnailProduced != null)
CameraThumbnailProduced(this, new CameraThumbnailProducedEventArgs(summary, image, imageDescriptor, hadError, cancelled));
}
public void Cancel()
{
log.DebugFormat("Cancelling thumbnail for {0}.", Alias);
if (!camera.IsOpened)
return;
lock (locker)
{
camera.EventFrame -= camera_EventFrame;
camera.EventDeviceRemove -= camera_EventDeviceRemove;
camera.EventDeviceUnPlugged -= camera_EventDeviceUnPlugged;
Close();
cancelled = true;
}
waitHandle.Set();
}
private void Close()
{
try
{
camera.Exit();
}
catch (Exception e)
{
LogError(e, "");
}
}
private void LogError(Exception e, string additionalErrorMessage)
{
log.ErrorFormat("Camera {0} failure during thumbnail capture.", summary.Alias);
log.Error(e.ToString());
if (!string.IsNullOrEmpty(additionalErrorMessage))
log.Error(additionalErrorMessage);
}
#region Camera events
private void camera_EventFrame(object sender, EventArgs e)
{
uEye.Camera camera = sender as uEye.Camera;
if (camera == null || !camera.IsOpened)
{
waitHandle.Set();
return;
}
uEye.Defines.DisplayMode mode;
camera.Display.Mode.Get(out mode);
if (mode != uEye.Defines.DisplayMode.DiB)
{
waitHandle.Set();
return;
}
Int32 s32MemID;
camera.Memory.GetActive(out s32MemID);
camera.Memory.Lock(s32MemID);
Bitmap bitmap;
camera.Memory.ToBitmap(s32MemID, out bitmap);
if (bitmap != null)
{
if (image != null)
image.Dispose();
// Force output into an RGB24 bitmap.
image = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(bitmap, Point.Empty);
}
int bufferSize = ImageFormatHelper.ComputeBufferSize(image.Width, image.Height, Kinovea.Services.ImageFormat.RGB24);
imageDescriptor = new ImageDescriptor(Kinovea.Services.ImageFormat.RGB24, image.Width, image.Height, true, bufferSize);
bitmap.Dispose();
}
camera.Memory.Unlock(s32MemID);
waitHandle.Set();
}
private void camera_EventDeviceUnPlugged(object sender, EventArgs e)
{
hadError = true;
log.ErrorFormat("Camera {0} unplugged during thumbnail capture.", summary.Alias);
waitHandle.Set();
}
private void camera_EventDeviceRemove(object sender, EventArgs e)
{
hadError = true;
log.ErrorFormat("Camera {0} removed during thumbnail capture.", summary.Alias);
waitHandle.Set();
}
#endregion
}
}