|
@@ -1,25 +1,28 @@
|
1
|
1
|
using System;
|
|
2
|
+using System.Drawing.Imaging;
|
|
3
|
+using System.Drawing;
|
2
|
4
|
using System.Runtime.InteropServices;
|
3
|
|
-
|
4
|
5
|
using Accord.Video;
|
5
|
6
|
using Accord.Video.FFMPEG;
|
6
|
7
|
|
7
|
8
|
namespace XHWK.WKTool.Utils
|
8
|
9
|
{
|
|
10
|
+ using System.Linq;
|
|
11
|
+
|
9
|
12
|
public class ZVideoRecordHelper
|
10
|
13
|
{
|
11
|
14
|
public enum RecordState
|
12
|
15
|
{
|
13
|
|
- stop = 0,
|
14
|
|
- start = 1,
|
15
|
|
- pause = 2
|
|
16
|
+ Stop = 0,
|
|
17
|
+ Start = 1,
|
|
18
|
+ Pause = 2
|
16
|
19
|
}
|
17
|
20
|
|
18
|
|
- private RecordState _state = RecordState.stop;
|
|
21
|
+ private RecordState _state = RecordState.Stop;
|
19
|
22
|
|
20
|
|
- private string _filePath = "";
|
21
|
|
- private int _width = 0;
|
22
|
|
- private int _height = 0;
|
|
23
|
+ private string _filePath;
|
|
24
|
+ private int _width;
|
|
25
|
+ private int _height;
|
23
|
26
|
|
24
|
27
|
//录制帧率
|
25
|
28
|
private int _rate = 5;
|
|
@@ -40,8 +43,7 @@ namespace XHWK.WKTool.Utils
|
40
|
43
|
_rate = rate;
|
41
|
44
|
_quality = quality;
|
42
|
45
|
_captureMouse = captureMouse;
|
43
|
|
-
|
44
|
|
- _state = RecordState.pause;
|
|
46
|
+ _state = RecordState.Pause;
|
45
|
47
|
try
|
46
|
48
|
{
|
47
|
49
|
// 打开写入
|
|
@@ -72,7 +74,7 @@ namespace XHWK.WKTool.Utils
|
72
|
74
|
/// </summary>
|
73
|
75
|
public bool StartRecordVideo()
|
74
|
76
|
{
|
75
|
|
- _state = RecordState.start;
|
|
77
|
+ _state = RecordState.Start;
|
76
|
78
|
return true;
|
77
|
79
|
}
|
78
|
80
|
|
|
@@ -98,7 +100,7 @@ namespace XHWK.WKTool.Utils
|
98
|
100
|
// 帧返回时绘制上鼠标
|
99
|
101
|
private void VideoNewFrame(object sender, NewFrameEventArgs e)
|
100
|
102
|
{
|
101
|
|
- if (_state == RecordState.start)
|
|
103
|
+ if (_state == RecordState.Start)
|
102
|
104
|
{
|
103
|
105
|
if (_captureMouse)
|
104
|
106
|
{
|
|
@@ -109,9 +111,11 @@ namespace XHWK.WKTool.Utils
|
109
|
111
|
try
|
110
|
112
|
{
|
111
|
113
|
System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);
|
112
|
|
- cur.Draw(
|
|
114
|
+ cur.Draw
|
|
115
|
+ (
|
113
|
116
|
g,
|
114
|
|
- new System.Drawing.Rectangle(
|
|
117
|
+ new System.Drawing.Rectangle
|
|
118
|
+ (
|
115
|
119
|
System.Windows.Forms.Cursor.Position.X - 10,
|
116
|
120
|
System.Windows.Forms.Cursor.Position.Y - 10,
|
117
|
121
|
cur.Size.Width,
|
|
@@ -119,7 +123,7 @@ namespace XHWK.WKTool.Utils
|
119
|
123
|
)
|
120
|
124
|
);
|
121
|
125
|
}
|
122
|
|
- catch { }//打开任务管理器时会导致异常
|
|
126
|
+ catch { } //打开任务管理器时会导致异常
|
123
|
127
|
}
|
124
|
128
|
videoWriter.WriteVideoFrame(e.Frame);
|
125
|
129
|
}
|
|
@@ -130,7 +134,7 @@ namespace XHWK.WKTool.Utils
|
130
|
134
|
/// </summary>
|
131
|
135
|
public void StopRecordVideo()
|
132
|
136
|
{
|
133
|
|
- _state = RecordState.stop;
|
|
137
|
+ _state = RecordState.Stop;
|
134
|
138
|
// 停止
|
135
|
139
|
videoStreamer.Stop();
|
136
|
140
|
|
|
@@ -144,7 +148,7 @@ namespace XHWK.WKTool.Utils
|
144
|
148
|
/// </summary>
|
145
|
149
|
public void PauseRecordVideo()
|
146
|
150
|
{
|
147
|
|
- _state = RecordState.pause;
|
|
151
|
+ _state = RecordState.Pause;
|
148
|
152
|
}
|
149
|
153
|
|
150
|
154
|
/// <summary>
|
|
@@ -152,7 +156,50 @@ namespace XHWK.WKTool.Utils
|
152
|
156
|
/// </summary>
|
153
|
157
|
public void ResumeRecordVideo()
|
154
|
158
|
{
|
155
|
|
- _state = RecordState.start;
|
|
159
|
+ _state = RecordState.Start;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ /// <summary>
|
|
163
|
+ /// 生成缩略图
|
|
164
|
+ /// </summary>
|
|
165
|
+ /// <param name="mp4Path"></param>
|
|
166
|
+ /// <param name="picPath"></param>
|
|
167
|
+ public static void GetVideoPic(string mp4Path, string picPath)
|
|
168
|
+ {
|
|
169
|
+ using (var video = new VideoFileReader())
|
|
170
|
+ {
|
|
171
|
+ video.Open(mp4Path);
|
|
172
|
+ int frameNum = video.FrameCount > 3 ? 3 : 0;
|
|
173
|
+ using (var bmp = video.ReadVideoFrame(frameNum))
|
|
174
|
+ {
|
|
175
|
+ SaveAsJpeg
|
|
176
|
+ (
|
|
177
|
+ bmp,
|
|
178
|
+ picPath,
|
|
179
|
+ 90
|
|
180
|
+ );
|
|
181
|
+ }
|
|
182
|
+ video.Close();
|
|
183
|
+ }
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ // 保存为jpeg格式,quality为图像质量(0-100)
|
|
187
|
+ public static void SaveAsJpeg
|
|
188
|
+ (
|
|
189
|
+ Bitmap bitmap,
|
|
190
|
+ string fileName,
|
|
191
|
+ int quality
|
|
192
|
+ )
|
|
193
|
+ {
|
|
194
|
+ var jpegEncoder = ImageCodecInfo.GetImageEncoders().First(codec => codec.FormatID == ImageFormat.Jpeg.Guid);
|
|
195
|
+ var encoderParameters = new EncoderParameters(1);
|
|
196
|
+ encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
|
|
197
|
+ bitmap.Save
|
|
198
|
+ (
|
|
199
|
+ fileName,
|
|
200
|
+ jpegEncoder,
|
|
201
|
+ encoderParameters
|
|
202
|
+ );
|
156
|
203
|
}
|
157
|
204
|
}
|
158
|
|
-}
|
|
205
|
+}
|