Photo.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 bool TakePhoto() {
  34. if (!iSEM.SetMagnification(prm.Mag))
  35. {
  36. return false;
  37. }
  38. float set = prm.Mag;
  39. Thread.Sleep(500);
  40. float current = iSEM.GetMagnification();
  41. while (Math.Abs(current - set) > 1)
  42. {
  43. iSEM.SetMagnification(set);
  44. Thread.Sleep(200);
  45. current = iSEM.GetMagnification();
  46. }
  47. iSEM.CmdFocusScanSpeed("CMD_SCANRATE5");//iSEM.CmdSaveRate();
  48. Thread.Sleep(500);
  49. //拍照前改变速度,延时
  50. iSEM.CmdLineScan();
  51. Thread.Sleep(200);
  52. float cycle_time = iSEM.GetCycleTime();
  53. Thread.Sleep(Convert.ToInt32(cycle_time) + 100);
  54. if (!GetImage(prm.savePath))
  55. {
  56. log.Error("SEM拍照失败", false);
  57. return false;
  58. }
  59. iSEM.CmdPixelScan();
  60. Thread.Sleep(200);
  61. iSEM.CmdFocusScanSpeed("CMD_SCANRATE4");//CmdFocusRate(4);
  62. Thread.Sleep(500);
  63. return true;
  64. }
  65. //拍图
  66. public bool GetImage(String a_fileName)
  67. {
  68. log.Info("获取分辨率开始!", true);
  69. //3. 获取分辨率
  70. int[] ImageSize = iSEM.GetImageStore();
  71. if (ImageSize[0] == 0 || ImageSize[1] == 0)
  72. {
  73. return false;
  74. }
  75. short width = (short)ImageSize[0];
  76. short height = (short)ImageSize[1];
  77. log.Info("获取分辨率结束!" + a_fileName + "====" + width + "*" + height, true);
  78. //4. 抓图
  79. if (!iSEM.GrabImage(a_fileName, 0, 0, width, height, 0))
  80. {
  81. log.Info("抓图失败!", true);
  82. return false;
  83. }
  84. return true;
  85. }
  86. }
  87. }