HistoryDataPage.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using CCDCount.DLL;
  2. using CCDCount.DLL.SqlDataClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using CCDCount.MODEL.ShuLiModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Markup;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace CCDCountWpf.WpfPage
  24. {
  25. /// <summary>
  26. /// HistoryDataPage.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class HistoryDataPage : Page
  29. {
  30. private static ActionMesSqliteDataClass actionMesSqliteDataClass = null;
  31. int DataStartLine = 0;
  32. private int PageHeight = 2000;
  33. public HistoryDataPage()
  34. {
  35. InitializeComponent();
  36. DataContext = ShowMessageBus.ShowBinding;
  37. InitBatchItems();
  38. //UpDateShowBinding();
  39. }
  40. private void PreviousBtn_Click(object sender, RoutedEventArgs e)
  41. {
  42. if (ShowMessageBus.ShowBinding.HistoryImageNum <= 1)
  43. {
  44. MessageBox.Show("已经是第一页");
  45. return;
  46. }
  47. ShowMessageBus.ShowBinding.HistoryImageNum -= 1;
  48. UpDateShowBinding();
  49. }
  50. private void NextBtn_Click(object sender, RoutedEventArgs e)
  51. {
  52. if (ShowMessageBus.ShowBinding.HistoryImageNum >= ShowMessageBus.ShowBinding.HistoryImageCount)
  53. {
  54. MessageBox.Show("已经是最后一页");
  55. return;
  56. }
  57. ShowMessageBus.ShowBinding.HistoryImageNum += 1;
  58. UpDateShowBinding();
  59. }
  60. private void UpDateShowBinding()
  61. {
  62. if (actionMesSqliteDataClass == null)
  63. {
  64. return;
  65. }
  66. Stopwatch stopwatch = Stopwatch.StartNew();
  67. BitmapImage ShowBitMap = null;
  68. Bitmap BitmapImage = null;
  69. List<ActiveObjectClass> ShowResult = actionMesSqliteDataClass.GetActiveObjectForPage((ShowMessageBus.ShowBinding.HistoryImageNum - 1) * PageHeight + DataStartLine, ShowMessageBus.ShowBinding.HistoryImageNum * PageHeight + DataStartLine);
  70. if (ShowResult.Count > 0)
  71. {
  72. int ThisImageStartLine = (int)ShowResult.Min(o => o.StartLine);
  73. int ImageHeight = (int)(ShowResult.Max(o => o.LastSeenLine) - ShowResult.Min(o => o.StartLine))+50;
  74. int ImageWidth = ShowResult.Max(o => o.ImageWidth);
  75. BitmapImage = new Bitmap(ImageWidth, ImageHeight <= PageHeight ? PageHeight : ImageHeight);
  76. using (Graphics g = Graphics.FromImage(BitmapImage))
  77. {
  78. g.Clear(System.Drawing.Color.White);
  79. var redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
  80. var bluePen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 146, 255), 1);
  81. var GreenPen = new System.Drawing.Pen(System.Drawing.Color.SeaGreen, 5);
  82. foreach (var item in ShowResult)
  83. {
  84. int roix = item.MinStartCol - 5;
  85. int roiy = (int)(item.StartLine - ThisImageStartLine) + 15;
  86. int roiheight = (int)(item.LastSeenLine - (long)item.StartLine) + 10;
  87. int roiwidth = item.MaxEndCol - item.MinStartCol + 10;
  88. g.DrawRectangle(GreenPen, new System.Drawing.Rectangle(roix, roiy, roiwidth, roiheight));
  89. Font font = new Font("Arial", 20);
  90. g.DrawString(item.Num.ToString(), font, System.Drawing.Brushes.Black, new System.Drawing.Point(roix - 20 * item.Num.ToString().Length, roiy - 20));
  91. foreach (var item1 in item.RowsData)
  92. {
  93. int yPos = (int)(item1.RowsCol - ThisImageStartLine + 20);
  94. g.DrawLine(item.StateCode == 0 ? bluePen : redPen, new System.Drawing.Point(item1.StartCol, yPos), new System.Drawing.Point(item1.EndCol, yPos));
  95. }
  96. }
  97. }
  98. }
  99. else
  100. {
  101. BitmapImage = new Bitmap(4096, PageHeight);
  102. using (Graphics g = Graphics.FromImage(BitmapImage))
  103. {
  104. g.Clear(System.Drawing.Color.White);
  105. }
  106. }
  107. Application.Current.Dispatcher.InvokeAsync(() =>
  108. {
  109. ShowBitMap = ConvertToBitmapImage(BitmapImage);
  110. ShowMessageBus.ShowBinding.HistoryImage = ShowBitMap;
  111. });
  112. stopwatch.Stop();
  113. Console.WriteLine($"{stopwatch.ElapsedMilliseconds}ms");
  114. }
  115. // Bitmap 转 BitmapImage 的辅助方法
  116. private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  117. {
  118. using (MemoryStream memory = new MemoryStream())
  119. {
  120. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  121. memory.Position = 0;
  122. var bitmapImage = new BitmapImage();
  123. bitmapImage.BeginInit();
  124. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  125. bitmapImage.StreamSource = memory;
  126. bitmapImage.EndInit();
  127. return bitmapImage;
  128. }
  129. }
  130. private void SkipBtn_Click(object sender, RoutedEventArgs e)
  131. {
  132. UpDateShowBinding();
  133. }
  134. /// <summary>
  135. /// 初始化批次列表
  136. /// </summary>
  137. private void InitBatchItems()
  138. {
  139. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}";
  140. if (!Directory.Exists(folderPath))
  141. {
  142. // 创建文件夹
  143. Directory.CreateDirectory(folderPath);
  144. }
  145. try
  146. {
  147. // 使用 DirectoryInfo 获取文件并按修改时间排序
  148. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  149. FileInfo[] files = dirInfo.GetFiles("*.db", SearchOption.AllDirectories);
  150. // 按照修改时间排序(最新的在前)
  151. var sortedFiles = files.OrderByDescending(f => f.LastWriteTime).ToArray();
  152. ShowMessageBus.ShowBinding.BatchItems.Clear();
  153. foreach (FileInfo file in sortedFiles)
  154. {
  155. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  156. }
  157. BatchNumComBox.SelectedIndex = 0;
  158. }
  159. catch
  160. { }
  161. }
  162. /// <summary>
  163. /// 初始化批号列表
  164. /// </summary>
  165. private void InitBatchItems(DateTime Mintime, DateTime MaxTime)
  166. {
  167. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  168. if (!Directory.Exists(folderPath))
  169. {
  170. // 创建文件夹
  171. Directory.CreateDirectory(folderPath);
  172. }
  173. try
  174. {
  175. // 使用 DirectoryInfo 获取文件并按修改时间排序
  176. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  177. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  178. // 按照修改时间排序(最新的在前)
  179. var sortedFiles = files.Where(f => f.CreationTime > Mintime && f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
  180. ShowMessageBus.ShowBinding.BatchItems.Clear();
  181. foreach (FileInfo file in sortedFiles)
  182. {
  183. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  184. }
  185. BatchNumComBox.SelectedIndex = 0;
  186. }
  187. catch
  188. { }
  189. }
  190. /// <summary>
  191. /// 初始化批号列表
  192. /// </summary>
  193. private void InitBatchItemsByMinTime(DateTime Mintime)
  194. {
  195. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  196. if (!Directory.Exists(folderPath))
  197. {
  198. // 创建文件夹
  199. Directory.CreateDirectory(folderPath);
  200. }
  201. try
  202. {
  203. // 使用 DirectoryInfo 获取文件并按修改时间排序
  204. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  205. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  206. // 按照修改时间排序(最新的在前)
  207. var sortedFiles = files.Where(f => f.CreationTime > Mintime).OrderByDescending(f => f.LastWriteTime).ToArray();
  208. ShowMessageBus.ShowBinding.BatchItems.Clear();
  209. foreach (FileInfo file in sortedFiles)
  210. {
  211. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  212. }
  213. BatchNumComBox.SelectedIndex = 0;
  214. }
  215. catch
  216. { }
  217. }
  218. /// <summary>
  219. /// 初始化批号列表
  220. /// </summary>
  221. private void InitBatchItemsByMaxTime(DateTime MaxTime)
  222. {
  223. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  224. if (!Directory.Exists(folderPath))
  225. {
  226. // 创建文件夹
  227. Directory.CreateDirectory(folderPath);
  228. }
  229. try
  230. {
  231. // 使用 DirectoryInfo 获取文件并按修改时间排序
  232. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  233. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  234. // 按照修改时间排序(最新的在前)
  235. var sortedFiles = files.Where(f => f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
  236. ShowMessageBus.ShowBinding.BatchItems.Clear();
  237. foreach (FileInfo file in sortedFiles)
  238. {
  239. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  240. }
  241. BatchNumComBox.SelectedIndex = 0;
  242. }
  243. catch
  244. { }
  245. }
  246. private void BatchNumRecordMinTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  247. {
  248. if (BatchNumRecordMinTime.SelectedDate == null)
  249. {
  250. return;
  251. }
  252. else
  253. {
  254. if (BatchNumRecordMaxTime.SelectedDate == null)
  255. {
  256. InitBatchItemsByMinTime((DateTime)BatchNumRecordMinTime.SelectedDate);
  257. }
  258. else
  259. {
  260. InitBatchItems((DateTime)BatchNumRecordMinTime.SelectedDate, (DateTime)BatchNumRecordMaxTime.SelectedDate);
  261. }
  262. }
  263. }
  264. private void BatchNumRecordMaxTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  265. {
  266. if (BatchNumRecordMaxTime.SelectedDate == null)
  267. {
  268. return;
  269. }
  270. else
  271. {
  272. if (BatchNumRecordMinTime.SelectedDate == null)
  273. {
  274. InitBatchItemsByMaxTime((DateTime)BatchNumRecordMaxTime.SelectedDate);
  275. }
  276. else
  277. {
  278. InitBatchItems((DateTime)BatchNumRecordMinTime.SelectedDate, (DateTime)BatchNumRecordMaxTime.SelectedDate);
  279. }
  280. }
  281. }
  282. private void BatchNumRecordComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  283. {
  284. if (BatchNumComBox.SelectedItem != null && BatchNumComBox.SelectedItem.ToString() != string.Empty)
  285. {
  286. string BatchNumber = BatchNumComBox.SelectedItem.ToString();
  287. actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}\\ActiveObjectData_{BatchNumber}.db");
  288. //actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}\\ActiveObjectData_20250908.db");
  289. actionMesSqliteDataClass.GetAllActionMinStartMaxEndLine(out int num, out int StartLine, out int EndLine);
  290. ShowMessageBus.ShowBinding.HistoryImageCount = (EndLine - StartLine) / PageHeight;
  291. DataStartLine = StartLine;
  292. ShowMessageBus.ShowBinding.HistoryImageNum = 1;
  293. UpDateShowBinding();
  294. }
  295. }
  296. }
  297. }