Photo.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 NLog;
  8. using SmartSEMControl;
  9. namespace MeasureThread
  10. {
  11. class Photo : IPhoto
  12. {
  13. NLog.Logger log;
  14. private PhotoParam prm;
  15. private ISEMControl iSEM;
  16. public Photo( ISEMControl sem)
  17. {
  18. this.log = NLog.LogManager.GetCurrentClassLogger();
  19. this.iSEM = sem ?? throw new ArgumentNullException(nameof(sem));
  20. }
  21. public void SetPhotoParam(PhotoParam a_prm)
  22. {
  23. this.prm = a_prm ?? throw new ArgumentNullException(nameof(a_prm));
  24. }
  25. public PhotoParam GetParam()
  26. {
  27. if (prm == null)
  28. {
  29. prm = new PhotoParam();
  30. }
  31. return prm;
  32. }
  33. public void EnforceBrightness()
  34. {
  35. float b = iSEM.GetBrightness();
  36. Thread.Sleep(100);
  37. iSEM.SetBrightness(b + 1);
  38. Thread.Sleep(200);
  39. }
  40. public void EnforceContrast()
  41. {
  42. float c= iSEM.GetContrast();
  43. Thread.Sleep(100);
  44. iSEM.SetContrast(c + 1);
  45. Thread.Sleep(200);
  46. }
  47. public bool TakePhoto() {
  48. if (!iSEM.SetMagnification(prm.Mag))
  49. {
  50. return false;
  51. }
  52. float set = prm.Mag;
  53. Thread.Sleep(500);
  54. float current = iSEM.GetMagnification();
  55. while (Math.Abs(current - set) > 1)
  56. {
  57. iSEM.SetMagnification(set);
  58. Thread.Sleep(200);
  59. current = iSEM.GetMagnification();
  60. }
  61. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//iSEM.CmdSaveRate();
  62. Thread.Sleep(200);
  63. float cycle_time = iSEM.GetCycleTime();
  64. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  65. //拍照前改变速度,延时
  66. iSEM.CmdLineScan();
  67. Thread.Sleep(200);
  68. cycle_time = iSEM.GetCycleTime();
  69. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  70. if (!GetImage(prm.savePath))
  71. {
  72. log.Error("SEM拍照失败", false);
  73. return false;
  74. }
  75. iSEM.CmdPixelScan();
  76. Thread.Sleep(200);
  77. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//CmdFocusRate(4);
  78. cycle_time = iSEM.GetCycleTime();
  79. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  80. return true;
  81. }
  82. //拍图
  83. public bool GetImage(String a_fileName)
  84. {
  85. log.Info("获取分辨率开始!", true);
  86. //3. 获取分辨率
  87. int[] ImageSize = iSEM.GetImageStore();
  88. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  89. {
  90. return false;
  91. }
  92. short width = (short)ImageSize[0];
  93. short height = (short)ImageSize[1];
  94. log.Info("获取分辨率结束!" + a_fileName + "====" + width + "*" + height, true);
  95. //4. 抓图
  96. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  97. {
  98. log.Info("抓图失败!", true);
  99. return false;
  100. }
  101. return true;
  102. }
  103. }
  104. }