ScanWork.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using Asa.FaceControl;
using BLL;
using Model;
using OnlineStore.Common.util;
using SmartScan.Form;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SmartScan
{
public class ScanWork
{
public bool isRun = false;
private bool isTouch = false;
private readonly FacePictureBox picShow;
private readonly FaceButton btnMatchedName;
private List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo;
public Dictionary<string, string> workCodeKeyword;
private string[] originalCodeText;
private bool[] originalCodeIsUsed;
public ScanWork()
{
picShow = (FacePictureBox)Common.frmMain.Controls["PicShow"];
btnMatchedName = (FaceButton)Common.frmMain.Controls["BtnMatchedName"];
}
public void Open()
{
isRun = true;
isTouch = false;
Common.frmMain.Controls["BtnSet"].Visible = false;
Common.frmMain.Controls["BtnRetrospect"].Visible = false;
Common.frmMain.Controls["BtnAbout"].Visible = false;
Common.frmMain.Controls["BtnTriggerIO"].Visible = true;
LogNet.log.Info("Work Start");
if (BLLCommon.config.EnabledCamera)
Camera.Open();
if (BLLCommon.config.TriggerOpenLight) //触发时才打开光源
BLLCommon.lightSource?.TurnOff();
else
BLLCommon.lightSource?.TurnOn();
}
public void Close()
{
isRun = false;
Common.frmMain.Controls["BtnSet"].Visible = true;
Common.frmMain.Controls["BtnRetrospect"].Visible = true;
Common.frmMain.Controls["BtnAbout"].Visible = true;
Common.frmMain.Controls["BtnTriggerIO"].Visible = false;
LogNet.log.Info("Work Stop");
if (BLLCommon.config.EnabledCamera)
Camera.Close();
BLLCommon.lightSource?.TurnOff();
}
public void TouchOff()
{
isTouch = false;
}
private readonly object lockObject = new object();
static System.Diagnostics.Stopwatch reckontime = new System.Diagnostics.Stopwatch();
public void Scan()
{
//此段代码加锁是因为,客户在连续点击识别设备按钮时,线程会同时生成rid=123并且rid赋值是重复的123123
//在测试时也遇到这个情况,但只成功复现了一次;故加锁。
lock (lockObject)
{
if (!isRun) return;
if (isTouch) return;
isTouch = true;
btnMatchedName.Invoke(delegate ()
{
if (!BLL.Config.Backgrounder)
btnMatchedName.Visible = false;
BLLCommon.extension.Clear();
});
Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialScanning"));//拍照识别...
var t = Task.Run(() =>
{
reckontime.Restart();
try
{
Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialScanning"));//拍照识别...
workCodeInfo = new();
workCodeKeyword = new(StringComparer.OrdinalIgnoreCase);
originalCodeText = null;
originalCodeIsUsed = null;
if (!GetCodeInfo())
{
isTouch = false;
Common.frmMain.CloseWaittingDialog();
return;
}
Common.frmMain.Invoke(delegate ()
{
AddCodeCenter();
});
LogNet.log.Info($"获取图片耗时{reckontime.ElapsedMilliseconds}ms");
Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialTemplateMatching"),5);//模版匹配...
reckontime.Restart();
bool hasMatch = MatchingTemplate();
LogNet.log.Info($"模板匹配耗时{reckontime.ElapsedMilliseconds}ms");
reckontime.Restart();
Common.frmMain.SetWaittingMsg(Language.Dialog("MaterialProcessing"),5);//计算结果...
Common.frmMain.Invoke(delegate ()
{
SetKey(hasMatch);
});
isTouch = false;
LogNet.log.Info("Work scan is done");
LogNet.log.Info($"渲染控件耗时{reckontime.ElapsedMilliseconds}ms");
}
catch (Exception ex)
{
LogNet.log.Error("Scan", ex);
}
finally
{
isTouch = false;
Common.frmMain.CloseWaittingDialog();
}
});
Common.frmMain.Invoke(delegate ()
{
Common.frmMain.ShowWaittingDialog();
});
}
}
public void Scan(string[] code)
{
try
{
if (!isRun) return;
if (isTouch) return;
if (!BLL.Config.Backgrounder)
btnMatchedName.Visible = false;
BLLCommon.extension.Clear();
workCodeInfo = new();
workCodeKeyword = new(StringComparer.OrdinalIgnoreCase);
originalCodeText = code;
originalCodeIsUsed = null;
var hasMatch = MatchingTemplate();
SetKey(hasMatch);
isTouch = true;
LogNet.log.Info("Work scan code is done");
}
catch (Exception ex)
{
LogNet.log.Error("Scan", ex);
}
}
public object[] SaveCodeInfo()
{
object[] obj = null;
if (workCodeInfo == null)
return obj;
obj = new object[workCodeInfo.Count];
for (int i = 0; i < workCodeInfo.Count; i++)
{
Dictionary<string, string> dicCode = new()
{
{ "X", workCodeInfo[i].Center.X.ToString() },
{ "Y", workCodeInfo[i].Center.Y.ToString() },
{ "Text", workCodeInfo[i].Text }
};
obj[i] = dicCode;
}
return obj;
}
public WebCodeAll[] GetWebCodeAll()
{
WebCodeAll[] str = new WebCodeAll[workCodeInfo.Count];
for (int i = 0; i < str.Length; i++)
{
str[i] = new()
{
Text = workCodeInfo[i].Text,
CodeType = workCodeInfo[i].CodeType,
CenterX = workCodeInfo[i].Center.X,
CenterY = workCodeInfo[i].Center.Y,
Angle = workCodeInfo[i].Angle,
Width = workCodeInfo[i].Size.Width,
Height = workCodeInfo[i].Size.Height,
IsUsed = originalCodeIsUsed[i]
};
}
return str;
}
public WebCodeText[] GetWebCodeText()
{
WebCodeText[] str = new WebCodeText[originalCodeText.Length];
for (int i = 0; i < str.Length; i++)
{
str[i] = new()
{
Text = originalCodeText[i],
IsUsed = originalCodeIsUsed[i]
};
}
return str;
}
private bool GetCodeInfo()
{
LogNet.log.Info("Work GetCodeInfo");
if (BLLCommon.config.EnabledCamera)
{
if (BLLCommon.config.TriggerOpenLight)
{
BLLCommon.lightSource.TurnOn();
System.Threading.Thread.Sleep(100); //光源打开有一个由暗变亮的过程
}
List<Bitmap> image = new List<Bitmap>(Camera.CaptureAndGetCode(out workCodeInfo));
if (image.Count > 0)
{
BLLCommon.mateEdit.CurrntBitmap = WebCallWork.DeepClone(image[0]);
if (!BLL.Config.Backgrounder)
picShow.Image = image[0];
if (BLLCommon.mateEdit.CurrntBitmap!=null)
_ = UnifiedDataHandler.PostSmfImageAsync(BLLCommon.mateEdit.CurrntBitmap, new Dictionary<string, string> { { "cid", BLLCommon.config.CID + "_1" } }, BLLCommon.mateEdit.CurrntBitmap.Width, BLLCommon.mateEdit.CurrntBitmap.Height);
}
if (BLLCommon.config.TriggerOpenLight)
BLLCommon.lightSource.TurnOff();
return true;
}
else
{
Common.frmMain.Invoke(delegate ()
{
if (!BLL.Config.Backgrounder)
picShow.Image?.Dispose();
BLLCommon.mateEdit.CurrntBitmap?.Dispose();
});
string filename = "";
Common.frmMain.Invoke(delegate ()
{
OpenFileDialog dlg = new() { Filter = "图片文件|*.jpg;*.png;*.bmp;*.jpeg" };
DialogResult dr = dlg.ShowDialog();
filename = dlg.FileName;
});
if (string.IsNullOrEmpty(filename))
return false;
Bitmap bmp = null;
bmp = ObjConversion.ReadImageFile(filename);
workCodeInfo = Camera.GetBarCode(bmp);
Common.frmMain.Invoke(delegate ()
{
if (!BLL.Config.Backgrounder)
picShow.Image = bmp;
BLLCommon.mateEdit.CurrntBitmap = bmp;
});
return true;
}
if (workCodeInfo.Count == 0 && !BLL.Config.Backgrounder)
{
string text = Language.Dialog(LanguageDialogKey.CODE_COUNT);
new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog(Common.frmMain);
}
Application.DoEvents();
LogNet.log.Info("条码个数:" + workCodeInfo.Count);
}
private void AddCodeCenter()
{
LogNet.log.Info("Work AddCodeCenter");
if (!BLL.Config.Backgrounder)
picShow.CodeCenterClear();
if (workCodeInfo.Count == 0) return;
PointF[] center = new PointF[workCodeInfo.Count];
for (int i = 0; i < workCodeInfo.Count; i++)
center[i] = workCodeInfo[i].Center;
if (!BLL.Config.Backgrounder)
picShow.AddCodeCenter(center);
Application.DoEvents();
}
private bool MatchingTemplate()
{
LogNet.log.Info("Work MatchingTemplate");
//没有条码也继续进行模板匹配
//if (workCodeInfo.Count == 0) return false;
originalCodeText = Camera.GetBarCodeText(workCodeInfo);
bool rtn = BLLCommon.mateEdit.MatchingTemplate(workCodeInfo, BLLCommon.config.DefaultMaterialName, false, out string mateName, out workCodeKeyword, out AMatch aMatch);
BLL.MatchAnalysis.ShowResult();
Common.frmMain.Invoke(delegate ()
{
if (rtn)
{
LogNet.log.Info("模板匹配 " + mateName + ",关键字个数 " + workCodeKeyword.Count);
if (!BLL.Config.Backgrounder)
{
btnMatchedName.Visible = true;
btnMatchedName.Text = mateName;
}
}
else
{
LogNet.log.Info("没有匹配到模板");
if (!BLL.Config.Backgrounder)
{
string text = Language.Dialog("MaterialTemplateNoMatch");
var fm = new FaceMessageBox("", text, MessageBoxButtons.OK);
fm.TopMost = true;
fm.ShowDialog(Common.frmMain);
}
}
});
return rtn;
}
private void SetKey(bool hasMatch)
{
LogNet.log.Info("Work SetKey hasMatch:" + hasMatch);
//if (workCodeKeyword.Count == 0){ return;}
BLLCommon.extension.SetKey(originalCodeText, workCodeKeyword, hasMatch, out _);
if (workCodeKeyword.Any(a => a.Value.StartsWith("<OCR>")))
{
BLLCommon.extension.DrawTextBackground(workCodeKeyword);
FrmDrawText frmDraw = new FrmDrawText();
if (frmDraw.ShowDialog() == DialogResult.OK)
{
Dictionary<string, string> pairs = workCodeKeyword.ToDictionary(val => val.Key,
val => val.Value.StartsWith("<OCR>")?val.Value.Replace("<OCR>", "") : val.Value
);
//打印
BLLCommon.extension.Print(hasMatch, pairs);
}
}
else
{
BLLCommon.extension.Print(hasMatch, workCodeKeyword);
}
if (BLLCommon.config.PromptAfterPrinting && !BLL.Config.Backgrounder)
{
string text = Language.Dialog("SelectPrintContent");
new FaceMessageBox("", text, MessageBoxButtons.OK).ShowDialog();
}
}
}
}