PicoSartScanController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using OTSModelSharp.ServiceCenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using OTSMeasureApp.ServiceCenter;
  8. using OTSCLRINTERFACE;
  9. using OxfordExtenderWrapper;
  10. using System.Drawing;
  11. using static OxfordExtenderWrapper.ExtenderIpcUI;
  12. using OpenCvSharp;
  13. using System.Runtime.InteropServices.WindowsRuntime;
  14. using System.Threading;
  15. using OpenCvSharp.Extensions;
  16. using System.Drawing.Imaging;
  17. using System.IO;
  18. using OTSMeasureApp.ServiceCenter.PicoSmart;
  19. namespace OTSMeasureApp.ServiceCenter
  20. {
  21. public class PicoSartScanController : IScanController
  22. {
  23. public PicoSmartApi_cshape m_api=null;
  24. private int imageWidth = 640;
  25. private int imageHeight = 480;
  26. double dwelltime = 10;
  27. int port = 7321;
  28. string ip = "127.0.0.1";
  29. /// <summary>
  30. /// //se 0;bse 1;se_bse 2;
  31. /// </summary>
  32. int scan_chanel = 1;
  33. /// <summary>
  34. /// //red 0; fast 1; slow 2; slow2 3
  35. /// </summary>
  36. int scan_speed = 1;
  37. public void set_image_w(int w)
  38. {
  39. imageWidth = w;
  40. }
  41. public void set_image_h(int h)
  42. {
  43. imageHeight= h;
  44. }
  45. int get_image_type()
  46. {
  47. if (imageWidth == 640)
  48. {
  49. return 0;
  50. }
  51. else if (imageWidth == 1280)
  52. {
  53. return 1;
  54. }
  55. else if (imageWidth == 2560)
  56. {
  57. return 2;
  58. }
  59. return 0;
  60. }
  61. int get_scan_time()
  62. {
  63. if (imageWidth == 640)
  64. {
  65. return 17000;
  66. }
  67. else if (imageWidth == 1280)
  68. {
  69. return 20000;
  70. }
  71. else if (imageWidth == 2560)
  72. {
  73. return 30000;
  74. }
  75. return 20000;
  76. }
  77. ImageInputSourceType imagesourceType = new ImageInputSourceType();
  78. public PicoSartScanController()
  79. {
  80. m_api = PicoSmartSemController.GetApiClassInstance();
  81. }
  82. public void set_scan_chanel(int scan_chanel = 1)
  83. {
  84. if (m_api.isStart() == 1)
  85. {
  86. m_api.SET_SCANMODE((uint)(scan_chanel));
  87. }
  88. }
  89. public void set_scan_speed(int scan_speed = 1)
  90. {
  91. if (m_api.isStart() == 1)
  92. {
  93. m_api.SET_SCANSPEED((uint)(scan_speed));
  94. }
  95. }
  96. CBSEImgClr IScanController.AcquireBSEImage()
  97. {
  98. try
  99. {
  100. Rectangle r = new Rectangle();
  101. CBSEImgClr bse = new CBSEImgClr(r);
  102. bse.InitImageData(imageWidth, imageHeight);
  103. ImageAquistionParam p = new ImageAquistionParam();
  104. p.width = imageWidth;
  105. p.height = imageHeight;
  106. p.DwellTime = dwelltime;
  107. p.sourceType = imagesourceType;
  108. //byte[] ImageData = new byte[imageWidth * imageHeight * 3];
  109. byte[] ImageData;
  110. NLog.LogManager.GetCurrentClassLogger().Info("Begin to acquire BSE image!");
  111. //get image
  112. Mat image = new Mat();
  113. int imagtype = get_image_type();
  114. m_api.GET_IMAGE((uint)scan_chanel, imagtype);
  115. Thread.Sleep(50);
  116. int waittime = get_scan_time();
  117. if (m_api.GET_IMAGE_MSGDATA(ref image, waittime) == 1)
  118. {
  119. if (!image.Empty())
  120. {
  121. if(image.Channels()==3)
  122. {
  123. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  124. }
  125. Cv2.ImWrite("d:/sem.png", image);
  126. Bitmap bitmap = image.ToBitmap();
  127. using (MemoryStream stream = new MemoryStream())
  128. {
  129. bitmap.Save(stream, ImageFormat.Bmp);
  130. ImageData = stream.ToArray();
  131. bse.SetImageData(ImageData, image.Cols, image.Rows);
  132. NLog.LogManager.GetCurrentClassLogger().Info("End acquiring BSE image!");
  133. }
  134. }
  135. }
  136. else
  137. {
  138. NLog.LogManager.GetCurrentClassLogger().Info("acquiring BSE image timeout!");
  139. }
  140. //bse.SetImageData(p.ImageData, imageWidth, imageHeight);
  141. return bse;
  142. }
  143. catch (Exception e)
  144. {
  145. NLog.LogManager.GetCurrentClassLogger().Warn(e.Message);
  146. }
  147. return null;
  148. }
  149. bool IScanController.Init()
  150. {
  151. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  152. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  153. if (FEIIP == "" || FEIPORT == "")
  154. {
  155. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  156. return false;
  157. }
  158. m_api.set_ip(FEIIP);
  159. m_api.set_port(int.Parse(FEIPORT));
  160. if (m_api.isStart() == 1)
  161. {
  162. return true;
  163. }
  164. if (m_api.start() == 1)
  165. {
  166. return true;
  167. }
  168. return false;
  169. }
  170. //bool IScanController.SetDwellTime(double val)
  171. //{
  172. // scan_chanel = (int)val;
  173. // return true;
  174. //}
  175. bool IScanController.SetImageSize(int nWidth, int nHeight)
  176. {
  177. this.imageWidth = nWidth;
  178. this.imageHeight = nHeight;
  179. return true;
  180. }
  181. public bool SetDwellTime(DwellTimeLevel val)
  182. {
  183. return true;
  184. }
  185. public CBSEImgClr AcquireRectangleBSEImage(Rectangle rec)
  186. {
  187. Rectangle r = new Rectangle();
  188. CBSEImgClr bse = new CBSEImgClr(r);
  189. return bse;
  190. }
  191. }
  192. }