-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCameraService.cs
More file actions
584 lines (507 loc) · 18 KB
/
CameraService.cs
File metadata and controls
584 lines (507 loc) · 18 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
namespace ShowWrite
{
/// <summary>
/// 摄像头服务类,直接使用 OpenCVSharp 捕获摄像头,提供帧数据和透视变换功能。
/// </summary>
public class CameraService : IDisposable
{
private VideoCapture? _capture;
private CancellationTokenSource? _cts;
private Task? _captureTask;
private volatile bool _cancelled = false;
private readonly object _lock = new();
private volatile bool _connectCancelled = false;
private WriteableBitmap? _frameBitmap;
private Mat? _latestFrame;
private Mat? _processedFrame;
private int _frameWidth;
private int _frameHeight;
private int _frameStride;
private DispatcherTimer? _uiTimer;
private int _currentCameraIndex = 0;
private List<int> _availableCameraIndices = new();
private Config _config = new(); // 使用全局 Config
private Mat? _perspectiveMatrix;
private bool _hasPerspectiveTransform = false;
private Point2f[]? _sourcePoints;
private Point2f[]? _destinationPoints;
// 公共属性
public WriteableBitmap? FrameBitmap => _frameBitmap;
public Mat? LatestFrame => _latestFrame;
public int CameraCount => _availableCameraIndices.Count;
public int CurrentCameraIndex => _currentCameraIndex;
public bool HasPerspectiveTransform => _hasPerspectiveTransform;
public bool IsConnected
{
get
{
lock (_lock)
{
return _capture != null && !_cancelled;
}
}
}
/// <summary>
/// 获取处理后的帧(应用了透视变换)
/// </summary>
public Mat? GetProcessedFrame()
{
lock (_lock)
{
if (_latestFrame == null || _latestFrame.Empty())
return null;
if (_hasPerspectiveTransform && _perspectiveMatrix != null)
{
return ApplyPerspectiveTransform(_latestFrame);
}
return _latestFrame;
}
}
// 事件
public event Action<string>? ErrorOccurred;
public event Action? FrameReady;
public event Action? CameraStarted;
public event Action? ScanComplete;
public event Action? UsingCachedCameras;
/// <summary>
/// 检测并连接摄像头(基于缓存配置或扫描)
/// </summary>
public void DetectAndConnectCamera()
{
_connectCancelled = false;
_config = Config.Load(); // 使用全局 Config 的静态方法
if (_config.AvailableCameraIndices.Count > 0)
{
_availableCameraIndices = new List<int>(_config.AvailableCameraIndices);
_currentCameraIndex = _config.CurrentCameraIndex;
if (_currentCameraIndex >= _availableCameraIndices.Count)
_currentCameraIndex = 0;
if (_availableCameraIndices.Count > 0)
{
var cameraIdx = _availableCameraIndices[_currentCameraIndex];
UsingCachedCameras?.Invoke();
if (!_connectCancelled)
{
StartCapture(cameraIdx);
}
return;
}
}
ScanCameras();
}
/// <summary>
/// 扫描可用摄像头(0-4)
/// </summary>
public void ScanCameras()
{
Task.Run(() =>
{
var foundCameras = new List<int>();
for (int i = 0; i < 5; i++)
{
try
{
using var test = new VideoCapture(i, VideoCaptureAPIs.MSMF);
if (test.IsOpened())
foundCameras.Add(i);
}
catch { }
}
_availableCameraIndices = foundCameras;
_config.AvailableCameraIndices = foundCameras;
_config.LastScanTime = DateTime.Now;
_config.Save(); // 保存到全局配置
Dispatcher.UIThread.Post(() =>
{
ScanComplete?.Invoke();
if (_availableCameraIndices.Count > 0)
{
_currentCameraIndex = 0;
_config.CurrentCameraIndex = 0;
_config.Save();
if (!_connectCancelled)
{
StartCapture(_availableCameraIndices[0]);
}
}
else
{
ErrorOccurred?.Invoke("未检测到摄像头");
}
});
});
}
public List<string> GetAvailableCameraNames()
{
var names = new List<string>();
var deviceNames = GetCameraDeviceNames();
for (int i = 0; i < _availableCameraIndices.Count; i++)
{
int idx = _availableCameraIndices[i];
if (deviceNames.TryGetValue(idx, out string? deviceName))
{
names.Add(deviceName);
}
else
{
names.Add($"摄像头 {idx}");
}
}
return names;
}
private Dictionary<int, string> GetCameraDeviceNames()
{
var result = new Dictionary<int, string>();
try
{
using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE PNPClass='Camera' OR PNPClass='Image'");
int index = 0;
foreach (ManagementObject obj in searcher.Get())
{
string? name = obj["Name"]?.ToString();
if (!string.IsNullOrEmpty(name))
{
result[index] = name;
index++;
}
}
}
catch
{
}
return result;
}
public int GetCameraIndexByName(string name)
{
var deviceNames = GetCameraDeviceNames();
for (int i = 0; i < _availableCameraIndices.Count; i++)
{
int idx = _availableCameraIndices[i];
if (deviceNames.TryGetValue(idx, out string? deviceName) && deviceName == name)
return idx;
if ($"摄像头 {idx}" == name)
return idx;
}
return -1;
}
/// <summary>
/// 启动指定索引的摄像头
/// </summary>
public void StartCapture(int cameraIndex)
{
if (_connectCancelled) return;
lock (_lock)
{
StopCaptureInternal();
_cancelled = false;
}
Task.Run(() =>
{
VideoCapture? capture = null;
Mat? mat = null;
try
{
capture = new VideoCapture(cameraIndex, VideoCaptureAPIs.MSMF);
if (!capture.IsOpened())
{
Dispatcher.UIThread.Post(() =>
{
ErrorOccurred?.Invoke($"无法打开摄像头 {cameraIndex}");
});
return;
}
// 设置分辨率(可选)
capture.Set(VideoCaptureProperties.FrameWidth, 1280);
capture.Set(VideoCaptureProperties.FrameHeight, 720);
capture.Set(VideoCaptureProperties.Fps, 30);
var width = (int)capture.Get(VideoCaptureProperties.FrameWidth);
var height = (int)capture.Get(VideoCaptureProperties.FrameHeight);
mat = new Mat(height, width, MatType.CV_8UC3);
// 丢弃前几帧(让摄像头稳定)
for (int i = 0; i < 5; i++)
{
if (_cancelled)
{
capture?.Release();
capture?.Dispose();
mat?.Dispose();
return;
}
capture.Read(mat);
}
Dispatcher.UIThread.Post(() =>
{
lock (_lock)
{
if (_cancelled)
{
capture?.Release();
capture?.Dispose();
mat?.Dispose();
return;
}
_capture = capture;
_latestFrame = mat;
_frameWidth = width;
_frameHeight = height;
_frameStride = width * 3;
_frameBitmap = new WriteableBitmap(
new PixelSize(width, height),
new Vector(96, 96),
PixelFormats.Bgr24);
_cts = new CancellationTokenSource();
_captureTask = Task.Run(() => CaptureLoop(_cts.Token));
StartUiTimer();
CameraStarted?.Invoke();
}
});
}
catch (Exception ex)
{
capture?.Release();
capture?.Dispose();
mat?.Dispose();
Dispatcher.UIThread.Post(() =>
{
ErrorOccurred?.Invoke($"启动摄像头失败: {ex.Message}");
});
}
});
}
/// <summary>
/// 捕获循环(后台线程)
/// </summary>
private void CaptureLoop(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
lock (_lock)
{
if (_capture == null || _latestFrame == null || _cancelled)
break;
_capture.Read(_latestFrame);
}
// 控制帧率约 60fps
Thread.Sleep(16);
}
catch (Exception ex)
{
Dispatcher.UIThread.Post(() =>
{
ErrorOccurred?.Invoke($"捕获循环异常: {ex.Message}");
});
break;
}
}
}
/// <summary>
/// 启动 UI 定时器,定期将最新帧绘制到 WriteableBitmap
/// </summary>
private void StartUiTimer()
{
_uiTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(16) // 约 60fps
};
_uiTimer.Tick += (_, _) => UpdateFrame();
_uiTimer.Start();
}
/// <summary>
/// 更新 UI 帧(应用透视变换并复制到 Bitmap)
/// </summary>
private unsafe void UpdateFrame()
{
lock (_lock)
{
if (_frameBitmap == null || _latestFrame == null || _cancelled)
return;
if (_latestFrame.Empty())
return;
Mat frameToDisplay = _latestFrame;
bool shouldDispose = false;
// 应用透视变换(如果需要)
if (_hasPerspectiveTransform && _perspectiveMatrix != null)
{
frameToDisplay = ApplyPerspectiveTransform(_latestFrame);
shouldDispose = true;
}
using var locked = _frameBitmap.Lock();
var srcPtr = (void*)frameToDisplay.DataPointer;
var dstPtr = (void*)locked.Address;
if (locked.RowBytes == _frameStride)
{
Buffer.MemoryCopy(srcPtr, dstPtr,
_frameStride * _frameHeight,
_frameStride * _frameHeight);
}
else
{
for (int i = 0; i < _frameHeight; i++)
{
Buffer.MemoryCopy(
(byte*)srcPtr + i * _frameStride,
(byte*)dstPtr + i * locked.RowBytes,
_frameStride,
_frameStride);
}
}
if (shouldDispose)
{
frameToDisplay.Dispose();
}
FrameReady?.Invoke();
}
}
/// <summary>
/// 应用透视变换
/// </summary>
private Mat ApplyPerspectiveTransform(Mat input)
{
if (!_hasPerspectiveTransform || _perspectiveMatrix == null)
return input;
var output = new Mat();
Cv2.WarpPerspective(input, output, _perspectiveMatrix, input.Size());
return output;
}
/// <summary>
/// 停止捕获
/// </summary>
public void StopCapture()
{
lock (_lock)
{
StopCaptureInternal();
}
}
/// <summary>
/// 取消正在进行的连接
/// </summary>
public void CancelConnecting()
{
_connectCancelled = true;
StopCapture();
}
private void StopCaptureInternal()
{
_cancelled = true;
_uiTimer?.Stop();
_uiTimer = null;
_cts?.Cancel();
_captureTask = null;
_cts?.Dispose();
_cts = null;
_capture?.Release();
_capture?.Dispose();
_capture = null;
_latestFrame?.Dispose();
_latestFrame = null;
}
/// <summary>
/// 切换到下一个可用摄像头
/// </summary>
public void SwitchCamera()
{
if (_availableCameraIndices.Count <= 1)
return;
_currentCameraIndex = (_currentCameraIndex + 1) % _availableCameraIndices.Count;
_config.CurrentCameraIndex = _currentCameraIndex;
_config.Save();
StartCapture(_availableCameraIndices[_currentCameraIndex]);
}
/// <summary>
/// 切换到指定索引的摄像头
/// </summary>
public void SwitchToCamera(int cameraIndex)
{
if (_availableCameraIndices.Count == 0)
return;
var pos = _availableCameraIndices.IndexOf(cameraIndex);
if (pos < 0)
return;
_currentCameraIndex = pos;
_config.CurrentCameraIndex = _currentCameraIndex;
_config.Save();
StartCapture(cameraIndex);
}
// ---------- 透视变换相关 ----------
public void SetPerspectiveTransform(Point2f[] sourcePoints, Point2f[] destPoints)
{
lock (_lock)
{
_sourcePoints = sourcePoints;
_destinationPoints = destPoints;
if (_sourcePoints != null && _destinationPoints != null &&
_sourcePoints.Length == 4 && _destinationPoints.Length == 4)
{
_perspectiveMatrix?.Dispose();
_perspectiveMatrix = Cv2.GetPerspectiveTransform(_sourcePoints, _destinationPoints);
_hasPerspectiveTransform = true;
}
else
{
_perspectiveMatrix?.Dispose();
_perspectiveMatrix = null;
_hasPerspectiveTransform = false;
}
}
}
public void ResetPerspectiveTransform()
{
lock (_lock)
{
_perspectiveMatrix?.Dispose();
_perspectiveMatrix = null;
_hasPerspectiveTransform = false;
_sourcePoints = null;
_destinationPoints = null;
}
}
public Point2f[]? GetSourcePoints()
{
return _sourcePoints;
}
public Point2f[] GetDefaultSourcePoints(int width, int height)
{
var margin = 50;
return new Point2f[]
{
new Point2f(margin, margin),
new Point2f(width - margin, margin),
new Point2f(width - margin, height - margin),
new Point2f(margin, height - margin)
};
}
public Point2f[] GetDefaultDestPoints(int width, int height)
{
return new Point2f[]
{
new Point2f(0, 0),
new Point2f(width, 0),
new Point2f(width, height),
new Point2f(0, height)
};
}
// ---------- 资源释放 ----------
public void Dispose()
{
StopCapture();
_frameBitmap?.Dispose();
_processedFrame?.Dispose();
_perspectiveMatrix?.Dispose();
}
}
}