IdentifyCameraForm.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using CCDCount.DLL;
  2. using CCDCount.MODEL.ConfigModel;
  3. using LogClass;
  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.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace CCDCount.Forms
  14. {
  15. public partial class IdentifyCameraForm : Form
  16. {
  17. #region 变量
  18. private List<CameraConfig> camerasConfig = null;
  19. private bool IsShow = false;
  20. #endregion
  21. #region 实例
  22. //主线程实例队列
  23. public List<MainThreadClass> LsMainThread = null;
  24. private MainThreadClass NowLoadMianThread = null;
  25. #endregion
  26. #region 窗体事件
  27. public IdentifyCameraForm(List<CameraConfig> CamerasConfigValue, List<MainThreadClass> lsMainThread)
  28. {
  29. InitializeComponent();
  30. camerasConfig = CamerasConfigValue;
  31. this.WindowState = FormWindowState.Maximized;
  32. LsMainThread = lsMainThread;
  33. StartShowImageThread();
  34. UpdateTapPage();
  35. }
  36. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
  37. {
  38. if (tabCameraControl.SelectedTab != null)
  39. {
  40. string currentTabName = tabCameraControl.SelectedTab.Name.Split('_')[1];
  41. if (LsMainThread.Count == 0)
  42. {
  43. LOG.error(string.Format("{0}:主识别线程均未创建成功", "IdentifyCameraForm-tabControl1_SelectedIndexChanged"));
  44. MessageBox.Show("主识别线程创建失败");
  45. return;
  46. }
  47. // 实际使用时替换为业务逻辑
  48. NowLoadMianThread = LsMainThread.Where(o => o.ThisCameraSN == currentTabName).First();
  49. LOG.log(string.Format("{0}:当前加载的为{1}号相机", "IdentifyCameraForm-tabControl1_SelectedIndexChanged", NowLoadMianThread.ThisCamerNo));
  50. }
  51. }
  52. /// <summary>
  53. /// 界面关闭
  54. /// </summary>
  55. /// <param name="sender"></param>
  56. /// <param name="e"></param>
  57. private void IdentifyCameraForm_FormClosing(object sender, FormClosingEventArgs e)
  58. {
  59. stopShowImageThread();
  60. this.Dispose();
  61. base.OnClosing(e);
  62. }
  63. #endregion
  64. #region 公共方法
  65. #endregion
  66. #region 私有方法
  67. /// <summary>
  68. /// 更新标签页
  69. /// </summary>
  70. private void UpdateTapPage()
  71. {
  72. if (camerasConfig != null)
  73. {
  74. tabCameraControl.TabPages.Clear();
  75. foreach (var item in camerasConfig)
  76. {
  77. TabPage tabPage = new TabPage();
  78. tabPage.Text = item.CameraName == string.Empty?item.DeviceName:item.CameraName;
  79. tabPage.Name = string.Format("CameratabPage_{0}", item.CameraSNNum);
  80. tabCameraControl.TabPages.Add(tabPage);
  81. //var labelvalue = FindControlsBFS(this, o => o.Name == item.CameraName);
  82. }
  83. tabControl1_SelectedIndexChanged(this, null);
  84. }
  85. }
  86. /// <summary>
  87. /// 控件广度优先搜索
  88. /// </summary>
  89. /// <param name="root"></param>
  90. /// <param name="condition"></param>
  91. /// <returns></returns>
  92. private Control FindControlsBFS(Control root, Func<Control, bool> condition)
  93. {
  94. var results = new List<Control>();
  95. var queue = new Queue<Control>();
  96. queue.Enqueue(root);
  97. while (queue.Count > 0)
  98. {
  99. var current = queue.Dequeue();
  100. if (condition(current)) results.Add(current);
  101. foreach (Control child in current.Controls)
  102. {
  103. queue.Enqueue(child);
  104. }
  105. }
  106. if (results.Count == 0) return null;
  107. return results.First();
  108. }
  109. private void StartShowImageThread()
  110. {
  111. IsShow = true;
  112. Bitmap image = null;
  113. Stopwatch renderSW = new Stopwatch();
  114. const int targetMsPerFrame = 100; // 20FPS=50ms
  115. Task.Run(() =>
  116. {
  117. while (IsShow)
  118. {
  119. renderSW.Restart();
  120. if (NowLoadMianThread == null)
  121. {
  122. LOG.log(string.Format("{0}:当前加载相机空", "IdentifyCameraForm-StartShowImageThread"));
  123. Thread.Sleep(100);
  124. continue;
  125. }
  126. if (NowLoadMianThread.CameraStatic)
  127. {
  128. NowLoadMianThread.GetShowImage(2048, out image);
  129. }
  130. else
  131. {
  132. if(image.Size!=new Size(1,1))
  133. image = new Bitmap(1, 1);
  134. Thread.Sleep(100);
  135. LOG.log(string.Format("{0}-{1}号相机线程启动异常", "IdentifyCameraForm-StartShowImageThread:", NowLoadMianThread.ThisCamerNo));
  136. }
  137. if (image == null)
  138. {
  139. LOG.log(string.Format("{0}:获取图片失败", "IdentifyCameraForm-StartShowImageThread"));
  140. continue;
  141. }
  142. UpdatepictureBox1(image);
  143. //进行精准20帧控制
  144. int elapsed = (int)renderSW.ElapsedMilliseconds;
  145. if (elapsed < targetMsPerFrame)
  146. {
  147. Thread.Sleep(targetMsPerFrame - elapsed);
  148. }
  149. }
  150. });
  151. }
  152. private void stopShowImageThread()
  153. {
  154. IsShow = false;
  155. }
  156. private void UpdatepictureBox1(Bitmap image)
  157. {
  158. LOG.log(string.Format("{0}:{1}号相机线程,更新显示图片", "IdentifyCameraForm-UpdatepictureBox1", NowLoadMianThread.ThisCamerNo), 0);
  159. if (ImageShowBox.InvokeRequired)
  160. {
  161. try
  162. {
  163. ImageShowBox.Invoke(new Action<Bitmap>(UpdatepictureBox1), image);
  164. }
  165. catch (Exception ex)
  166. {
  167. LOG.log(string.Format("{0}:{1}号相机线程,更新显示图片失败,{2}",
  168. "IdentifyCameraForm-UpdatepictureBox1", NowLoadMianThread.ThisCamerNo,ex.Message.ToString()));
  169. }
  170. }
  171. else
  172. {
  173. ImageShowBox.Image = image;
  174. }
  175. }
  176. #endregion
  177. }
  178. }