PicoSartScanController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Cv2.ImWrite("d:/sem.png", image);
  114. Bitmap bitmap = image.ToBitmap();
  115. using (MemoryStream stream = new MemoryStream())
  116. {
  117. bitmap.Save(stream, ImageFormat.Png); // 你可以选择其它的格式,比如Jpeg
  118. ImageData = stream.ToArray();
  119. bse.SetImageData(ImageData, image.Cols, image.Rows);
  120. NLog.LogManager.GetCurrentClassLogger().Info("End acquiring BSE image!");
  121. }
  122. }
  123. }
  124. else
  125. {
  126. NLog.LogManager.GetCurrentClassLogger().Info("acquiring BSE image timeout!");
  127. }
  128. //bse.SetImageData(p.ImageData, imageWidth, imageHeight);
  129. return bse;
  130. }
  131. catch (Exception e)
  132. {
  133. NLog.LogManager.GetCurrentClassLogger().Warn(e.Message);
  134. }
  135. return null;
  136. }
  137. bool IScanController.Init()
  138. {
  139. string FEIIP = FileHelper.GetXMLInformations("FEIIP");
  140. string FEIPORT = FileHelper.GetXMLInformations("FEIPORT");
  141. if (FEIIP == "" || FEIPORT == "")
  142. {
  143. NLog.LogManager.GetCurrentClassLogger().Error("FEI电镜端口配置为空!");
  144. return false;
  145. }
  146. m_api.set_ip(FEIIP);
  147. m_api.set_port(int.Parse(FEIPORT));
  148. if (m_api.isStart() == 1)
  149. {
  150. return true;
  151. }
  152. if (m_api.start() == 1)
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. bool IScanController.SetDwellTime(double val)
  159. {
  160. scan_chanel = (int)val;
  161. return true;
  162. }
  163. bool IScanController.SetImageSize(int nWidth, int nHeight)
  164. {
  165. this.imageWidth = nWidth;
  166. this.imageHeight = nHeight;
  167. return true;
  168. }
  169. }
  170. }