PicoSartScanController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. namespace OTSMeasureApp.ServiceCenter
  19. {
  20. public class PicoSartScanController : IScanController
  21. {
  22. public PicoSmartApi_cshape m_api;
  23. private int imageWidth = 640;
  24. private int imageHeight = 480;
  25. double dwelltime = 10;
  26. int port = 7321;
  27. string ip = "127.0.0.1";
  28. /// <summary>
  29. /// //se 0;bse 1;se_bse 2;
  30. /// </summary>
  31. int scan_chanel = 1;
  32. /// <summary>
  33. /// //red 0; fast 1; slow 2; slow2 3
  34. /// </summary>
  35. int scan_speed = 1;
  36. int get_image_type()
  37. {
  38. if (imageWidth == 640)
  39. {
  40. return 0;
  41. }
  42. else if (imageWidth == 1280)
  43. {
  44. return 1;
  45. }
  46. else if (imageWidth == 2560)
  47. {
  48. return 2;
  49. }
  50. return 0;
  51. }
  52. int get_scan_time()
  53. {
  54. if (imageWidth == 640)
  55. {
  56. return 17000;
  57. }
  58. else if (imageWidth == 1280)
  59. {
  60. return 20000;
  61. }
  62. else if (imageWidth == 2560)
  63. {
  64. return 30000;
  65. }
  66. return 20000;
  67. }
  68. ImageInputSourceType imagesourceType = new ImageInputSourceType();
  69. public void set_scan_chanel(int scan_chanel = 1)
  70. {
  71. if (m_api.isStart() == 1)
  72. {
  73. m_api.SET_SCANMODE((uint)(scan_chanel));
  74. }
  75. }
  76. public void set_scan_speed(int scan_speed = 1)
  77. {
  78. if (m_api.isStart() == 1)
  79. {
  80. m_api.SET_SCANSPEED((uint)(scan_speed));
  81. }
  82. }
  83. CBSEImgClr IScanController.AcquireBSEImage()
  84. {
  85. try
  86. {
  87. Rectangle r = new Rectangle();
  88. CBSEImgClr bse = new CBSEImgClr(r);
  89. bse.InitImageData(imageWidth, imageHeight);
  90. ImageAquistionParam p = new ImageAquistionParam();
  91. p.width = imageWidth;
  92. p.height = imageHeight;
  93. p.DwellTime = dwelltime;
  94. p.sourceType = imagesourceType;
  95. //byte[] ImageData = new byte[imageWidth * imageHeight * 3];
  96. byte[] ImageData;
  97. NLog.LogManager.GetCurrentClassLogger().Info("Begin to acquire BSE image!");
  98. //get image
  99. Mat image = new Mat();
  100. int imagtype = get_image_type();
  101. m_api.GET_IMAGE((uint)scan_chanel, imagtype);
  102. Thread.Sleep(50);
  103. int waittime = get_scan_time();
  104. if (m_api.GET_IMAGE_MSGDATA(ref image, waittime) == 1)
  105. {
  106. if (!image.Empty())
  107. {
  108. Bitmap bitmap = image.ToBitmap();
  109. using (MemoryStream stream = new MemoryStream())
  110. {
  111. bitmap.Save(stream, ImageFormat.Png); // 你可以选择其它的格式,比如Jpeg
  112. ImageData = stream.ToArray();
  113. bse.SetImageData(ImageData, imageWidth, imageHeight);
  114. NLog.LogManager.GetCurrentClassLogger().Info("End acquiring BSE image!");
  115. }
  116. }
  117. }
  118. else
  119. {
  120. NLog.LogManager.GetCurrentClassLogger().Info("acquiring BSE image timeout!");
  121. }
  122. //bse.SetImageData(p.ImageData, imageWidth, imageHeight);
  123. return bse;
  124. }
  125. catch (Exception e)
  126. {
  127. NLog.LogManager.GetCurrentClassLogger().Warn(e.Message);
  128. }
  129. return null;
  130. }
  131. bool IScanController.Init()
  132. {
  133. m_api = new PicoSmartApi_cshape();
  134. m_api.set_ip(ip);
  135. m_api.set_port(port);
  136. if (m_api.start() == 1)
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142. bool IScanController.SetDwellTime(double val)
  143. {
  144. scan_chanel = (int)val;
  145. return true;
  146. }
  147. bool IScanController.SetImageSize(int nWidth, int nHeight)
  148. {
  149. this.imageWidth = nWidth;
  150. this.imageWidth = nHeight;
  151. return true;
  152. }
  153. }
  154. }