HDCodeHelper.cs
6.3 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
using HalconDotNet;
using OnlineStore.Common;
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.Tasks;
using System.Windows.Forms;
namespace CodeTest
{
public class HDCodeHelper
{
/// <summary>
/// 显示的窗口
/// </summary>
public static HWindow HalconWindow = null;
/// <summary>
/// 扫码时是否使用参数文件
/// </summary>
public static bool IsUseParam = true;
public static List<string> DecodeCode(string filePath, int codeCount, params string[] paramType)
{
HObject ho_Image;
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, filePath);
if (HalconWindow != null)
{
HOperatorSet.DispObj(ho_Image, HalconWindow);
}
return DecodeCode(ho_Image, codeCount, paramType);
}
public static List<string> DecodeCode(Bitmap map, int codeCount, params string[] paramType)
{
HObject ho_image = Bitmap2HObjectBpp24(map);
return DecodeCode(ho_image, codeCount, paramType ) ;
}
public static List<string> DecodeCode(HObject ho_Image, int codeCount, params string[] paramType)
{
List<string> codeType = new List<string>(paramType.ToList());
if (codeType.Count<string>() <= 0)
{
codeType.Add("Data Matrix ECC 200");
}
List<string> codeList = new List<string>();
foreach (string t in codeType)
{
string[] array = GetCode(ho_Image, t, codeCount);
codeList.AddRange(array.ToArray());
}
return codeList;
}
private static string[] GetCode(HObject ho_Image, string symbolType,int codeCount)
{
try
{
HObject ho_SymbolXLDs;
HTuple hv_ResultHandles = null;
HTuple hv_DecodedDataStrings = null;
HTuple hv_DataCodeHandle = null;
HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
HOperatorSet.CreateDataCode2dModel(symbolType, "default_parameters", "maximum_recognition", out hv_DataCodeHandle);
string hv_model_path = GetCodeParamFilePath(symbolType);
if (!hv_model_path.Equals("")&&IsUseParam)
{
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
}
ho_SymbolXLDs.Dispose();
if (codeCount <= 0)
{
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
}
else
{
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
"stop_after_result_num", codeCount, out hv_ResultHandles, out hv_DecodedDataStrings);
}
if (HalconWindow != null)
{
ShowImage(HalconWindow, ho_Image, ho_SymbolXLDs);
}
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
string[] resultList = hv_DecodedDataStrings.SArr;
return resultList;
}
catch (Exception ex)
{
return new string[] { };
}
}
//private static int dWidth = 0;
//private static int dHeight = 0;
//public static void ClearShowInfo()
//{
// dWidth = 0;
// dHeight = 0;
//}
private static void ShowImage(HWindow window, HObject ho_Image, HObject ho_SymbolXLDs)
{
if (window == null || ho_Image == null)
{
return;
}
Task.Factory.StartNew(delegate ()
{
try
{
HTuple width, height;
//int dWidth = 0; int dHeight = 0;
HOperatorSet.GetImageSize(ho_Image, out width, out height);
int dWidth = (int)width.D;
int dHeight = (int)height.D;
HalconWindow.SetPart(0, 0, dHeight, dWidth);
HOperatorSet.SetColor(window, "red");
HOperatorSet.SetLineWidth(window, new HTuple(2));
HOperatorSet.DispObj(ho_Image, window);
HOperatorSet.DispObj(ho_SymbolXLDs, window);
}
catch (Exception ex)
{
}
});
}
public static string GetParamFilePathName(string codeType)
{
string appPath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
if (!Directory.Exists(appPath))
{
Directory.CreateDirectory(appPath);
}
string filePath = appPath + codeType + ".dcm";
return filePath;
}
public static string GetCodeParamFilePath(string codeType)
{
string filePath = GetParamFilePathName(codeType);
if (File.Exists(filePath))
{
return filePath;
}
else
{
return "";
}
}
public static HObject Bitmap2HObjectBpp24(Bitmap bmp)
{
HObject ho_Image = null;
try
{
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
HOperatorSet.GenImageInterleaved(out ho_Image, srcBmpData.Scan0, "bgrx", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
bmp.UnlockBits(srcBmpData);
}
catch (Exception ex)
{
ho_Image = null;
}
return ho_Image;
}
}
}