|
@@ -2,21 +2,16 @@
|
2
|
2
|
{
|
3
|
3
|
using System;
|
4
|
4
|
using System.Collections.Generic;
|
5
|
|
- using System.Drawing;
|
6
|
5
|
using System.Drawing.Imaging;
|
7
|
6
|
using System.IO;
|
8
|
7
|
using System.Windows.Media.Imaging;
|
9
|
8
|
using System.Windows.Threading;
|
10
|
|
- using AForge.Controls;
|
11
|
9
|
using AForge.Video.DirectShow;
|
12
|
10
|
|
13
|
11
|
public static class CameraHelper
|
14
|
12
|
{
|
15
|
13
|
private static List<FilterInfo> _cameraDevices;
|
16
|
|
- private static VideoCaptureDevice _div;
|
17
|
|
- private static readonly VideoSourcePlayer SourcePlayer = new VideoSourcePlayer();
|
18
|
|
-
|
19
|
|
- //指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false
|
|
14
|
+ private static VideoCaptureDevice _vedioCapture;
|
20
|
15
|
|
21
|
16
|
private static Dispatcher _dispatcher;
|
22
|
17
|
public static Action<BitmapImage> imageCallback;
|
|
@@ -68,15 +63,13 @@
|
68
|
63
|
return false;
|
69
|
64
|
}
|
70
|
65
|
// 设定初始视频设备
|
71
|
|
- _div = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
|
72
|
|
- _div.NewFrame += _div_NewFrame;
|
73
|
|
- SourcePlayer.VideoSource = _div;
|
74
|
|
- _div.Start();
|
75
|
|
- SourcePlayer.Start();
|
|
66
|
+ _vedioCapture = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
|
|
67
|
+ _vedioCapture.NewFrame += VedioCaptureNewFrame;
|
|
68
|
+ _vedioCapture.Start();
|
76
|
69
|
return true;
|
77
|
70
|
}
|
78
|
71
|
|
79
|
|
- private static void _div_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
|
|
72
|
+ private static void VedioCaptureNewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
|
80
|
73
|
{
|
81
|
74
|
_dispatcher?.Invoke
|
82
|
75
|
(
|
|
@@ -96,68 +89,25 @@
|
96
|
89
|
}
|
97
|
90
|
|
98
|
91
|
/// <summary>
|
99
|
|
- /// 截取一帧图像并保存
|
|
92
|
+ /// 关闭摄像头设备
|
100
|
93
|
/// </summary>
|
101
|
|
- /// <param name="filePath">图像保存路径</param>
|
102
|
|
- /// <param name="fileName">保存的图像文件名</param>
|
103
|
|
- /// <returns>如果保存成功,则返回完整路径,否则为 null</returns>
|
104
|
|
- public static string CaptureImage(string filePath, string fileName = null)
|
|
94
|
+ public static void CloseDevice()
|
105
|
95
|
{
|
106
|
|
- if (SourcePlayer.VideoSource == null)
|
107
|
|
- {
|
108
|
|
- return null;
|
109
|
|
- }
|
110
|
|
- if (!Directory.Exists(filePath))
|
111
|
|
- {
|
112
|
|
- Directory.CreateDirectory(filePath);
|
113
|
|
- }
|
114
|
96
|
try
|
115
|
97
|
{
|
116
|
|
- Image bitmap = SourcePlayer.GetCurrentVideoFrame();
|
117
|
|
- if (bitmap != null)
|
|
98
|
+ if (_vedioCapture != null)
|
118
|
99
|
{
|
119
|
|
- if (fileName == null)
|
|
100
|
+ _vedioCapture.NewFrame -= VedioCaptureNewFrame;
|
|
101
|
+ if (_vedioCapture.IsRunning)
|
120
|
102
|
{
|
121
|
|
- fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
|
|
103
|
+ _vedioCapture.SignalToStop();
|
122
|
104
|
}
|
123
|
|
- string fullPath = Path.Combine(filePath, fileName + "-cap.jpg");
|
124
|
|
- Bitmap objNewPic = new Bitmap
|
125
|
|
- (
|
126
|
|
- bitmap,
|
127
|
|
- 172,
|
128
|
|
- 124
|
129
|
|
- ); //图片保存的大小尺寸
|
130
|
|
- objNewPic.Save(fullPath, ImageFormat.Jpeg);
|
131
|
|
- //bitmap.Save(fullPath, ImageFormat.Jpeg);
|
132
|
|
- bitmap.Dispose();
|
133
|
|
- objNewPic.Dispose();
|
134
|
|
- return fullPath;
|
|
105
|
+ _vedioCapture = null;
|
135
|
106
|
}
|
136
|
|
- return null;
|
137
|
107
|
}
|
138
|
108
|
catch (Exception)
|
139
|
109
|
{
|
140
|
|
- return null;
|
141
|
|
- }
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- /// <summary>
|
145
|
|
- /// 关闭摄像头设备
|
146
|
|
- /// </summary>
|
147
|
|
- public static void CloseDevice()
|
148
|
|
- {
|
149
|
|
- if (_div != null)
|
150
|
|
- {
|
151
|
|
- _div.NewFrame -= _div_NewFrame;
|
152
|
|
- if (SourcePlayer.IsRunning)
|
153
|
|
- {
|
154
|
|
- SourcePlayer.Stop();
|
155
|
|
- }
|
156
|
|
- if (_div.IsRunning)
|
157
|
|
- {
|
158
|
|
- _div.SignalToStop();
|
159
|
|
- }
|
160
|
|
- _div = null;
|
|
110
|
+ // ignored
|
161
|
111
|
}
|
162
|
112
|
}
|
163
|
113
|
}
|