HalconTool.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using CommonMethods;
  9. using HalconDotNet;
  10. using ToolBase;
  11. using System.Diagnostics;
  12. using ViewROI;
  13. using System.IO;
  14. namespace HalconTool
  15. {
  16. [Serializable]
  17. public class HalconTool: IToolBase
  18. {
  19. /// <summary>
  20. /// 流程名
  21. /// </summary>
  22. public string jobName = string.Empty;
  23. /// <summary>
  24. /// 曝光时间
  25. /// </summary>
  26. public Int32 exposure = 5;
  27. /// <summary>
  28. /// 图像的获取方式
  29. /// </summary>
  30. public ImageSourceMode imageSourceMode = ImageSourceMode.FormDevice;
  31. /// <summary>
  32. /// 是否处于实时采集模式
  33. /// </summary>
  34. public bool realTimeMode = false;
  35. /// <summary>
  36. /// 相机句柄
  37. /// </summary>
  38. public Int64 cameraHandle = -1;
  39. /// <summary>
  40. /// 设备信息字符串,包括了相机SN、品牌等信息
  41. /// </summary>
  42. public string deviceInfoStr = string.Empty;
  43. /// <summary>
  44. /// 实时采集线程
  45. /// </summary>
  46. public static Thread th_acq ; //Thread类不能序列化,所以申明为静态的
  47. /// <summary>
  48. /// 读取文件夹图像模式时每次运行是否自动切换图像
  49. /// </summary>
  50. public bool autoSwitch = true;
  51. /// <summary>
  52. /// 是否将彩色图像转化成灰度图像
  53. /// </summary>
  54. public bool RGBToGray = true;
  55. /// <summary>
  56. /// 工作模式为读取文件夹图像时,当前图像的名称
  57. /// </summary>
  58. public string currentImageName = "";
  59. /// <summary>
  60. /// 工作模式为读取文件夹图像时,当前显示的图片的索引
  61. /// </summary>
  62. public int currentImageIndex = 0;
  63. /// <summary>
  64. /// 文件夹中的图像文件集合
  65. /// </summary>
  66. public List<string> L_imageFile = new List<string>();
  67. /// <summary>
  68. /// 单张图像的文件路径
  69. /// </summary>
  70. public string imagePath = string.Empty;
  71. /// <summary>
  72. /// 图像文件夹路径
  73. /// </summary>
  74. public string imageDirectoryPath = string.Empty;
  75. /// <summary>
  76. /// 输出图像
  77. /// </summary>
  78. [NonSerialized]
  79. public HObject outputImage = new HObject();
  80. /// <summary>
  81. /// 输出图像的路径
  82. /// </summary>
  83. public string outputImageFilePath = null;
  84. /// <summary>
  85. /// 读取单张图像或批量读取文件夹图像工作模式
  86. /// </summary>
  87. internal WorkMode workMode = WorkMode.ReadMultImage;
  88. public override void Run(SoftwareRunState softwareState)
  89. {
  90. Stopwatch sw = new Stopwatch();
  91. sw.Restart();
  92. softwareRunState = softwareState;
  93. if(workMode == WorkMode.ReadOneImage)
  94. {
  95. DispImage();
  96. }
  97. else
  98. {
  99. if (currentImageIndex <= L_imageFile.Count && L_imageFile.Count != 0)
  100. {
  101. currentImageIndex = currentImageIndex == L_imageFile.Count ? 0 : currentImageIndex;
  102. outputImageFilePath = L_imageFile[currentImageIndex];
  103. DispImage();
  104. currentImageName = Path.GetFileName(L_imageFile[currentImageIndex]);
  105. if(softwareState == SoftwareRunState.Release)
  106. currentImageIndex++;
  107. }
  108. }
  109. SetToolStatusDisp();
  110. sw.Stop();
  111. runTime = $"工具运行时间:{sw.ElapsedMilliseconds.ToString()} ms";
  112. }
  113. public override void DispImage()
  114. {
  115. HObject image = new HObject();
  116. try
  117. {
  118. if(outputImageFilePath != null && outputImageFilePath != "")
  119. {
  120. HOperatorSet.ReadImage(out image, outputImageFilePath);
  121. if (RGBToGray)
  122. {
  123. HTuple channel;
  124. HOperatorSet.CountChannels(image, out channel);
  125. if (channel == 3)
  126. HOperatorSet.Rgb1ToGray(image, out image);
  127. }
  128. outputImage = image;
  129. }
  130. else
  131. {
  132. runMessage = $"图像文件路径为空!";
  133. toolRunStatu = ToolRunStatu.File_Error_Or_Path_Invalid;
  134. return;
  135. }
  136. }
  137. catch(Exception ex)
  138. {
  139. if(softwareRunState == SoftwareRunState.Debug)
  140. {
  141. runMessage = $"图像文件异常或路径不合法{ex}";
  142. toolRunStatu = ToolRunStatu.File_Error_Or_Path_Invalid;
  143. }
  144. return;
  145. }
  146. if (outputImage != null)
  147. {
  148. if (softwareRunState == SoftwareRunState.Debug)
  149. {
  150. FormHalconTool.Instance.myHwindow.DispImage(outputImage);
  151. }
  152. }
  153. else
  154. {
  155. runMessage = $"图像为空!";
  156. toolRunStatu = ToolRunStatu.Lack_Of_Input_Image;
  157. return;
  158. }
  159. toolRunStatu = ToolRunStatu.Succeed;
  160. }
  161. public void SetToolStatusDisp()
  162. {
  163. FormHalconTool.Instance.lb_RunStatus.Text = toolRunStatu == ToolRunStatu.Succeed ? "工具运行成功!" : $"工具运行异常, 异常原因:{runMessage}";
  164. FormHalconTool.Instance.lb_RunTime.Text = runTime;
  165. if (toolRunStatu == ToolRunStatu.Succeed)
  166. {
  167. FormHalconTool.Instance.statusStrip.BackColor = System.Drawing.Color.LimeGreen;
  168. }
  169. else
  170. {
  171. FormHalconTool.Instance.statusStrip.BackColor = System.Drawing.Color.Red;
  172. }
  173. }
  174. public override void DispMainWindow(HWindowTool_Smart window)
  175. {
  176. throw new NotImplementedException();
  177. }
  178. }
  179. }