PicoSartScanController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. int get_image_type()
  38. {
  39. if (imageWidth == 640)
  40. {
  41. return 0;
  42. }
  43. else if (imageWidth == 1280)
  44. {
  45. return 1;
  46. }
  47. else if (imageWidth == 2560)
  48. {
  49. return 2;
  50. }
  51. return 0;
  52. }
  53. int get_scan_time()
  54. {
  55. if (imageWidth == 640)
  56. {
  57. return 17000;
  58. }
  59. else if (imageWidth == 1280)
  60. {
  61. return 20000;
  62. }
  63. else if (imageWidth == 2560)
  64. {
  65. return 30000;
  66. }
  67. return 20000;
  68. }
  69. ImageInputSourceType imagesourceType = new ImageInputSourceType();
  70. public PicoSartScanController()
  71. {
  72. m_api = PicoSmartSemController.GetApiClassInstance();
  73. }
  74. public void set_scan_chanel(int scan_chanel = 1)
  75. {
  76. if (m_api.isStart() == 1)
  77. {
  78. m_api.SET_SCANMODE((uint)(scan_chanel));
  79. }
  80. }
  81. public void set_scan_speed(int scan_speed = 1)
  82. {
  83. if (m_api.isStart() == 1)
  84. {
  85. m_api.SET_SCANSPEED((uint)(scan_speed));
  86. }
  87. }
  88. CBSEImgClr IScanController.AcquireBSEImage()
  89. {
  90. try
  91. {
  92. Rectangle r = new Rectangle();
  93. CBSEImgClr bse = new CBSEImgClr(r);
  94. bse.InitImageData(imageWidth, imageHeight);
  95. ImageAquistionParam p = new ImageAquistionParam();
  96. p.width = imageWidth;
  97. p.height = imageHeight;
  98. p.DwellTime = dwelltime;
  99. p.sourceType = imagesourceType;
  100. //byte[] ImageData = new byte[imageWidth * imageHeight * 3];
  101. byte[] ImageData;
  102. NLog.LogManager.GetCurrentClassLogger().Info("Begin to acquire BSE image!");
  103. //get image
  104. Mat image = new Mat();
  105. int imagtype = get_image_type();
  106. m_api.GET_IMAGE((uint)scan_chanel, imagtype);
  107. Thread.Sleep(50);
  108. int waittime = get_scan_time();
  109. if (m_api.GET_IMAGE_MSGDATA(ref image, waittime) == 1)
  110. {
  111. if (!image.Empty())
  112. {
  113. if(image.Channels()==3)
  114. {
  115. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  116. }
  117. Cv2.ImWrite("d:/sem.png", image);
  118. Bitmap bitmap = image.ToBitmap();
  119. using (MemoryStream stream = new MemoryStream())
  120. {
  121. bitmap.Save(stream, ImageFormat.Bmp);
  122. ImageData = stream.ToArray();
  123. bse.SetImageData(ImageData, image.Cols, image.Rows);
  124. NLog.LogManager.GetCurrentClassLogger().Info("End acquiring BSE image!");
  125. }
  126. }
  127. }
  128. else
  129. {
  130. NLog.LogManager.GetCurrentClassLogger().Info("acquiring BSE image timeout!");
  131. }
  132. //bse.SetImageData(p.ImageData, imageWidth, imageHeight);
  133. return bse;
  134. }
  135. catch (Exception e)
  136. {
  137. NLog.LogManager.GetCurrentClassLogger().Warn(e.Message);
  138. }
  139. return null;
  140. }
  141. bool IScanController.Init()
  142. {
  143. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  144. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  145. if (FEIIP == "" || FEIPORT == "")
  146. {
  147. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  148. return false;
  149. }
  150. m_api.set_ip(FEIIP);
  151. m_api.set_port(int.Parse(FEIPORT));
  152. if (m_api.isStart() == 1)
  153. {
  154. return true;
  155. }
  156. if (m_api.start() == 1)
  157. {
  158. return true;
  159. }
  160. return false;
  161. }
  162. //bool IScanController.SetDwellTime(double val)
  163. //{
  164. // scan_chanel = (int)val;
  165. // return true;
  166. //}
  167. bool IScanController.SetImageSize(int nWidth, int nHeight)
  168. {
  169. this.imageWidth = nWidth;
  170. this.imageHeight = nHeight;
  171. return true;
  172. }
  173. public bool SetDwellTime(DwellTimeLevel val)
  174. {
  175. return true;
  176. }
  177. public CBSEImgClr AcquireRectangleBSEImage(Rectangle rec)
  178. {
  179. Rectangle r = new Rectangle();
  180. CBSEImgClr bse = new CBSEImgClr(r);
  181. return bse;
  182. }
  183. }
  184. }