IPCamera.cs
4.7 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
using DL.CV;
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Drawing;
using System.Threading.Tasks;
namespace Dolen.CV
{
public class IPCamera
{
private VideoCapture currentDevice;
private VideoWriter videoWriter;
private bool recording;
bool isLive;
private int videoWidth;
private int videoHeight;
public IPCameraConfig Config { get; set; }
///// <summary>
///// 记录保存的文件夹路径
///// </summary>
//public string RecordDirectory { get; set; } = "";
public int VideoWidth { get { return videoWidth; } }
public int VideoHeight { get { return videoHeight; } }
/// <summary>
/// 相机是否连接
/// </summary>
public bool CamIsOpened
{
get
{
if (currentDevice != null)
{
return currentDevice.IsOpened;
}
else
return false;
}
}
public delegate void ImageGrabbedEventHandler(IPCameraEventArgs eventArgs);
public event ImageGrabbedEventHandler ImageGrabbed;
public IPCamera(IPCameraConfig cameraConfig)
{
Config = cameraConfig;
}
private void InitializeVariables(string rtsp)
{
currentDevice = new VideoCapture(rtsp);
recording = false;
videoWidth = currentDevice.Width;
videoHeight = currentDevice.Height;
currentDevice.ImageGrabbed += CurrentDevice_ImageGrabbed;
}
int cnt = 0;
private void CurrentDevice_ImageGrabbed(object sender, EventArgs e)
{
try
{
if (cnt < 5)
{
cnt++;
return;
}
cnt = 0;
using (Mat m = new Mat())
{
if (currentDevice.Retrieve(m, 0))
{
ImageGrabbed?.Invoke(new IPCameraEventArgs(Config, m.ToImage<Bgr, Byte>().ToBitmap()));
if (recording && videoWriter != null)
{
videoWriter.Write(m);
}
}
}
}
catch
{
}
}
/// <summary>
/// 开始实时
/// </summary>
/// <returns></returns>
public bool StartLive()
{
if (isLive)
return false;
Task.Factory.StartNew(() =>
{
try
{
isLive = true;
InitializeVariables(Config.ToString());
currentDevice.Start();
return true;
}
catch
{
return false;
}
});
return false;
}
/// <summary>
/// 停止实时
/// </summary>
public void StopLive()
{
if (!isLive)
return;
Task.Factory.StartNew(() =>
{
currentDevice.Stop();
isLive = false;
});
}
/// <summary>
/// 开始记录
/// </summary>
/// <param name="filename">文件名,无后缀</param>
public void StartRecording(string filename)
{
if (recording)
return;
recording = true;
videoWriter = new VideoWriter($"{filename}.mp4", VideoWriter.Fourcc('M', 'P', '4', 'V'), 30, new System.Drawing.Size(videoWidth, videoHeight), true);
}
/// <summary>
/// 停止记录
/// </summary>
public bool StopRecording()
{
try
{
recording = false;
if (videoWriter != null)
{
videoWriter.Dispose();
}
return true;
}
catch
{
return false;
}
}
}
public class IPCameraEventArgs : EventArgs
{
public string Name { get; set; }
public Bitmap Image { get; set; }
public string IP { get; set; }
public int ChannelNum { get; set; }
public DateTime UpdateTime { get; set; }
public IPCameraEventArgs(IPCameraConfig config, Bitmap bitmap)
{
Name = config.Name;
IP = config.IP;
Image = bitmap;
ChannelNum = config.ChannelNum;
UpdateTime = DateTime.Now;
}
}
}