Photo.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(500);
  63. //拍照前改变速度,延时
  64. iSEM.CmdLineScan();
  65. Thread.Sleep(200);
  66. float cycle_time = iSEM.GetCycleTime();
  67. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  68. if (!GetImage(prm.savePath))
  69. {
  70. log.Error("SEM拍照失败", false);
  71. return false;
  72. }
  73. iSEM.CmdPixelScan();
  74. Thread.Sleep(200);
  75. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//CmdFocusRate(4);
  76. Thread.Sleep(500);
  77. return true;
  78. }
  79. //拍图
  80. public bool GetImage(String a_fileName)
  81. {
  82. log.Info("获取分辨率开始!", true);
  83. //3. 获取分辨率
  84. int[] ImageSize = iSEM.GetImageStore();
  85. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  86. {
  87. return false;
  88. }
  89. short width = (short)ImageSize[0];
  90. short height = (short)ImageSize[1];
  91. log.Info("获取分辨率结束!" + a_fileName + "====" + width + "*" + height, true);
  92. //4. 抓图
  93. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  94. {
  95. log.Info("抓图失败!", true);
  96. return false;
  97. }
  98. return true;
  99. }
  100. }
  101. }