Camera.cs
5.0 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
using Asa.Camera;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore
{
public static class HIKCamera
{
static Thread camerathread;
static VisionLib camera;
static public string Name = "";
static public event EventHandler<Bitmap> camera_event;
static string DeviceName = "monitor1";
static public void LoadCameraConfig()
{
string path = @".\StoreConfig\Camera.json";
if (!File.Exists(path))
{
LogUtil.error(Name + "找不到监控相机配置文件" + path);
return;
}
try
{
var configtxt = File.ReadAllText(path);
if (configtxt.IndexOf("Pwd") > 0)
{
configtxt = configtxt.Replace("Pwd", "Password");
configtxt = configtxt.Replace("\"8000\"", "8000");
File.WriteAllText(path, configtxt);
}
var m = Regex.Match(configtxt, "name\".*?\"(.+)\"", RegexOptions.IgnoreCase);
DeviceName = m.Groups[1].Value;
camera = new VisionLib(path, "HIK.IPCamera");
}
catch (Exception e)
{
LogUtil.error(Name + "加载监控相机配置文件失败:" + e.ToString());
return;
}
camera.Open(DeviceName);
Bitmap bmp = camera.GetImage(DeviceName);
if (bmp == null)
{
LogUtil.error(Name + $"监控相机打开失败");
//camera.Close(DeviceName);
//Thread.Sleep(5000);
//camera.Open(DeviceName);
}
camerathread = new Thread(new ThreadStart(startCamera));
camerathread.Start();
GC.KeepAlive(camerathread);
}
static int errortimes = 0;
static void startCamera()
{
while (true)
{
try
{
Bitmap bmp = camera.GetImage(DeviceName);
if (bmp != null)
{
errortimes = 0;
camera_event?.Invoke(null, bmp);
}
else if (bmp == null && errortimes < 5)
{
errortimes++;
LogUtil.error(Name + $"相机获取图像出错,{errortimes}");
}
else if (errortimes == 5)
{
camera.Open(DeviceName);
errortimes++;
LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
}
else if (errortimes == 6)
{
//LogUtil.error(Name + $"相机连接失败, 相机线程退出,{errortimes}");
//break;
errortimes = 0;
LogUtil.error(Name + $"相机连接失败, 30秒后再次尝试,{errortimes}");
Thread.Sleep(30 * 1000);
camera.Open(DeviceName);
}
Thread.Sleep(1000 / 7);
}
catch
{
errortimes++;
}
}
camera.Close(DeviceName);
camera.Dispose();
}
static public void CameraGrabOne(string filename)
{
try
{
LogUtil.info(Name + "库位文件名:" + filename);
Bitmap bmp = camera.GetImage(DeviceName);
if (bmp != null)
{
if (File.Exists(filename))
File.Delete(filename);
bmp.Save(filename, ImageFormat.Jpeg);
bmp.Dispose();
}
}
catch (Exception e)
{
LogUtil.error(Name + e.ToString());
}
}
static public string GetFixtureStateFilename(string PosId,string PosCode, MoveType storeMoveType, FixtureState fixtureState)
{
string dir = $"\\image\\Fixture\\{storeMoveType}\\{PosId}\\";
Directory.CreateDirectory(dir);
string filename = $"{PosCode}@@{fixtureState}.jpg";
foreach (var x in Path.GetInvalidFileNameChars())
{
filename = filename.Replace(x.ToString(), "");
}
return dir + filename;
}
public enum FixtureState
{
In,
Up,
Down,
Out,
DoorIn,
DoorBack
}
}
}