BoxEquip_Camera.cs
13.1 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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
partial class BoxEquip
{
Thread[] camerathread;
bool IsOpen = false;
bool AutoSaveImage = true;
Asa.Camera.VisionLib camera;
bool loadCameraState = false;
public delegate void CameraAcqEventHandler(CameraArgs cameraArgs);
public event CameraAcqEventHandler camera_event;
void LoadCameraConfig(string id = "")
{
if (loadCameraState)
return;
string path = @".\Config\Camera.json";
if (!File.Exists(path))
{
LogUtil.error(Name + "找不到监控相机配置文件" + path);
}
camera = new Asa.Camera.VisionLib(path);
camerathread = new Thread[2];
//pictureBox1.Image = bmp;
StartCamera();
loadCameraState = true;
}
void StartCamera()
{
camera.Open("box_A");
camera.Open("box_B");
IsOpen = true;
camerathread[0] = new Thread(new ParameterizedThreadStart(startMonitor));
camerathread[0].IsBackground = true;
camerathread[0].Start("box_A");
camerathread[1] = new Thread(new ParameterizedThreadStart(startMonitor));
camerathread[1].IsBackground = true;
camerathread[1].Start("box_B");
}
void startMonitor(object obj)
{
if (!loadCameraState)
{
LogUtil.error(Name + "监控相机初始化失败,无法开启");
return;
}
string name = (string)obj;
while (IsOpen)
{
Bitmap bmp = AcqImage(name);
if (bmp != null)
{
if (IsRecord)
{
cnt++;
Task.Factory.StartNew(delegate
{
SaveImage("box_A", cnt);
});
Task.Factory.StartNew(delegate
{
SaveImage("box_B", cnt);
});
}
camera_event?.Invoke(new CameraArgs(name, bmp));
}
Thread.Sleep(300);
}
}
void StopCamera()
{
IsOpen = false;
camera.Close();
camera.Dispose();
}
public Bitmap AcqImage(string camName)
{
Bitmap bitmap = camera.GetImage(camName);
return bitmap;
}
string imgPath = ConfigAppSettings.GetValue(Setting_Init.ImagePath);
//public void SaveImage(string camName)
//{
// try
// {
// if (MoveInfo.MoveParam == null)
// {
// string path = Application.StartupPath + imgPath + camName + "\\" + DateTime.Now.ToString("yyyyMMdd");
// if (AutoSaveImage)
// {
// if (!System.IO.Directory.Exists(path))
// Directory.CreateDirectory(path);
// camera.SaveImage(camName, path, DateTime.Now.ToString("hhmmssfff"), System.Drawing.Imaging.ImageFormat.Bmp);
// }
// }
// else
// {
// if (MoveInfo.MoveParam.PosInfo != null)
// {
// InOutPosInfo inOutPosInfo = MoveInfo.MoveParam.PosInfo;
// string path = Application.StartupPath + imgPath + camName + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
// if (AutoSaveImage)
// {
// if (!System.IO.Directory.Exists(path))
// Directory.CreateDirectory(path);
// camera.SaveImage(camName, path, $"{inOutPosInfo.barcode}-{MoveInfo.MoveType}-{DateTime.Now.ToString("hhmmssfff")}", System.Drawing.Imaging.ImageFormat.Bmp);
// }
// }
// }
// }
// catch (Exception ex)
// {
// LogUtil.error($"保存{camName}图片失败", ex);
// }
//}
public void SaveImage(string camName)
{
try
{
if (MoveInfo.MoveParam == null)
{
string path = Application.StartupPath + imgPath + "Records\\" + DateTime.Now.ToString("yyyyMMdd");
if (AutoSaveImage)
{
if (!System.IO.Directory.Exists(path))
Directory.CreateDirectory(path);
Task.Factory.StartNew(() =>
{
camera.SaveImage(camName, path, $"{camName}-{DateTime.Now.ToString("hhmmssfff")}", System.Drawing.Imaging.ImageFormat.Bmp);
});
}
}
else
{
if (MoveInfo.MoveParam.PosInfo != null)
{
InOutPosInfo inOutPosInfo = MoveInfo.MoveParam.PosInfo;
string path = Application.StartupPath + imgPath + "Records\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
if (AutoSaveImage)
{
if (!System.IO.Directory.Exists(path))
Directory.CreateDirectory(path);
Task.Factory.StartNew(() =>
{
camera.SaveImage(camName, path, $"{camName}-{inOutPosInfo.barcode}-{MoveInfo.MoveType}-{DateTime.Now.ToString("hhmmssfff")}", System.Drawing.Imaging.ImageFormat.Bmp);
});
}
}
}
}
catch (Exception ex)
{
LogUtil.error($"保存{camName}图片失败", ex);
}
}
#region 监控保存
void SaveImage(string camName, int count)
{
try
{
if (inOutPosInfo != null)
{
string path = Application.StartupPath + imgPath + camName + "\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
if (!System.IO.Directory.Exists(path))
Directory.CreateDirectory(path);
Task.Factory.StartNew(() =>
{
camera.SaveImage(camName, path, $"{count.ToString().PadLeft(4, '0')}", System.Drawing.Imaging.ImageFormat.Bmp);
});
}
}
catch (Exception ex)
{
LogUtil.error($"保存{camName}图片失败", ex);
}
}
FFMPEG ffmpegA = new FFMPEG();
FFMPEG ffmpegB = new FFMPEG();
bool IsRecord = false;
int cnt = 0;
InOutPosInfo inOutPosInfo = null;
/// <summary>
/// 开始记录
/// </summary>
public void StartRecord(bool isTest = false)
{
if (isTest)
{
inOutPosInfo = new InOutPosInfo("code" + DateTime.Now.ToString("mmssfff"), DateTime.Now.ToString("yyyyMMddhh"));
}
else
{
inOutPosInfo = MoveInfo.MoveParam.PosInfo;
}
cnt = 0;
string inputfolderA = Application.StartupPath + imgPath + "box_A\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
string inputfolderB = Application.StartupPath + imgPath + "box_B\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
string outputfolderA = Application.StartupPath + "\\Videos\\box_A\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
string outputfolderB = Application.StartupPath + "\\Videos\\box_B\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + inOutPosInfo.PosId;
ffmpegA.SetParam(inputfolderA, outputfolderA, $"{DateTime.Now.ToString("hhmmss")}_{inOutPosInfo.barcode}.mp4");
ffmpegB.SetParam(inputfolderB, outputfolderB, $"{DateTime.Now.ToString("hhmmss")}_{inOutPosInfo.barcode}.mp4");
IsRecord = true;
}
/// <summary>
/// 停止记录
/// </summary>
public void StopRecord()
{
IsRecord = false;
Task task1 = ffmpegA.ConvertImgsToMp4();
Task task2 = ffmpegB.ConvertImgsToMp4();
Task.WaitAll(new Task[] { task1, task2 }, TimeSpan.FromMinutes(1));
Task.Factory.StartNew(delegate {
try
{
System.IO.Directory.Delete(ffmpegA.InputFolder, true);
}
catch (Exception ex)
{
LogUtil.error($"删除文件夹失败:{ffmpegA.InputFolder}", ex);
}
});
Task.Factory.StartNew(delegate {
try
{
System.IO.Directory.Delete(ffmpegB.InputFolder, true);
}
catch (Exception ex)
{
LogUtil.error($"删除文件夹失败:{ffmpegB.InputFolder}", ex);
}
});
}
#endregion
}
public class CameraArgs : EventArgs
{
/// <summary>
/// 相机名
/// </summary>
public string CamName { get; set; }
/// <summary>
/// 相机采集的图片
/// </summary>
public Bitmap Image { get; set; }
public CameraArgs(string name, Bitmap img)
{
CamName = name;
Image = img;
}
}
public class FFMPEG
{
public static string appFolderPath = Application.StartupPath;
static string imgPath = ConfigAppSettings.GetValue(Setting_Init.ImagePath);
/// <summary>
/// 帧频
/// </summary>
public int FrameRate { get; set; } = 2;
/// <summary>
/// 画面宽
/// </summary>
public int Width { get; set; } = 1920;
/// <summary>
/// 画面高
/// </summary>
public int Height { get; set; } = 1080;
/// <summary>
/// 输入文件夹
/// </summary>
public string InputFolder { get; set; } = appFolderPath + imgPath;
public string InputFileNamePrefix { get; set; } = "";
/// <summary>
/// 输出文件夹
/// </summary>
public string OutputFolder { get; set; } = appFolderPath + "Videos\\";
/// <summary>
/// 输出文件名带后缀
/// </summary>
public string OutputFileName { get; set; } = "test.mp4";
public string Cmd { get { return ExportCmd(); } }
public void SetParam(string inputfolder, string outputfolder, string outputfilename)
{
InputFolder = inputfolder;
OutputFolder = outputfolder;
OutputFileName = outputfilename;
if (!Directory.Exists(outputfolder))
Directory.CreateDirectory(outputfolder);
}
string ExportCmd()
{
StringBuilder sb = new StringBuilder();
sb.Append(appFolderPath + "\\ffmpeg ");
sb.Append($" -y -r {FrameRate} -f image2 -s {Width}*{Height} -i {InputFolder}\\{InputFileNamePrefix}%04d.Bmp -vcodec libx264 -pix_fmt yuv420p ");
sb.Append(OutputFolder + "\\" + OutputFileName);
return sb.ToString();
}
public Task ConvertImgsToMp4()
{
Task task = Task.Factory.StartNew(delegate
{
using (Process p = new Process())
{
//(1)设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//p.StartInfo.FileName = appFolderPath + "\\ffmpeg.exe";
//p.StartInfo.Arguments = Cmd +" & exit /b";
//(2)是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
//(3)接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//(4)输出信息
p.StartInfo.RedirectStandardOutput = true;
//(5)输出错误
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(Cmd + "&exit");
p.StandardInput.AutoFlush = true;
p.StandardInput.Close();
p.StandardError.ReadToEnd();
p.WaitForExit();
}
});
return task;
}
}
}