WPF_Date_From.xaml.cs
20.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static SmartScan.FrmRetrospect;
using Image = System.Windows.Controls.Image;
namespace SmartScan.SetControl.WPF
{
/// <summary>
/// WPF_Date_From.xaml 的交互逻辑
/// </summary>
public partial class WPF_Date_From : System.Windows.Controls.UserControl
{
private List<HistoryPath> fileFull = new List<HistoryPath>();
private System.Windows.Forms.PictureBox pictureBox;
private System.Windows.Forms.PictureBox pictureBox1;
Dictionary<string, string> printdic = new Dictionary<string, string>();
// 定义事件
public event EventHandler MinimizeClicked;
public event EventHandler MaximizeClicked;
public event EventHandler CloseClicked;
public event EventHandler startservice;
public event EventHandler<Dictionary<string, string>> PrintRequested;
private class HistoryPath
{
public string cameraPath = "";
public string labelPath = "";
public string codePath = "";
public int Count
{
get
{
int n = 0;
if (cameraPath != "") n++;
if (labelPath != "") n++;
if (codePath != "") n++;
return n;
}
}
}
public WPF_Date_From()
{
InitializeComponent();
// 创建并配置 PictureBox
pictureBox = new System.Windows.Forms.PictureBox();
pictureBox.Dock = System.Windows.Forms.DockStyle.Fill;
pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; // 或 StretchImage、CenterImage 等
pictureBox.BackColor = System.Drawing.Color.Black;
// 将 PictureBox 托管到 WindowsFormsHost 中
imageViewer.Child = pictureBox;
pictureBox1 = new System.Windows.Forms.PictureBox();
pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; // 或 StretchImage、CenterImage 等
pictureBox1.BackColor = System.Drawing.Color.Black;
// 将 PictureBox 托管到 WindowsFormsHost 中
imageViewer1.Child = pictureBox1;
this.Loaded += (s, e) => {
if (logos is Image img)
{
// 将WinForms资源转换为WPF可用的BitmapSource
System.Drawing.Bitmap winFormsBitmap = global::SmartScan.Properties.Resources.App;
if (winFormsBitmap != null)
{
var bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
winFormsBitmap.GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
img.Source = bitmapSource;
}
}
};
}
private void Button_Click(object sender, RoutedEventArgs e)
{
List<string> directory = new List<string>();
string[] dirs = System.IO.Directory.GetDirectories(FilePath.RETROSPECT_DIR);
var isChecked = startdate.Tag;
var isChecked2 = enddate.Tag;
DateTime? selectedDate = startdate.SelectedDate;
DateTime? selectedDate2 = enddate.SelectedDate;
DateTime date = new DateTime();
DateTime date2 = new DateTime();
if (selectedDate.HasValue)
{
date = selectedDate.Value;
// 使用date...
}
if (selectedDate2.HasValue)
{
date2 = selectedDate2.Value;
// 使用date...
}
for (int i = 0; i < dirs.Length; i++)
{
System.IO.DirectoryInfo info = new DirectoryInfo(dirs[i]);
DateTime dt = DateTime.ParseExact(info.Name, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
if (isChecked.ToString() == "True")
{
if (dt < date)
continue;
}
if (isChecked2.ToString() == "True")
{
if (dt > date2)
continue;
}
directory.Add(dirs[i]);
}
fileFull = new List<HistoryPath>();
for (int i = 0; i < directory.Count; i++)
{
string[] files = System.IO.Directory.GetFiles(directory[i]);
fileFull.AddRange(CheckFiles(files));
}
int index = 0;
string text = TxtCode.Text;
if (text.Length > 0)
{
while (index < fileFull.Count)
{
string allText = System.IO.File.ReadAllText(fileFull[index].codePath);
if (allText.IndexOf(text) == -1) //任意位置出现,相同用=
fileFull.RemoveAt(index);
else
index++;
}
}
string[] items = new string[fileFull.Count];
for (int i = 0; i < fileFull.Count; i++)
{
string name = System.IO.Path.GetFileNameWithoutExtension(fileFull[i].codePath);
string[] arr = name.Split('_');
System.IO.DirectoryInfo info = new DirectoryInfo(fileFull[i].codePath);
items[i] = info.Parent.Name + " " + arr[0];
}
data3.ItemsSource = null;
// 2. 设置ListView的视图模式(可选)
// 4. 将数组项目添加到ListView
data3.ItemsSource = items;
// 如果有项,选中第一项
if (items.Length > 0)
{
data3.SelectedIndex = 0;
}
int fileFulls = fileFull.Count;
FrmRetrospect_faceLabel1.Text = string.Format(LanguageWwitchover.Dialog("information"), fileFull.Count);
}
private List<HistoryPath> CheckFiles(string[] files)
{
string[] affix = new string[3] { "_camera", "_label", "_code" };
List<HistoryPath> result = new List<HistoryPath>();
for (int i = 0; i < files.Length; i++)
{
if (files[i] == "") continue;
HistoryPath temp = new HistoryPath();
string name = System.IO.Path.GetFileNameWithoutExtension(files[i]);
string[] arr = name.Split('_');
string prefix = arr[0];
int index = Array.FindIndex(files, match => prefix + affix[0] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.cameraPath = files[index];
files[index] = "";
}
index = Array.FindIndex(files, match => prefix + affix[1] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.labelPath = files[index];
files[index] = "";
}
index = Array.FindIndex(files, match => prefix + affix[2] == System.IO.Path.GetFileNameWithoutExtension(match));
if (index > -1)
{
temp.codePath = files[index];
files[index] = "";
}
if (temp.Count == affix.Length)
result.Add(temp);
}
return result;
}
private void data3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 获取当前选中的项
var selectedItem = data3.SelectedItem;
if (selectedItem != null)
{
HistoryPath path = fileFull[data3.SelectedIndex];
if (File.Exists(path.cameraPath))
{
// 使用 pictureBox 而不是直接使用 imageViewer
pictureBox.Image = new System.Drawing.Bitmap(path.cameraPath);
pictureBox1.Image = new System.Drawing.Bitmap(path.labelPath);
data1.ItemsSource = null;
data1.Items.Clear();
data2.Items.Clear();
data2.ItemsSource = null;
string json = System.IO.File.ReadAllText(path.codePath);
JavaScriptSerializer serializer = new JavaScriptSerializer();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
// 处理条码数据
if (dic.ContainsKey("Code") && dic["Code"] != null)
{
object[] obj = (object[])dic["Code"];
List<PointF> centers = new List<PointF>();
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
string text = dicCode["Text"].ToString().Replace("\r", "").Replace("\n", "");
text = string.Format("({0}) {1}", i + 1, text);
data1.Items.Add(text);
float x = Convert.ToSingle(dicCode["X"]);
float y = Convert.ToSingle(dicCode["Y"]);
centers.Add(new PointF(x, y));
}
// 在实际应用中,这里可以标出图片上的条码位置
}
// 处理标签数据
if (dic.ContainsKey("Label"))
{
object[] obj = (object[])dic["Label"];
for (int i = 0; i < obj.Length; i++)
{
data2.Items.Add(obj[i].ToString());
}
} // 处理内容数据,用于打印
//添加关键字键值对,用于打印
printdic.Clear();
if (dic.ContainsKey("Content"))
{
var content = (object[])dic["Content"];
var dictionary = (Dictionary<string, object>)content[0];
foreach (var item in dictionary)
{
if (!string.IsNullOrWhiteSpace((string)item.Value))
{
printdic.Add(item.Key, (string)item.Value);
}
}
}
}
else
{
// 文件不存在时清空图片或显示默认图片
pictureBox.Image = null;
pictureBox1.Image = null;
// 可选:显示错误信息
// MessageBox.Show($"图片文件不存在: {path.cameraPath}");
}
}
}
// 修改按钮事件处理程序
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// 最小化按钮
MinimizeClicked?.Invoke(this, EventArgs.Empty);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
// 最大化按钮
MaximizeClicked?.Invoke(this, EventArgs.Empty);
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
// 关闭按钮
CloseClicked?.Invoke(this, EventArgs.Empty);
}
private void FrmRetrospect_faceBut_prin_Click(object sender, RoutedEventArgs e)
{
if (printdic.Count == 0)
{
LogNet.log.Info("打印参数为0");
return;
}
// 触发打印事件,传递打印字典
PrintRequested?.Invoke(this, new Dictionary<string, string>(printdic));
}
int exportFileFormat = ConfigHelper.Config.Get("ExportFileFormat", 1);
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmRetrospect_BtnExport_Click(object sender, RoutedEventArgs e)
{
if (data3.SelectedIndex == -1) return;
switch (exportFileFormat)
{
case 1:
exportByMacroKey(data3.SelectedIndex);
break;
default:
SaveFileDialog dlg = new() { Filter = "TXT文件|*.txt" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
string content = ExportIndex(data3.SelectedIndex);
System.IO.File.WriteAllText(dlg.FileName, content, Encoding.UTF8);
break;
}
}
private string ExportIndex(int index)
{
return exportOriginalFormat(index);
}
string exportOriginalFormat(int index)
{
HistoryPath path = fileFull[index];
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
string content = "";
if (dic.ContainsKey("Code"))
{
object[] obj = (object[])dic["Code"];
string[] items = new string[obj.Length];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
items[i] = dicCode["Text"].ToString().Replace("\r", "");
items[i] = items[i].Replace("\n", "");
}
content += "Code," + string.Join(",", items) + "\r\n";
}
if (dic.ContainsKey("Label"))
{
object[] obj = (object[])dic["Label"];
string[] items = new string[obj.Length];
for (int i = 0; i < items.Length; i++)
items[i] = obj[i].ToString();
content += "Label," + string.Join(",", items);
}
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
string[] items = new string[obj.Length];
for (int i = 0; i < items.Length; i++)
items[i] = obj[i].ToString();
content += "Content," + string.Join(",", items);
}
return content;
}
void exportByMacroKey(int index)
{
SaveFileDialog dlg = new() { Filter = "CSV文件|*.csv" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
StringBuilder content = new StringBuilder();
content.AppendLine(string.Join(",", BLLCommon.macroKey));
var path = fileFull[index];
{
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
if (checkHasMacroKey(dicCode))
{
content.AppendLine(genLine(dicCode));
break;
}
}
}
}
System.IO.File.WriteAllText(dlg.FileName, content.ToString(), Encoding.UTF8);
}
bool checkHasMacroKey(Dictionary<string, object> content)
{
for (int i = 0; i < BLLCommon.macroKey.Count; i++)
{
if (i > 0)
return true;
else if (!content.ContainsKey(BLLCommon.macroKey[i])) { return false; }
}
return true;
}
string genLine(Dictionary<string, object> pairs)
{
List<string> lines = new List<string>();
foreach (var item in BLLCommon.macroKey)
{
string value = "";
if (pairs.ContainsKey(item))
{
value = pairs[item].ToString();
}
if (value.StartsWith("<OCR>"))
{
value = value.Replace("<OCR>", "");
}
value = value.Replace("\"\"", "\"\"\"\"");
lines.Add("\"" + value + "\"");
}
return $"{string.Join(",", lines)}";
}
void exportAllByMacroKey()
{
SaveFileDialog dlg = new() { Filter = "CSV文件|*.csv" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
StringBuilder content = new StringBuilder();
content.AppendLine(string.Join(",", BLLCommon.macroKey));
foreach (var path in fileFull)
{
string json = System.IO.File.ReadAllText(path.codePath);
System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
Dictionary<string, object> dic = (Dictionary<string, object>)serializer.DeserializeObject(json);
if (dic.ContainsKey("Content"))
{
object[] obj = (object[])dic["Content"];
for (int i = 0; i < obj.Length; i++)
{
Dictionary<string, object> dicCode = (Dictionary<string, object>)obj[i];
if (checkHasMacroKey(dicCode))
{
content.AppendLine(genLine(dicCode));
break;
}
}
}
}
System.IO.File.WriteAllText(dlg.FileName, content.ToString(), Encoding.UTF8);
}
private void FrmRetrospect_BtnExportAll_Click(object sender, RoutedEventArgs e)
{
switch (exportFileFormat)
{
case 1:
exportAllByMacroKey();
break;
default:
SaveFileDialog dlg = new() { Filter = "TXT文件|*.txt" };
if (dlg.ShowDialog() == DialogResult.Cancel) return;
string[] content = new string[fileFull.Count];
for (int i = 0; i < fileFull.Count; i++)
content[i] = ExportIndex(i);
System.IO.File.WriteAllLines(dlg.FileName, content, Encoding.UTF8);
break;
}
}
private void FrmRetrospect_butt_startserver_Click(object sender, RoutedEventArgs e)
{
startservice?.Invoke(this, EventArgs.Empty);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
string YU = BLLCommon.config.Language;
LanguageWwitchover.LoadPath(FilePath.LANGUAGE_DIR);
LanguageWwitchover.LoadLanguage(YU);
LanguageWwitchover.SetLanguage(this);
}
}
}