BoxEquip_Camera.cs
8.9 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
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)
camera_event?.Invoke(new CameraArgs(name, bmp));
Thread.Sleep(300);
}
}
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 + "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 监控保存
public const string boxACamName = "box_A";
public const string boxBCamName = "box_B";
public void StartRecord(string boxAFileName, string boxBFileName)
{
StartBoxARecord(boxAFileName);
StartBoxBRecord(boxBFileName);
}
public void StartBoxARecord(string filename)
{
IPCameraHelper.StartRecord(boxACamName, filename);
}
public void StartBoxBRecord(string filename)
{
IPCameraHelper.StartRecord(boxBCamName, filename);
}
public void StopBoxARecord()
{
IPCameraHelper.StopRecord(boxACamName);
}
public void StopBoxBRecord()
{
IPCameraHelper.StopRecord(boxBCamName);
}
public void StopRecord()
{
StopBoxARecord();
StopBoxBRecord();
}
#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 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()
{
StringBuilder sb = new StringBuilder(InputFolder);
StringBuilder sb1 = new StringBuilder(ExportCmd());
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(sb1.ToString() + "&exit");
p.StandardInput.AutoFlush = true;
p.StandardInput.Close();
p.StandardError.ReadToEnd();
p.WaitForExit();
try
{
System.IO.Directory.Delete(sb.ToString(), true);
}
catch (Exception ex)
{
LogUtil.error($"删除文件夹失败:{sb.ToString()}", ex);
}
}
});
return task;
}
}
}