CodeManager.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
using CodeLibrary;
using HalconDotNet;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class CodeManager
{
public static List<string> cameraNameList = new List<string>();
public static List<string> codeTypeList = new List<string>();
public static List<string> balserNameList = new List<string>();
public static List<string> hikNameList = new List<string>();
private static char spiltChar = '#';
/// <summary>
/// 初始化摄像机名称和二维码类型
/// </summary>
/// <param name="nameStr">摄像机名称,多个用#分割</param>
/// <param name="codeStr">二维码类型,多个用#分割</param>
public static void LoadConfig(string nameStr, string codeStr)
{
cameraNameList = new List<string>();
codeTypeList = new List<string>();
try
{
string[] nameArray = nameStr.Split(spiltChar);
foreach (string str in nameArray)
{
LogUtil.info("加载到配置摄像机名称:" + str.Trim());
cameraNameList.Add(str.Trim());
}
string[] codeArray = codeStr.Split(spiltChar);
foreach (string str in codeArray)
{
LogUtil.info("加载到配置二维码类型:" + str.Trim());
codeTypeList.Add(str.Trim());
}
string[] names = CodeLibrary.HIKCamera.Instance.CameraName;
hikNameList.AddRange(names);
names = CodeLibrary.BaslerCamera.Instance.CameraName;
balserNameList.AddRange(names);
foreach(string name in hikNameList)
{
LogUtil.info("加载到HIK相机:" + name);
}
foreach (string name in balserNameList)
{
LogUtil.info("加载到Balser相机:" + name);
}
CodeLibrary.HDCodeLearnHelper.LoadConfig(nameStr, codeStr);
}
catch (Exception ex)
{
LogUtil.error("解析摄像机配置出错:" + ex.ToString());
}
}
public static Bitmap GetCamerImage(string cameraName)
{
Bitmap bitm = null;
if (balserNameList.Contains(cameraName))
{
BaslerCamera.Instance.Open(cameraName);
BaslerCamera.Instance.GrabOne();
bitm = BaslerCamera.Instance.Image;
BaslerCamera.Instance.Close();
}
else if(hikNameList.Contains(cameraName))
{
HIKCamera.Instance.Open(cameraName);
HIKCamera.Instance.GrabOne();
bitm = HIKCamera.Instance.Image;
HIKCamera.Instance.Close();
}
else
{
LogUtil.info("未找到摄像机【"+cameraName+"】无法获取图片");
}
return bitm;
}
public static List<string> CameraScan( )
{
List<string> codeList = new List<string>();
List<CodeInfo> allCodeList = new List<CodeInfo>();
try
{
foreach (string cameraName in cameraNameList)
{
Bitmap bitmap = GetCamerImage(cameraName);
if (bitmap == null)
{
LogUtil.info(" 摄像机【" + cameraName + "】获取图片失败");
}
else
{
HObject ho_Image = HDCodeHelper.Bitmap2HObjectBpp24(bitmap);
List<CodeInfo> cc = new List<CodeInfo>();
foreach (string codeType in codeTypeList)
{
cc = HDCodeHelper.DecodeCode(ho_Image, 1, GetCodeParamFilePath(codeType), codeType);
}
allCodeList.AddRange(cc);
}
}
}
catch (Exception ex)
{
LogUtil.error("扫码出错:" + ex.ToString());
}
foreach(CodeInfo info in allCodeList)
{
codeList.Add(info.CodeStr);
}
return codeList;
}
public static string GetCodeParamFilePath(string codePath)
{
string appPath = Application.StartupPath;
string path = appPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
string filePath = path + codePath + ".dcm";
if (File.Exists(filePath))
{
return filePath;
}
else
{
return "";
}
}
}
}