HDevelopExport.cs
13.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
// File generated by HDevelop for HALCON/DOTNET (C#) Version 12.0
//
// This file is intended to be used with the HDevelopTemplate or
// HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using HalconDotNet;
using OnlineStore.Common;
public partial class HDevelopExport
{
public HTuple hv_ExpDefaultWinHandle;
// Procedures
// Chapter: Graphics / Text
// Short Description: This procedure writes a text message.
public void disp_message(HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
HTuple hv_Red = null, hv_Green = null, hv_Blue = null;
HTuple hv_Row1Part = null, hv_Column1Part = null, hv_Row2Part = null;
HTuple hv_Column2Part = null, hv_RowWin = null, hv_ColumnWin = null;
HTuple hv_WidthWin = new HTuple(), hv_HeightWin = null;
HTuple hv_MaxAscent = null, hv_MaxDescent = null, hv_MaxWidth = null;
HTuple hv_MaxHeight = null, hv_R1 = new HTuple(), hv_C1 = new HTuple();
HTuple hv_FactorRow = new HTuple(), hv_FactorColumn = new HTuple();
HTuple hv_UseShadow = null, hv_ShadowColor = null, hv_Exception = new HTuple();
HTuple hv_Width = new HTuple(), hv_Index = new HTuple();
HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
HTuple hv_W = new HTuple(), hv_H = new HTuple(), hv_FrameHeight = new HTuple();
HTuple hv_FrameWidth = new HTuple(), hv_R2 = new HTuple();
HTuple hv_C2 = new HTuple(), hv_DrawMode = new HTuple();
HTuple hv_CurrentColor = new HTuple();
HTuple hv_Box_COPY_INP_TMP = hv_Box.Clone();
HTuple hv_Color_COPY_INP_TMP = hv_Color.Clone();
HTuple hv_Column_COPY_INP_TMP = hv_Column.Clone();
HTuple hv_Row_COPY_INP_TMP = hv_Row.Clone();
HTuple hv_String_COPY_INP_TMP = hv_String.Clone();
//Prepare window
HOperatorSet.GetRgb(hv_ExpDefaultWinHandle, out hv_Red, out hv_Green, out hv_Blue);
HOperatorSet.GetPart(hv_ExpDefaultWinHandle, out hv_Row1Part, out hv_Column1Part,
out hv_Row2Part, out hv_Column2Part);
HOperatorSet.GetWindowExtents(hv_ExpDefaultWinHandle, out hv_RowWin, out hv_ColumnWin,
out hv_WidthWin, out hv_HeightWin);
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, 0, 0, hv_HeightWin - 1, hv_WidthWin - 1);
//
//default settings
if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Row_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
{
hv_Column_COPY_INP_TMP = 12;
}
if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(new HTuple()))) != 0)
{
hv_Color_COPY_INP_TMP = "";
}
//
hv_String_COPY_INP_TMP = ((("" + hv_String_COPY_INP_TMP) + "")).TupleSplit("\n");
//
//Estimate extentions of text depending on font size.
HOperatorSet.GetFontExtents(hv_ExpDefaultWinHandle, out hv_MaxAscent, out hv_MaxDescent,
out hv_MaxWidth, out hv_MaxHeight);
if ((int)(new HTuple(hv_CoordSystem.TupleEqual("window"))) != 0)
{
hv_R1 = hv_Row_COPY_INP_TMP.Clone();
hv_C1 = hv_Column_COPY_INP_TMP.Clone();
}
else
{
//Transform image to window coordinates
hv_FactorRow = (1.0 * hv_HeightWin) / ((hv_Row2Part - hv_Row1Part) + 1);
hv_FactorColumn = (1.0 * hv_WidthWin) / ((hv_Column2Part - hv_Column1Part) + 1);
hv_R1 = ((hv_Row_COPY_INP_TMP - hv_Row1Part) + 0.5) * hv_FactorRow;
hv_C1 = ((hv_Column_COPY_INP_TMP - hv_Column1Part) + 0.5) * hv_FactorColumn;
}
//
//Display text box depending on text size
hv_UseShadow = 1;
hv_ShadowColor = "gray";
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleEqual("true"))) != 0)
{
if (hv_Box_COPY_INP_TMP == null)
hv_Box_COPY_INP_TMP = new HTuple();
hv_Box_COPY_INP_TMP[0] = "#fce9d4";
hv_ShadowColor = "#f28d26";
}
if ((int)(new HTuple((new HTuple(hv_Box_COPY_INP_TMP.TupleLength())).TupleGreater(
1))) != 0)
{
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual("true"))) != 0)
{
//Use default ShadowColor set above
}
else if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(1))).TupleEqual(
"false"))) != 0)
{
hv_UseShadow = 0;
}
else
{
hv_ShadowColor = hv_Box_COPY_INP_TMP[1];
//Valid color?
try
{
HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
1));
}
// catch (Exception)
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[1] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
}
}
if ((int)(new HTuple(((hv_Box_COPY_INP_TMP.TupleSelect(0))).TupleNotEqual("false"))) != 0)
{
//Valid color?
try
{
HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
0));
}
// catch (Exception)
catch (HalconException HDevExpDefaultException1)
{
HDevExpDefaultException1.ToHTuple(out hv_Exception);
hv_Exception = "Wrong value of control parameter Box[0] (must be a 'true', 'false', or a valid color string)";
throw new HalconException(hv_Exception);
}
//Calculate box extents
hv_String_COPY_INP_TMP = (" " + hv_String_COPY_INP_TMP) + " ";
hv_Width = new HTuple();
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index), out hv_Ascent, out hv_Descent, out hv_W, out hv_H);
hv_Width = hv_Width.TupleConcat(hv_W);
}
hv_FrameHeight = hv_MaxHeight * (new HTuple(hv_String_COPY_INP_TMP.TupleLength()
));
hv_FrameWidth = (((new HTuple(0)).TupleConcat(hv_Width))).TupleMax();
hv_R2 = hv_R1 + hv_FrameHeight;
hv_C2 = hv_C1 + hv_FrameWidth;
//Display rectangles
HOperatorSet.GetDraw(hv_ExpDefaultWinHandle, out hv_DrawMode);
HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, "fill");
//Set shadow color
HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_ShadowColor);
if ((int)(hv_UseShadow) != 0)
{
HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1 + 1, hv_C1 + 1, hv_R2 + 1,
hv_C2 + 1);
}
//Set box color
HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_Box_COPY_INP_TMP.TupleSelect(
0));
HOperatorSet.DispRectangle1(hv_ExpDefaultWinHandle, hv_R1, hv_C1, hv_R2, hv_C2);
HOperatorSet.SetDraw(hv_ExpDefaultWinHandle, hv_DrawMode);
}
//Write text.
for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_String_COPY_INP_TMP.TupleLength()
)) - 1); hv_Index = (int)hv_Index + 1)
{
hv_CurrentColor = hv_Color_COPY_INP_TMP.TupleSelect(hv_Index % (new HTuple(hv_Color_COPY_INP_TMP.TupleLength()
)));
if ((int)((new HTuple(hv_CurrentColor.TupleNotEqual(""))).TupleAnd(new HTuple(hv_CurrentColor.TupleNotEqual(
"auto")))) != 0)
{
HOperatorSet.SetColor(hv_ExpDefaultWinHandle, hv_CurrentColor);
}
else
{
HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
}
hv_Row_COPY_INP_TMP = hv_R1 + (hv_MaxHeight * hv_Index);
HOperatorSet.SetTposition(hv_ExpDefaultWinHandle, hv_Row_COPY_INP_TMP, hv_C1);
HOperatorSet.WriteString(hv_ExpDefaultWinHandle, hv_String_COPY_INP_TMP.TupleSelect(
hv_Index));
}
//Reset changed window settings
HOperatorSet.SetRgb(hv_ExpDefaultWinHandle, hv_Red, hv_Green, hv_Blue);
HOperatorSet.SetPart(hv_ExpDefaultWinHandle, hv_Row1Part, hv_Column1Part, hv_Row2Part,
hv_Column2Part);
return;
}
// Main procedure
private void action(HObject ho_Image)
{
HObject ho_SymbolXLDs, ho_SymbolXLD = null;
HTuple hv_WindowHandle = new HTuple(), hv_DataCodeHandle = null;
HTuple hv_ResultHandles = null, hv_DecodedDataStrings = null;
HTuple hv_i = null, hv_Row = new HTuple(), hv_Col = new HTuple();
HTuple hv_Ascent = new HTuple(), hv_Descent = new HTuple();
HTuple hv_TxtWidth = new HTuple(), hv_TxtHeight = new HTuple();
HOperatorSet.GenEmptyObj(out ho_SymbolXLDs);
HOperatorSet.GenEmptyObj(out ho_SymbolXLD);
try
{
HOperatorSet.SetSystem("filename_encoding", "utf8");
HOperatorSet.CreateDataCode2dModel("Data Matrix ECC 200", "default_parameters", "enhanced_recognition", out hv_DataCodeHandle);
HOperatorSet.SetDataCode2dParam(hv_DataCodeHandle, "polarity", "dark_on_light");
ho_SymbolXLDs.Dispose();
HOperatorSet.FindDataCode2d(ho_Image, out ho_SymbolXLDs, hv_DataCodeHandle,
new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
//检测和读取二维代码符号,也支持读取二维数据模型的序列,参数详见这个算子
for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_ResultHandles.TupleLength())) - 1); hv_i = (int)hv_i + 1)
{
ho_SymbolXLD.Dispose();
HOperatorSet.SelectObj(ho_SymbolXLDs, out ho_SymbolXLD, hv_i + 1);
HOperatorSet.GetContourXld(ho_SymbolXLD, out hv_Row, out hv_Col);
HOperatorSet.GetStringExtents(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect(
hv_i), out hv_Ascent, out hv_Descent, out hv_TxtWidth, out hv_TxtHeight);
disp_message(hv_ExpDefaultWinHandle, hv_DecodedDataStrings.TupleSelect(hv_i),
"image", ((hv_Row - 50)).TupleMax(), (((((((hv_Col + 30)).TupleMin()) - (hv_TxtWidth / 2))).TupleConcat(
1))).TupleMax(), "black", "true");
}
//Clear the 2d data code model
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
string[] resultList = hv_DecodedDataStrings.SArr;
result = new List<string>(resultList);
foreach (string res in resultList)
{
result.Add(res);
}
}
catch (HalconException HDevExpDefaultException)
{
ho_Image.Dispose();
ho_SymbolXLDs.Dispose();
ho_SymbolXLD.Dispose();
}
ho_Image.Dispose();
ho_SymbolXLDs.Dispose();
ho_SymbolXLD.Dispose();
}
public List<string> result = new List<string>( );
public void RunHalcon(HTuple Window, HObject ho_Image,string codeType, int count)
{
hv_ExpDefaultWinHandle = Window;
string[] array = GetQrCode(ho_Image,codeType,count);
result = new List<string>(array);
}
public void RunHalcon(HTuple Window, string filePath)
{
hv_ExpDefaultWinHandle = Window;
HObject ho_Image;
HOperatorSet.GenEmptyObj(out ho_Image);
ho_Image.Dispose();
HOperatorSet.ReadImage(out ho_Image, filePath);
HOperatorSet.DispObj(ho_Image, Window);
action(ho_Image);
}
// Procedures
//private static HTuple hv_DataCodeHandle = null;
private static string[] GetQrCode(HObject ho_Image, string symbolType,int count)
{
try
{
DateTime date = DateTime.Now;
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 = CodeTest.HDCodeHelper.GetCodeParamFilePath(symbolType);
if (!hv_model_path.Equals(""))
{
HOperatorSet.ReadDataCode2dModel(hv_model_path, out hv_DataCodeHandle);
}
ho_SymbolXLDs.Dispose();
if (count <= 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", count, out hv_ResultHandles, out hv_DecodedDataStrings);
}
HOperatorSet.ClearDataCode2dModel(hv_DataCodeHandle);
string[] resultList = hv_DecodedDataStrings.SArr;
return resultList;
}
catch (Exception ex)
{
return new string[] { };
}
}
}