ShuLiForm.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using CCDCount.DLL;
  2. using CCDCount.MODEL.ShuLiClass;
  3. using MvCameraControl;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Windows.Forms;
  12. using CCDCount.MODEL.ConfigModel;
  13. namespace CCDCount.Forms
  14. {
  15. public partial class ShuLiForm : Form
  16. {
  17. #region 变量
  18. List<byte[]> ImageData = new List<byte[]>();
  19. LoadSplieImageClass loadSplieImageClass = new LoadSplieImageClass();
  20. //MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass());
  21. MainThreadClass mainThread = new MainThreadClass(new ShuLiConfigClass(),new MODEL.ConfigModel.CameraConfig());
  22. List<RowStartEndCol> RowsShowList = new List<RowStartEndCol>();
  23. Stopwatch ShowImageStopwatch = new Stopwatch();
  24. int Frame = 24; // 帧率
  25. #endregion
  26. #region 测试
  27. ShuLiClass shuLiClass = null;
  28. #endregion
  29. #region 构造函数
  30. public ShuLiForm()
  31. {
  32. InitializeComponent();
  33. SDKSystem.Initialize();
  34. this.FormBorderStyle = FormBorderStyle.None;
  35. // 全屏显示
  36. this.WindowState = FormWindowState.Maximized;
  37. // 窗口置顶
  38. //this.TopMost = true;
  39. button4.Enabled = false;
  40. button5.Enabled = false;
  41. button8.Enabled = false;
  42. }
  43. #endregion
  44. #region 窗口事件
  45. private void button1_Click(object sender, EventArgs e)
  46. {
  47. var openFileDialog = new OpenFileDialog
  48. {
  49. Title = "请选择文件", // 对话框标题
  50. Filter = "图片文件|*.bmp;*.jpg;*.png", // 文件类型过滤器
  51. Multiselect = false // 是否允许多选
  52. };
  53. // 显示对话框并判断结果
  54. if (openFileDialog.ShowDialog() == DialogResult.OK)
  55. {
  56. string selectedFilePath = openFileDialog.FileName;
  57. loadSplieImageClass.LoadImage(selectedFilePath);
  58. ImageData = loadSplieImageClass.SplieImage();
  59. }
  60. }
  61. private void button2_Click(object sender, EventArgs e)
  62. {
  63. RowsShowList.Clear();
  64. shuLiClass = new ShuLiClass();
  65. shuLiClass.UpdateIdentifyImageWidth(ImageData[0].Count());
  66. shuLiClass.InitChannel();
  67. shuLiClass.WorkCompleted += Worker_MianThreadToFrom;
  68. Stopwatch sw = new Stopwatch();
  69. List<ActiveObjectClass> ActivesList = new List<ActiveObjectClass>();
  70. for (int i = 0; i < ImageData.Count; i++)
  71. {
  72. sw.Restart();
  73. shuLiClass.ProcessImageSequence(ImageData[i], ImageData[i].Count());
  74. sw.Stop();
  75. Console.WriteLine(sw.Elapsed.ToString());
  76. }
  77. if (shuLiClass.GetHistoryActiveNum() > 0)
  78. {
  79. button8.Enabled = true;
  80. }
  81. }
  82. private void button3_Click(object sender, EventArgs e)
  83. {
  84. if (shuLiClass!=null)
  85. {
  86. shuLiClass = null;
  87. RowsShowList.Clear();
  88. }
  89. if (mainThread.StartMianThread())
  90. {
  91. ShowImageStopwatch.Start();
  92. mainThread.WorkerToFrom += Worker_MianThreadToFrom;
  93. button3.Enabled = false;
  94. button4.Enabled = true;
  95. button8.Enabled = false;
  96. }
  97. else
  98. {
  99. MessageBox.Show("请检查相机是否连接!");
  100. }
  101. }
  102. private void button4_Click(object sender, EventArgs e)
  103. {
  104. mainThread.StopMianThread();
  105. mainThread.WorkerToFrom -= Worker_MianThreadToFrom;
  106. button3.Enabled = true;
  107. button4.Enabled = false;
  108. if(mainThread.shuLiClass.GetHistoryActiveNum() > 0)
  109. button8.Enabled = true;
  110. }
  111. private void button5_Click(object sender, EventArgs e)
  112. {
  113. mainThread.SaveAllConfig();
  114. }
  115. private void button8_Click(object sender, EventArgs e)
  116. {
  117. HistoryImageDataForm historyDataForm = new HistoryImageDataForm();
  118. //图像测试模式下获取历史数据
  119. if (shuLiClass != null)
  120. historyDataForm.historyActive = shuLiClass.GetHistoryActive();
  121. //相机模式下获取历史数据
  122. else if (mainThread.shuLiClass != null)
  123. historyDataForm.historyActive = mainThread.shuLiClass.GetHistoryActive();
  124. historyDataForm.ShowDialog();
  125. }
  126. private void ImageMoniShuLiForm_FormClosing(object sender, FormClosingEventArgs e)
  127. {
  128. SDKSystem.Finalize();
  129. mainThread.StopMianThread();
  130. Thread.Sleep(1000);
  131. //System.Windows.Forms.Application.Exit();
  132. this.Dispose();
  133. }
  134. #endregion
  135. #region 主界面回调事件
  136. /// <summary>
  137. /// 通知主界面回调的事件
  138. /// </summary>
  139. /// <param name="sender"></param>
  140. /// <param name="e"></param>
  141. private void Worker_MianThreadToFrom(object sender, ActiveObjectEventArgsClass e)
  142. {
  143. // 事件处理逻辑
  144. Console.WriteLine("结果已通知到主界面!");
  145. e.Actives.ForEach(o => o.RowsData.ForEach(p => RowsShowList.Add(p)));
  146. e.Actives.ForEach(o => PrintMianPicrture(o));
  147. }
  148. #endregion
  149. #region 私有方法
  150. /// <summary>
  151. /// 渲染画面方法
  152. /// </summary>
  153. /// <param name="activeObjects"></param>
  154. private void PrintMianPicrture(ActiveObjectClass activeObject)
  155. {
  156. var timeChaZhi = (DateTime.Now - activeObject.EndCheckTime).TotalMilliseconds;
  157. UpdateLable2("总数量:" + activeObject.Num.ToString());
  158. UpdateRichTextBox1(string.Format("第{0}识别结束到展示总用时:\n{1}毫秒", activeObject.Num, timeChaZhi));
  159. UpdateRichTextBox1(string.Format("第{0}识别结束面积:{1}像素", activeObject.Num, activeObject.Area));
  160. UpdateRichTextBox1(string.Format("第{0}识别结果所属通达数为{1}", activeObject.Num, activeObject.ChannelNO+1));
  161. Bitmap bitmap = new Bitmap(activeObject.ImageWidth, 2048);
  162. Graphics g = Graphics.FromImage(bitmap);
  163. Pen redPen = new Pen(Color.Red, 1);
  164. List<RowStartEndCol> ShowList = RowsShowList.Where(o => o.RowsCol > activeObject.LastSeenLine - bitmap.Height).ToList();
  165. RowsShowList.Where(o => o.RowsCol < activeObject.LastSeenLine - bitmap.Height).ToList().ForEach(o => RowsShowList.Remove(o));
  166. if (ShowImageStopwatch.ElapsedMilliseconds < (1000 / Frame))
  167. {
  168. return;
  169. }
  170. else
  171. {
  172. ShowImageStopwatch.Restart();
  173. }
  174. ShowList.ForEach(o => g.DrawLine(redPen, new Point(o.StartCol, (int)(activeObject.LastSeenLine - o.RowsCol)), new Point(o.EndCol, (int)(activeObject.LastSeenLine - o.RowsCol))));
  175. UpdatepictureBox1(bitmap.Clone() as Bitmap);
  176. }
  177. private void UpdateLable2(string text)
  178. {
  179. if (label2.InvokeRequired)
  180. {
  181. label2.Invoke(new Action<string>(UpdateLable2), text);
  182. }
  183. else
  184. {
  185. label2.Text = text;
  186. }
  187. }
  188. private void UpdatepictureBox1(Bitmap image)
  189. {
  190. if (pictureBox1.InvokeRequired)
  191. {
  192. pictureBox1.Invoke(new Action<Bitmap>(UpdatepictureBox1), image);
  193. }
  194. else
  195. {
  196. pictureBox1.Image = image;
  197. }
  198. }
  199. private void UpdateRichTextBox1(string text)
  200. {
  201. if (richTextBox1.InvokeRequired)
  202. {
  203. richTextBox1.Invoke(new Action<string>(UpdateRichTextBox1), text);
  204. }
  205. else
  206. {
  207. richTextBox1.SelectionStart = richTextBox1.TextLength;
  208. richTextBox1.SelectionLength = 0;
  209. richTextBox1.AppendText(text + Environment.NewLine);
  210. richTextBox1.ScrollToCaret();
  211. }
  212. }
  213. #endregion
  214. private void button1_Click_1(object sender, EventArgs e)
  215. {
  216. this.Close();
  217. }
  218. }
  219. }