Camera.cs
3.5 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
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.Threading;
using System.Threading.Tasks;
namespace OnlineStore
{
public class HIKCamera
{
Thread camerathread;
Asa.HIK.IPCamera camera;
public string Name = "";
public event EventHandler<Bitmap> camera_event;
public void LoadCameraConfig()
{
string path = @".\StoreConfig\Camera.json";
if (!File.Exists(path))
{
LogUtil.error(Name + "找不到监控相机配置文件" + path);
return;
}
camera = new Asa.HIK.IPCamera(path);
bool rtn = camera.Load();
if (!rtn)
{
LogUtil.error(Name + "加载监控相机配置文件失败");
return;
}
camerathread = new Thread(new ThreadStart(startCamera));
camerathread.Start();
GC.KeepAlive(camerathread);
}
void startCamera()
{
if (!camera.IsOpen[0])
{
var rtn = camera.Open(0);
if (!rtn)
LogUtil.error(Name + "打开监控相机失败");
return;
}
while (camera.IsOpen[0])
{
var rtn = camera.GrabOne(0, out Bitmap bmp);
if (rtn)
{
camera_event?.Invoke(this, bmp);
}
Thread.Sleep(1000 / 7);
}
camera.Close(0);
camera.Dispose();
}
void CameraGrabOne(string filename)
{
try
{
LogUtil.info(Name + "库位文件名:" + filename);
//return;
if (!camera.IsOpen[0])
{
camera.Open(0);
LogUtil.error(Name + "打开监控相机失败2");
}
var rtn = camera.GrabOne(0, out Bitmap bmp);
if (rtn)
{
if (File.Exists(filename))
File.Delete(filename);
bmp.Save(filename, ImageFormat.Jpeg);
bmp.Dispose();
}
else
{
rtn = camera.GrabOne(0, out bmp);
if (rtn)
{
if (File.Exists(filename))
File.Delete(filename);
bmp.Save(filename, ImageFormat.Jpeg);
bmp.Dispose();
}
}
}
catch (Exception e)
{
LogUtil.error(Name + e.ToString());
}
}
string GetFixtureStateFilename(InOutStoreParam inOutParam,StoreMoveType storeMoveType, FixtureState fixtureState)
{
string dir = $"\\image\\Fixture\\{storeMoveType}\\{inOutParam.PositionNum}\\";
Directory.CreateDirectory(dir);
string filename = $"{inOutParam.WareNumber}@@{fixtureState}.jpg";
foreach (var x in Path.GetInvalidFileNameChars())
{
filename = filename.Replace(x.ToString(), "");
}
return dir + filename;
}
enum FixtureState
{
In,
Up,
Down,
Out
}
}
}