Extender.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. //显示弹出信息
  10. using System.Windows.Forms;
  11. using OINA.Extender;
  12. using OINA.Extender.MicroscopeControl;
  13. using OINA.Extender.Acquisition;
  14. using OINA.Extender.Acquisition.Image;
  15. using OINA.Extender.Data;
  16. using OINA.Extender.Data.Image;
  17. using OINA.Extender.Acquisition.Ed;
  18. using OINA.Extender.Acquisition.Quant;
  19. using OINA.Extender.Processing;
  20. using OINA.Extender.Processing.Ed;
  21. using OINA.Extender.Processing.Quant;
  22. using System.ComponentModel;
  23. using OINA.Extender.Data.Ed;
  24. namespace Extender
  25. {
  26. public class Extender : IExtenderControl
  27. {
  28. //构造函数
  29. public Extender()
  30. {
  31. InitMicroscopeController();
  32. InitImageAcquisition();
  33. InitXrayAcquistion();
  34. }
  35. //结束
  36. public void CloseExtender()
  37. {
  38. CloseMicroscopeController();
  39. CloseImageAcquisition();
  40. CloaseXrayAcquistion();
  41. }
  42. #region 电镜控制、样品台
  43. //控制电镜
  44. private IMicroscopeController microscopeController = null;
  45. //电压
  46. private double m_dHighVoltage;
  47. //放大倍数
  48. private double m_dMagnification;
  49. //工作距离
  50. private double m_dWorkingDistance;
  51. //亮度
  52. private double m_dBirghtness;
  53. //对比度
  54. private double m_dContrast;
  55. //BeamOn
  56. private bool m_bBeamOn;
  57. //FilamentOn
  58. private bool m_bFilamentOn;
  59. //样品台位置
  60. private double m_dStageX;
  61. private double m_dStageY;
  62. private double m_dStageZ;
  63. private double m_dStageR;
  64. private double m_dStageT;
  65. private double m_dStageXMax;
  66. public double StageXMax
  67. {
  68. get { return this.m_dStageXMax; }
  69. set { this.m_dStageXMax = value; }
  70. }
  71. private double m_dStageXMin;
  72. public double StageXMin
  73. {
  74. get { return this.m_dStageXMin; }
  75. set { this.m_dStageXMin = value; }
  76. }
  77. private double m_dStageYMax;
  78. public double StageYMax
  79. {
  80. get { return this.m_dStageYMax; }
  81. set { this.m_dStageYMax = value; }
  82. }
  83. private double m_dStageYMin;
  84. public double StageYMin
  85. {
  86. get { return this.m_dStageYMin; }
  87. set { this.m_dStageYMin = value; }
  88. }
  89. private double m_dStageZMax;
  90. public double StageZMax
  91. {
  92. get { return this.m_dStageZMax; }
  93. set { this.m_dStageZMax = value; }
  94. }
  95. private double m_dStageZMin;
  96. public double StageZMin
  97. {
  98. get { return this.m_dStageZMin; }
  99. set { this.m_dStageZMin = value; }
  100. }
  101. private double m_dStageTMax;
  102. public double StageTMax
  103. {
  104. get { return this.m_dStageTMax; }
  105. set { this.m_dStageTMax = value; }
  106. }
  107. private double m_dStageTMin;
  108. public double StageTMin
  109. {
  110. get { return this.m_dStageTMin; }
  111. set { this.m_dStageTMin = value; }
  112. }
  113. private double m_dStageRMax;
  114. public double StageRMax
  115. {
  116. get { return this.m_dStageRMax; }
  117. set { this.m_dStageRMax = value; }
  118. }
  119. private double m_dStageRMin;
  120. public double StageRMin
  121. {
  122. get { return this.m_dStageRMin; }
  123. set { this.m_dStageRMin = value; }
  124. }
  125. //控制电镜初始化
  126. void InitMicroscopeController(float a_fStageXMin = 0, float a_fStageXMax = 130,
  127. float a_fStageYMin = 0, float a_fStageYMax = 130,
  128. float a_fStageZMin = 0, float a_fStageZMax = 40,
  129. float a_fStageTMin = 0, float a_fStageTMax = 90,
  130. float a_fStageRMin = 0, float a_fStageRMax = 360)
  131. {
  132. this.microscopeController = AcquireFactory.CreateMicroscopeControl();
  133. this.microscopeController.ColumnChange += this.OnMicroscopeColumnChange;
  134. this.microscopeController.StageChange += this.OnMicroscopeStageChange;
  135. this.microscopeController.ColumnConnected += this.OnMicroscopeColumnConnected;
  136. this.microscopeController.StageConnected += this.OnMicroscopeStageConnected;
  137. this.microscopeController.ChangeCompleted += this.OnMicroscopeChangeCompleted;
  138. ReadMicroscopeColumn();
  139. ReadStage();
  140. StageXMax = a_fStageXMax;
  141. StageXMin = a_fStageXMin;
  142. StageYMax = a_fStageYMax;
  143. StageYMin = a_fStageYMin;
  144. StageZMax = a_fStageZMax;
  145. StageZMin = a_fStageZMin;
  146. StageTMax = a_fStageTMax;
  147. StageTMin = a_fStageTMin;
  148. StageRMax = a_fStageRMax;
  149. StageRMin = a_fStageRMin;
  150. }
  151. //控制电镜释放
  152. void CloseMicroscopeController()
  153. {
  154. this.microscopeController.ColumnChange -= this.OnMicroscopeColumnChange;
  155. this.microscopeController.StageChange -= this.OnMicroscopeStageChange;
  156. this.microscopeController.ColumnConnected -= this.OnMicroscopeColumnConnected;
  157. this.microscopeController.StageConnected -= this.OnMicroscopeStageConnected;
  158. this.microscopeController.ChangeCompleted -= this.OnMicroscopeChangeCompleted;
  159. }
  160. //读取当前的电镜控制值
  161. private void ReadMicroscopeColumn()
  162. {
  163. var columnCapabilities = this.microscopeController.ColumnCapabilities;
  164. var columnConditions = this.microscopeController.ColumnConditions;
  165. if (columnCapabilities.Magnification.CanRead)
  166. {
  167. m_dMagnification = columnConditions.Magnification;
  168. }
  169. if (columnCapabilities.WorkingDistance.CanRead)
  170. {
  171. m_dWorkingDistance = columnConditions.WorkingDistance;
  172. }
  173. if (columnCapabilities.HighVoltage.CanRead)
  174. {
  175. m_dHighVoltage = columnConditions.HighVoltage;
  176. }
  177. if (columnCapabilities.Brightness.CanRead)
  178. {
  179. m_dBirghtness = columnConditions.Brightness;
  180. }
  181. if (columnCapabilities.Contrast.CanRead)
  182. {
  183. m_dContrast = columnConditions.Contrast;
  184. }
  185. if (columnCapabilities.BeamOn.CanRead)
  186. {
  187. m_bBeamOn = Convert.ToBoolean(columnConditions.BeamOn);
  188. }
  189. if (columnCapabilities.FilamentOn.CanRead)
  190. {
  191. m_bFilamentOn = Convert.ToBoolean(columnConditions.FilamentOn);
  192. }
  193. }
  194. //读取样品台位置
  195. private void ReadStage()
  196. {
  197. var stageCapabilities = this.microscopeController.StageCapabilities;
  198. var stageConditions = this.microscopeController.StageConditions;
  199. if (stageCapabilities.StageX.CanRead)
  200. {
  201. this.m_dStageX = stageConditions.StageX;
  202. }
  203. if (stageCapabilities.StageY.CanRead)
  204. {
  205. this.m_dStageY = stageConditions.StageY;
  206. }
  207. if (stageCapabilities.StageZ.CanRead)
  208. {
  209. this.m_dStageZ = stageConditions.StageZ;
  210. }
  211. if (stageCapabilities.StageT.CanRead)
  212. {
  213. this.m_dStageT = stageConditions.StageT;
  214. }
  215. if (stageCapabilities.StageR.CanRead)
  216. {
  217. this.m_dStageR = stageConditions.StageR;
  218. }
  219. }
  220. //电镜控制改变事件
  221. private void OnMicroscopeColumnChange(object sender, EventArgs e)
  222. {
  223. //MessageBox.Show("电镜控制改变ColumnChange");
  224. ReadMicroscopeColumn();
  225. }
  226. //样品台控制改变事件
  227. private void OnMicroscopeStageChange(object sender, EventArgs e)
  228. {
  229. //MessageBox.Show("样品控制改变StageChange");
  230. ReadStage();
  231. }
  232. //列控制连接或断开时的事件
  233. private void OnMicroscopeColumnConnected(object sender, EventArgs e)
  234. {
  235. //MessageBox.Show("电镜控制连接ColumnConnected");
  236. }
  237. //样品台控制连接或断开时的事件
  238. private void OnMicroscopeStageConnected(object sender, EventArgs e)
  239. {
  240. //MessageBox.Show("样品台连接StageConnected");
  241. ReadStage();
  242. }
  243. //样品台控制、电镜控制、外围控制的事件改变完成
  244. private void OnMicroscopeChangeCompleted(object sender, EventArgs e)
  245. {
  246. //MessageBox.Show("电镜控制完成ChangeCompleted");
  247. ReadMicroscopeColumn();
  248. ReadStage();
  249. }
  250. //电镜控制
  251. //电压
  252. //放大倍数
  253. //工作距离
  254. //亮度
  255. //对比度
  256. //BeamOn
  257. //FilamentOn
  258. //缩放
  259. public float GetMagnification()
  260. {
  261. return (float)m_dMagnification;
  262. }
  263. public Boolean SetMagnification(float set)
  264. {
  265. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  266. {
  267. { Column.Magnification, (double)set }
  268. };
  269. this.microscopeController.SetColumnConditions(columnDictionary);
  270. return true;
  271. }
  272. //焦距
  273. public float GetWorkingDistance()
  274. {
  275. return (float)m_dWorkingDistance;
  276. }
  277. public Boolean SetWorkingDistance(float set)
  278. {
  279. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  280. {
  281. { Column.WorkingDistance, (double)set }
  282. };
  283. this.microscopeController.SetColumnConditions(columnDictionary);
  284. return true;
  285. }
  286. //亮度
  287. public float GetBrightness()
  288. {
  289. return (float)m_dBirghtness;
  290. }
  291. public Boolean SetBrightness(float set)
  292. {
  293. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  294. {
  295. { Column.Brightness, (double)set }
  296. };
  297. this.microscopeController.SetColumnConditions(columnDictionary);
  298. return true;
  299. }
  300. //对比度
  301. public float GetContrast()
  302. {
  303. return (float)m_dContrast;
  304. }
  305. public Boolean SetContrast(float set)
  306. {
  307. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  308. {
  309. { Column.Contrast, (double)set }
  310. };
  311. this.microscopeController.SetColumnConditions(columnDictionary);
  312. return true;
  313. }
  314. //SEM电压
  315. public float GetSEMVoltage()
  316. {
  317. return (float)m_dHighVoltage;
  318. }
  319. public Boolean SetSEMVoltage(float set)
  320. {
  321. Dictionary<Column, double> columnDictionary = new Dictionary<Column, double>
  322. {
  323. { Column.HighVoltage, (double)set }
  324. };
  325. this.microscopeController.SetColumnConditions(columnDictionary);
  326. return true;
  327. }
  328. //样品台
  329. public float[] GetStagePosition()
  330. {
  331. float[] ret = new float[5];
  332. ret[0] = (float)m_dStageX;
  333. ret[1] = (float)m_dStageY;
  334. ret[2] = (float)m_dStageZ;
  335. ret[3] = (float)m_dStageT;
  336. ret[4] = (float)m_dStageR;
  337. return ret;
  338. }
  339. public Boolean SetStagePosition(float[] set)
  340. {
  341. double stageX = (double)set[0];
  342. if (stageX > StageXMax || stageX < StageXMin)
  343. {
  344. return false;
  345. }
  346. double stageY = (double)set[1];
  347. if (stageY > StageYMax || stageY < StageYMin)
  348. {
  349. return false;
  350. }
  351. double stageZ = (double)set[2];
  352. if (stageZ > StageZMax || stageZ < StageZMin)
  353. {
  354. return false;
  355. }
  356. double stageT = (double)set[3];
  357. if (stageT > StageTMax || stageT < StageTMin)
  358. {
  359. return false;
  360. }
  361. double stageR = (double)set[4];
  362. if (stageR > StageRMax || stageR < StageRMin)
  363. {
  364. return false;
  365. }
  366. var stageDictionary = new Dictionary<Stage, double>
  367. {
  368. { Stage.StageX, (double)stageX },
  369. { Stage.StageY, (double)stageY },
  370. { Stage.StageZ, (double)stageZ },
  371. { Stage.StageT, (double)stageT },
  372. { Stage.StageR, (double)stageR }
  373. };
  374. this.microscopeController.SetStageConditions(stageDictionary);
  375. return true;
  376. }
  377. public float GetStageAtX()
  378. {
  379. return (float)m_dStageX;
  380. }
  381. public float GetStageAtY()
  382. {
  383. return (float)m_dStageY;
  384. }
  385. public float GetStageAtZ()
  386. {
  387. return (float)m_dStageZ;
  388. }
  389. public float GetStageAtT()
  390. {
  391. return (float)m_dStageT;
  392. }
  393. public float GetStageAtR()
  394. {
  395. return (float)m_dStageR;
  396. }
  397. public Boolean SetStageGotoX(float set)
  398. {
  399. double stageX = (double)set;
  400. if (stageX > StageXMax || stageX < StageXMin)
  401. {
  402. return false;
  403. }
  404. var stageDictionary = new Dictionary<Stage, double>
  405. {
  406. { Stage.StageX, (double)stageX }
  407. };
  408. this.microscopeController.SetStageConditions(stageDictionary);
  409. return true;
  410. }
  411. public Boolean SetStageGotoY(float set)
  412. {
  413. double stageY = (double)set;
  414. if (stageY > StageYMax || stageY < StageYMin)
  415. {
  416. return false;
  417. }
  418. var stageDictionary = new Dictionary<Stage, double>
  419. {
  420. { Stage.StageY, (double)stageY }
  421. };
  422. this.microscopeController.SetStageConditions(stageDictionary);
  423. return true;
  424. }
  425. public Boolean SetStageGotoZ(float set)
  426. {
  427. double stageZ = (double)set;
  428. if (stageZ > StageZMax || stageZ < StageZMin)
  429. {
  430. return false;
  431. }
  432. var stageDictionary = new Dictionary<Stage, double>
  433. {
  434. { Stage.StageZ, (double)stageZ }
  435. };
  436. this.microscopeController.SetStageConditions(stageDictionary);
  437. return true;
  438. }
  439. public Boolean SetStageGotoT(float set)
  440. {
  441. double stageT = (double)set;
  442. if (stageT > StageTMax || stageT < StageTMin)
  443. {
  444. return false;
  445. }
  446. var stageDictionary = new Dictionary<Stage, double>
  447. {
  448. { Stage.StageT, (double)stageT }
  449. };
  450. this.microscopeController.SetStageConditions(stageDictionary);
  451. return true;
  452. }
  453. public Boolean SetStageGotoR(float set)
  454. {
  455. double stageR = (double)set;
  456. if (stageR > StageRMax || stageR < StageRMin)
  457. {
  458. return false;
  459. }
  460. var stageDictionary = new Dictionary<Stage, double>
  461. {
  462. { Stage.StageR, (double)stageR }
  463. };
  464. this.microscopeController.SetStageConditions(stageDictionary);
  465. return true;
  466. }
  467. public Boolean MoveStageXY(float x, float y)
  468. {
  469. double stageX = (double)x;
  470. if (stageX > StageXMax || stageX < StageXMin)
  471. {
  472. return false;
  473. }
  474. double stageY = (double)y;
  475. if (stageY > StageYMax || stageY < StageYMin)
  476. {
  477. return false;
  478. }
  479. var stageDictionary = new Dictionary<Stage, double>
  480. {
  481. { Stage.StageX, (double)stageX },
  482. { Stage.StageY, (double)stageY }
  483. };
  484. this.microscopeController.SetStageConditions(stageDictionary);
  485. return true;
  486. }
  487. #endregion
  488. #region 拍图
  489. /// <summary>
  490. /// IImageAcquisitionController object
  491. /// </summary>
  492. private IImageAcquisitionController imageAcquisitionController = null;
  493. /// <summary>
  494. /// IImageSettings object
  495. /// </summary>
  496. private IImageAcquisitionSettings imageAcquisitionSettings = null;
  497. //图像扫描尺寸
  498. public double[] ImageScanSize =
  499. {
  500. 32,
  501. 64,
  502. 128,
  503. 256,
  504. 512,
  505. 1024,
  506. 4096,
  507. 8192
  508. };
  509. private byte[] m_ImageBit = null;
  510. private long m_nImageWidth = 0;
  511. private long m_nImageHeight = 0;
  512. bool m_bAcquistionDone = false;
  513. private Bitmap m_Bitmap = null;
  514. void InitImageAcquisition()
  515. {
  516. imageAcquisitionController = AcquireFactory.CreateImageServer();
  517. imageAcquisitionSettings = AcquireFactory.CreateImageSettings();
  518. imageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  519. imageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  520. }
  521. //控制电镜释放
  522. void CloseImageAcquisition()
  523. {
  524. imageAcquisitionController.ExperimentStarted -= this.OnImageExperimentStarted;
  525. imageAcquisitionController.ExperimentFinished -= this.OnImageExperimentFinished;
  526. }
  527. /// <summary>
  528. /// OnImageExperimentStarted
  529. /// </summary>
  530. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  531. {
  532. }
  533. //int m_nState;
  534. /// <summary>
  535. /// OnImageExperimentFinished
  536. /// </summary>
  537. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  538. {
  539. IElectronImage electronImage = e.Value[0];
  540. if (!ReadImageData(electronImage, out m_ImageBit, out m_nImageWidth, out m_nImageHeight))
  541. {
  542. MessageBox.Show("图像采集完成,获取图像像素失败!");
  543. }
  544. if (m_ImageBit != null && m_ImageBit.Length == m_nImageWidth * m_nImageHeight)
  545. {
  546. m_bAcquistionDone = true;
  547. }
  548. }
  549. bool ReadImageData(IElectronImage a_electronImage, out Byte[] a_pImageBits, out long a_nImageHeight, out long a_nImageWidth)
  550. {
  551. a_nImageHeight = 0;
  552. a_nImageWidth = 0;
  553. a_pImageBits = null;
  554. if (a_electronImage == null)
  555. {
  556. return false;
  557. }
  558. a_nImageHeight = a_electronImage.Height;
  559. a_nImageWidth = a_electronImage.Width;
  560. int nBytesPerPixel = a_electronImage.BytesPerPixel;
  561. long nImageSize = a_nImageHeight * a_nImageWidth;
  562. long nBufferSize = nImageSize * nBytesPerPixel;
  563. Byte[] imageData = new Byte[nBufferSize];
  564. a_electronImage.GetData(imageData);
  565. a_pImageBits = new Byte[nImageSize];
  566. // default, oxford will return short image, we need to convert to byte
  567. if (nBytesPerPixel == 2)
  568. {
  569. int nBSEValue = 0;
  570. for (int i = 0; i < nImageSize; ++i)
  571. {
  572. nBSEValue = imageData[0 + i * nBytesPerPixel] + imageData[1 + i * nBytesPerPixel] * 255;
  573. a_pImageBits[i] = (Byte)(nBSEValue / 128.0 + 0.5);
  574. }
  575. }
  576. else
  577. {
  578. string msg = string.Format("image byte per pixel other than 2({0}), image convert may wrong", nBytesPerPixel);
  579. MessageBox.Show(msg);
  580. int nOffset = nBytesPerPixel - 1;
  581. for (int i = 0; i < nImageSize; ++i)
  582. {
  583. a_pImageBits[i] = imageData[nOffset + i * nBytesPerPixel];
  584. }
  585. }
  586. return true;
  587. }
  588. void AcquisitionParamInit()
  589. {
  590. m_ImageBit = null;
  591. m_nImageWidth = 0;
  592. m_nImageHeight = 0;
  593. m_bAcquistionDone = false;
  594. Bitmap m_Bitmap = null;
  595. }
  596. //a_dDwellTime : 1~100000之间的数
  597. //a_sImageType : 1: SE, 2: Bse
  598. //a_dImageScanSize : 图像分辨率,图像的高
  599. public bool SetImageAcquistionSetting(double a_dDwellTime, int a_nImageType, double a_dImageScanSize)
  600. {
  601. IImageSettings imageSettings = imageAcquisitionSettings.ImageSettings;
  602. IImageCapabilities imageCapabilities = imageAcquisitionSettings.ImageCapabilities;
  603. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  604. if (a_dDwellTime > imageCapabilities.MaximumImageDwellMicroseconds)
  605. {
  606. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  607. }
  608. if (a_dDwellTime < imageCapabilities.MinimumImageDwellMicroseconds)
  609. {
  610. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  611. }
  612. else
  613. {
  614. imageSettings.DwellTimeMicroSeconds = a_dDwellTime;
  615. }
  616. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  617. imageSettings.EnableInputSource((ImageInputSources)a_nImageType, true);
  618. if (!ImageScanSize.Contains(a_dImageScanSize))
  619. {
  620. MessageBox.Show("图像尺寸输入无效");
  621. return false;
  622. }
  623. var pixelSize = 1d / a_dImageScanSize;
  624. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  625. return true;
  626. }
  627. ////拍图
  628. public Boolean GrabImage(String filename, short xoff, short yoff, short width, short height, short type)
  629. {
  630. AcquisitionParamInit();
  631. try
  632. {
  633. IEnumerable<IElectronImage> images = imageAcquisitionController.StartAcquisition(imageAcquisitionSettings);
  634. images.ToList().ForEach(i =>
  635. {
  636. i.Label = string.Format(@"Code example image - {0:HH:mm:ss} - {1}", DateTime.Now, i.InputSource.ToString());
  637. });
  638. while (true)
  639. {
  640. if (m_bAcquistionDone)
  641. {
  642. // 图像对象
  643. m_Bitmap = ToGrayBitmap(m_ImageBit, (int)m_nImageHeight, (int)m_nImageWidth);
  644. switch (Path.GetExtension(filename))
  645. {
  646. case ".bmp":
  647. m_Bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp);
  648. break;
  649. case ".jpg":
  650. m_Bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
  651. break;
  652. case ".gif":
  653. m_Bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Gif);
  654. break;
  655. case ".tif":
  656. m_Bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Tiff);
  657. break;
  658. case ".png":
  659. m_Bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
  660. break;
  661. default:
  662. break;
  663. }
  664. break;
  665. }
  666. }
  667. }
  668. catch (InvalidSettingsException settingsException)
  669. {
  670. var sb = new StringBuilder(@"Invalid settings have been supplied: ");
  671. sb.AppendLine();
  672. settingsException.ValidationResults.ValidationErrors.ToList().ForEach(ve => sb.AppendFormat("{0}{1}", Environment.NewLine, ve));
  673. MessageBox.Show(sb.ToString());
  674. }
  675. catch (AcquisitionStartException startException)
  676. {
  677. string msg = string.Format(@"AcquisitionStartException: {0}", startException.Message);
  678. MessageBox.Show(msg);
  679. }
  680. return true;
  681. }
  682. /// <summary>
  683. /// 将一个byte的数组转换为8bit灰度位图
  684. /// </summary>
  685. /// <param name="data">数组</param>
  686. /// <param name="width">图像宽度</param>
  687. /// <param name="height">图像高度</param>
  688. /// <returns>位图</returns>
  689. Bitmap ToGrayBitmap(byte[] data, int width, int height)
  690. {
  691. // 申请目标位图的变量,并将其内存区域锁定
  692. Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
  693. //BitmapData这部分内容 需要 using System.Drawing.Imaging;
  694. BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
  695. ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
  696. // 获取图像参数
  697. // 扫描线的宽度
  698. int stride = bmpData.Stride;
  699. // 显示宽度与扫描线宽度的间隙
  700. int offset = stride - width;
  701. // 获取bmpData的内存起始位置
  702. IntPtr iptr = bmpData.Scan0;
  703. // 用stride宽度,表示这是内存区域的大小
  704. int scanBytes = stride * height;
  705. // 下面把原始的显示大小字节数组转换为内存中实际存放的字节数组
  706. int posScan = 0;
  707. int posReal = 0;// 分别设置两个位置指针,指向源数组和目标数组
  708. byte[] pixelValues = new byte[scanBytes]; //为目标数组分配内存
  709. for (int x = 0; x < height; x++)
  710. {
  711. int startIndex = x * width;
  712. //// 下面的循环节是模拟行扫描
  713. for (int y = 0; y < width; y++)
  714. {
  715. pixelValues[posScan++] = data[posReal++];
  716. }
  717. posScan += offset; //行扫描结束,要将目标位置指针移过那段“间隙”
  718. }
  719. // 用Marshal的Copy方法,将刚才得到的内存字节数组复制到BitmapData中
  720. System.Runtime.InteropServices.Marshal.Copy(pixelValues, 0, iptr, scanBytes);
  721. bmp.UnlockBits(bmpData); // 解锁内存区域
  722. // 下面的代码是为了修改生成位图的索引表,从伪彩修改为灰度
  723. ColorPalette tempPalette;
  724. using (Bitmap tempBmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
  725. {
  726. tempPalette = tempBmp.Palette;
  727. }
  728. for (int i = 0; i < 256; i++)
  729. {
  730. tempPalette.Entries[i] = Color.FromArgb(i, i, i);
  731. }
  732. bmp.Palette = tempPalette;
  733. return bmp;
  734. }
  735. //获取分辨率
  736. public int[] GetImageStore()
  737. {
  738. int[] ImageStore = new int[8];
  739. for (int i = 0; i < 8; i++)
  740. {
  741. ImageStore[i] = (int)ImageScanSize[i];
  742. }
  743. return ImageStore;
  744. }
  745. //设置分辨率
  746. public Boolean SetImageStore(float set)
  747. {
  748. IImageScanSettings scanSettings = imageAcquisitionSettings.ScanSettings;
  749. if (!ImageScanSize.Contains(set))
  750. {
  751. MessageBox.Show("图像尺寸输入无效");
  752. return false;
  753. }
  754. var pixelSize = 1d / (double)set;
  755. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  756. return true;
  757. }
  758. public Bitmap GetBitmap()
  759. {
  760. return m_Bitmap;
  761. }
  762. #endregion
  763. #region X-ray
  764. //控制器
  765. private IEdSpectrumAcquisitionController EdSpectrumAcquisitionController = null;
  766. private IEdSpectrumSettings EdSpectrumSettings = null;
  767. private IEdSpectrumProcessing EdSpectrumProcessing = null;
  768. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  769. private IAutoIdSettings autoIdSettings = null;
  770. private ISEMQuantSettings settings = null;
  771. private int XRayChannelLength = 2000;
  772. private long[] m_XrayData = null;
  773. private bool m_bXrayDone = false;
  774. /// <summary>
  775. /// List of EdSpectrum objects
  776. /// </summary>
  777. private Dictionary<string, double> listElement = null;
  778. void InitXrayAcquistion()
  779. {
  780. EdSpectrumSettings = AcquireFactory.CreateEdSpectrumSettings();
  781. EdSpectrumAcquisitionController = AcquireFactory.CreateEdSpectrumServer();
  782. EdSpectrumProcessing = ProcessingFactory.CreateSpectrumProcessing();
  783. // Use the autoIdSettings to define elements that are known or elements that you want to exclude. They also list elements that cannot be identified
  784. autoIdSettings = ProcessingFactory.CreateAutoIdSettings();
  785. settings = ProcessingFactory.CreateSEMQuantSettings();
  786. EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  787. //EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  788. }
  789. void CloaseXrayAcquistion()
  790. {
  791. EdSpectrumAcquisitionController.ExperimentFinished -= this.OnEdSpectrumExperimentFinished;
  792. //EdSpectrumAcquisitionController.ExperimentStarted -= this.OnEdSpectrumExperimentStarted;
  793. }
  794. void SetXrayAcquisitionParam(double a_dMilliSecondsTime)
  795. {
  796. EdSpectrumSettings.EdSettings.AcquisitionMode = EdAcquireMode.LiveTime; // RealTime or LiveTime
  797. if (a_dMilliSecondsTime < 200)
  798. {
  799. a_dMilliSecondsTime = 200;
  800. }
  801. EdSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromMilliseconds(a_dMilliSecondsTime);
  802. EdSpectrumSettings.EdSettings.ProcessTime = 1;
  803. EdSpectrumSettings.EdSettings.EnergyRange = 20;
  804. EdSpectrumSettings.EdSettings.NumberOfChannels = 4096;
  805. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateFullFieldRegion(1.0 / 1024.0);
  806. }
  807. void SetPointAcquistionRegion(int a_nX, int a_nY)
  808. {
  809. Chord chord = new Chord(a_nX, a_nY, 1);
  810. List < Chord > chords = new List < Chord> ();
  811. chords.Add(chord);
  812. ChordList chordsList = null;
  813. if (m_nImageWidth != 0)
  814. {
  815. chordsList = new ChordList(chords, 1 / (double)m_nImageWidth);
  816. }
  817. else
  818. {
  819. chordsList = new ChordList(chords, 1 / 1024.0);
  820. }
  821. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  822. }
  823. void SetAreaAcquistionRegion(List<Chord> a_nChords)
  824. {
  825. ChordList chordsList = null;
  826. if (m_nImageWidth != 0)
  827. {
  828. chordsList = new ChordList(a_nChords, 1 / (double)m_nImageWidth);
  829. }
  830. else
  831. {
  832. chordsList = new ChordList(a_nChords, 1 / 1024.0);
  833. }
  834. EdSpectrumSettings.ScanSettings.AcquisitionRegion.CreateChordListRegion(chordsList);
  835. }
  836. void XrayParamInit()
  837. {
  838. m_XrayData = null;
  839. m_bXrayDone = false;
  840. }
  841. /// <summary>
  842. /// Called when IEdSpectrumAcquisitionController Experiment Starting.
  843. /// </summary>
  844. /// <param name="sender">The sender.</param>
  845. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  846. private void OnEdSpectrumExperimentStarting(object sender, AcquisitionStartingEventArgs<IEdSpectrum> e)
  847. {
  848. }
  849. /// <summary>
  850. /// Called when IEdSpectrumAcquisitionController Experiment Started.
  851. /// </summary>
  852. /// <param name="sender">The sender.</param>
  853. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  854. private void OnEdSpectrumExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdSpectrum> e)
  855. {
  856. }
  857. /// <summary>
  858. /// Called when IEdSpectrumAcquisitionController Experiment Finished
  859. /// </summary>
  860. /// <param name="sender">sender object</param>
  861. /// <param name="e">The instance containing the event data.</param>
  862. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  863. {
  864. IEdSpectrumAcquisitionController edSpectrumAcquisitionController = sender as IEdSpectrumAcquisitionController;
  865. if (edSpectrumAcquisitionController != null)
  866. {
  867. // This only used for multiple acquisition:
  868. // If this is the last experiment, call EndMultipleAcquisition on the Acquisition
  869. // controller, to re-enable external scan switching
  870. edSpectrumAcquisitionController.EndMultipleAcquisition();
  871. }
  872. IEdSpectrum edSpectrum = e.Value;
  873. if (!ReadXrayData(edSpectrum, out m_XrayData, XRayChannelLength))
  874. {
  875. MessageBox.Show("Xray采集完成,获取图像像素失败!");
  876. }
  877. //Quantify processing
  878. EdSpectrumProcessing.IdentifyElements(e.Value, autoIdSettings);
  879. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  880. settings.CombinedElement = 8;
  881. settings.Normalised = true;
  882. ISEMQuantStatus quantStatus = EdSpectrumProcessing.SEMQuantifySpectrum(e.Value, settings);//(a_nChannelData, OIHelper::SEMQuantSettings);
  883. IEnumerable < ISEMQuantResult > Results = quantStatus.Results;
  884. //Get element result for single point
  885. var ie = Results.GetEnumerator();
  886. listElement = new Dictionary<string, double>();
  887. while (ie.MoveNext())
  888. {
  889. ISEMQuantResult result = ie.Current;
  890. if (result.WeightPercent != 0)
  891. {
  892. listElement.Add(ElementProperties.GetElementSymbol(result.AtomicNumber), result.WeightPercent );
  893. }
  894. }
  895. if (m_XrayData != null && m_XrayData.Length == XRayChannelLength)
  896. {
  897. m_bXrayDone = true;
  898. }
  899. }
  900. bool ReadXrayData(IEdSpectrum a_spectrum, out long[] a_pSpectrumData, int a_nBufferSize)
  901. {
  902. a_pSpectrumData = new long[a_nBufferSize];
  903. int[] xrayData = new int[a_spectrum.NumberOfChannels];
  904. a_spectrum.GetChannelData(xrayData);
  905. double dZeroChannelValue = a_spectrum.ZeroChannelValue;
  906. int nChannelStart = 0;
  907. if (dZeroChannelValue < 0) // zero channel value should less than zero
  908. {
  909. nChannelStart = (int)(-dZeroChannelValue / a_spectrum.ChannelWidth + 0.5);
  910. }
  911. int nDataLength = (int)(a_spectrum.EnergyRange * 1000 / a_spectrum.ChannelWidth + 0.5);
  912. double dStep1 = 1.0 / nDataLength;
  913. double dStep2 = 1.0 / a_nBufferSize;
  914. for (int i = 0; i < nDataLength; ++i)
  915. {
  916. int nValue = xrayData[i + nChannelStart] > 0 ? xrayData[i + nChannelStart] : 0;
  917. double dBinPos = i * dStep1;
  918. long nLeftBin = (long)(dBinPos / dStep2);
  919. // calculate % into left bin
  920. double dLeft_Percent = (double)(nLeftBin + 1) - dBinPos / dStep2; // ((nLeftBin + 1)*dStep2 - dBinPos)/dStep2
  921. // calculate data into the left bin
  922. long nValueToLeftBin = (long)((double)nValue * dLeft_Percent + 0.5);
  923. // put data into bins
  924. a_pSpectrumData[nLeftBin] += nValueToLeftBin;
  925. if ((nLeftBin + 1) < (long)a_nBufferSize)
  926. {
  927. a_pSpectrumData[nLeftBin + 1] += (nValue - nValueToLeftBin);
  928. }
  929. }
  930. return true;
  931. }
  932. /// <summary>
  933. /// Called when IEdSpectrum DataChanged
  934. /// </summary>
  935. /// <param name="sender">Event source</param>
  936. /// <param name="e">Event arguments</param>
  937. private void OnDataChanged(object sender, EventArgs e)
  938. {
  939. }
  940. public bool IsAcquiringSpectrum()
  941. {
  942. return EdSpectrumAcquisitionController.IsAcquiring;
  943. }
  944. //点扫描
  945. //X-ray
  946. public Boolean XrayPointCollectiong(double dMilliSecondsTime, int x, int y, out long[] XrayData, out Dictionary<string, double> a_listElement)
  947. {
  948. XrayParamInit();
  949. XrayData = new long[XRayChannelLength];
  950. a_listElement = new Dictionary<string, double>();
  951. SetXrayAcquisitionParam(dMilliSecondsTime);
  952. SetPointAcquistionRegion(x, y);
  953. // IEdSpectrumSettings.EdCapabilities will validate settings and hardware availability.
  954. if (EdSpectrumAcquisitionController.IsEdHardwareReady(EdSpectrumSettings))
  955. {
  956. // This only used for multiple acquisition:
  957. // if this is the first acquisition, call BeginMultipleAcquisition
  958. // on the IEdSpectrumAcquisitionController to suppress external scan switching
  959. if (m_bXrayDone)
  960. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  961. // Start spectrum acquisition
  962. try
  963. {
  964. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  965. edSpectrum.Label = string.Format(@"Point({0},{1})",x,y);
  966. while (true)
  967. {
  968. if (m_bXrayDone)
  969. {
  970. XrayData = m_XrayData;
  971. a_listElement =listElement;
  972. break;
  973. }
  974. }
  975. }
  976. catch (InvalidSettingsException invalidSettingsException)
  977. {
  978. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  979. invalidSettingsException.Message,
  980. invalidSettingsException.ValidationResults.ValidationErrors);
  981. MessageBox.Show(msg);
  982. }
  983. catch (AcquisitionStartException acquisitionStartException)
  984. {
  985. string msg = string.Format(@"Acquisition Start Exception:{0}",acquisitionStartException.Message);
  986. MessageBox.Show(msg);
  987. }
  988. }
  989. return true;
  990. }
  991. //面扫描
  992. public Boolean XrayAreaCollectiong(double dMilliSecondsTime, List<Segment> a_listChord, out long[] XrayData, out Dictionary<string, double> a_listElement)
  993. {
  994. XrayParamInit();
  995. XrayData = new long[XRayChannelLength];
  996. a_listElement = new Dictionary<string, double>();
  997. SetXrayAcquisitionParam(dMilliSecondsTime);
  998. List<Chord> Chords = new List<Chord>();
  999. foreach (Segment seg in a_listChord)
  1000. {
  1001. Chord chord = new Chord(seg.X, seg.Y, seg.Length);
  1002. Chords.Add(chord);
  1003. }
  1004. SetAreaAcquistionRegion(Chords);
  1005. // IEdSpectrumSettings.EdCapabilities will validate settings and hardware availability.
  1006. if (EdSpectrumAcquisitionController.IsEdHardwareReady(EdSpectrumSettings))
  1007. {
  1008. // This only used for multiple acquisition:
  1009. // if this is the first acquisition, call BeginMultipleAcquisition
  1010. // on the IEdSpectrumAcquisitionController to suppress external scan switching
  1011. if (m_bXrayDone)
  1012. EdSpectrumAcquisitionController.BeginMultipleAcquisition();
  1013. // Start spectrum acquisition
  1014. try
  1015. {
  1016. IEdSpectrum edSpectrum = EdSpectrumAcquisitionController.StartAcquisition(EdSpectrumSettings);
  1017. edSpectrum.Label = string.Format(@"chord");
  1018. while (true)
  1019. {
  1020. if (m_bXrayDone)
  1021. {
  1022. XrayData = m_XrayData;
  1023. a_listElement = listElement;
  1024. break;
  1025. }
  1026. }
  1027. }
  1028. catch (InvalidSettingsException invalidSettingsException)
  1029. {
  1030. string msg = string.Format(@"Invalid Settings Exception:{0}, {1}",
  1031. invalidSettingsException.Message,
  1032. invalidSettingsException.ValidationResults.ValidationErrors);
  1033. MessageBox.Show(msg);
  1034. }
  1035. catch (AcquisitionStartException acquisitionStartException)
  1036. {
  1037. string msg = string.Format(@"Acquisition Start Exception:{0}", acquisitionStartException.Message);
  1038. MessageBox.Show(msg);
  1039. }
  1040. }
  1041. return true;
  1042. }
  1043. #endregion
  1044. }
  1045. }