ImageCollectionMenu.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using PaintDotNet.Base.CommTool;
  4. using PaintDotNet.Camera;
  5. using PaintDotNet.Camera;
  6. using PaintDotNet.Data.Param;
  7. using PaintDotNet.ImageCollect;
  8. using PaintDotNet.ImageCollect.CameraEDOF;
  9. using PaintDotNet.Preview2;
  10. using PaintDotNet.Setting;
  11. using PaintDotNet.Setting.LabelComponent;
  12. using StageController;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.Globalization;
  18. using System.Linq;
  19. using System.Runtime.InteropServices;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows.Forms;
  23. namespace PaintDotNet.Menus
  24. {
  25. public delegate void UpdateCameraList(List<ICamera> cameras);
  26. /// <summary>
  27. /// 图像采集菜单
  28. /// </summary>
  29. internal sealed class ImageCollectionMenu : PdnMenuItem
  30. {
  31. public int index = 0;
  32. private PdnMenuItem menuCameraSelection;
  33. private PdnMenuItem menuCameraSetting;
  34. private PdnMenuItem menuCameraAdjust;
  35. private PdnMenuItem menuCameraFrame;
  36. private PdnMenuItem menuCameraConventional;
  37. private ToolStripSeparator menuSeparator1;
  38. private PdnMenuItem menuImageCollectHdr;
  39. private ToolStripSeparator menuSeparator2;
  40. private PdnMenuItem menuPreviewAction;
  41. private PdnMenuItem menuPreview2Action;
  42. private PdnMenuItem menuPreviewActionClose;
  43. private PdnMenuItem menuShootAction;
  44. private ToolStripSeparator menuSeparator3;
  45. private PdnMenuItem menuAutofocus;
  46. private PdnMenuItem menuRealTimeDepth;
  47. private PdnMenuItem menuRealTimeStitching;
  48. private PdnMenuItem menuAutomaticDepth;
  49. private PdnMenuItem menuImageStitching;
  50. private PdnMenuItem menuTimedShooting;
  51. private PdnMenuItem menuVideoRecording;
  52. private PdnMenuItem menuZaxisScan;
  53. private PdnMenuItem menuMultiDimensional;
  54. private PdnMenuItem menuListOfLocations;
  55. private PdnMenuItem menuTwainSelection;
  56. private PdnMenuItem menuTwainOpen;
  57. // 曝光
  58. private PdnMenuItem menuAdjustExposure;
  59. // 白平衡
  60. private PdnMenuItem menuAdjustWhiteBalance;
  61. // 增益
  62. private PdnMenuItem menuAdjustGain;
  63. // 饱和度
  64. private PdnMenuItem menuAdjustSaturation;
  65. // 相机模式
  66. private PdnMenuItem menuFrameColorMode;
  67. // 相机分辨率
  68. private PdnMenuItem menuResolution;
  69. // ROI
  70. private PdnMenuItem menuROI;
  71. // 图像旋转
  72. private PdnMenuItem menuCommonRotate;
  73. public ImageCollectionMenu(int menuId)
  74. {
  75. InitializeComponent();
  76. this.MenuId = menuId;
  77. }
  78. protected override void OnAppWorkspaceChanged()
  79. {
  80. base.OnAppWorkspaceChanged();
  81. }
  82. private void InitializeComponent()
  83. {
  84. //相机选择
  85. this.menuCameraSelection = new PdnMenuItem(ActionType.CameraSelection);
  86. this.menuCameraSelection.AutomaticScript = false;
  87. //相机设置
  88. this.menuCameraSetting = new PdnMenuItem(ActionType.CameraSetting);
  89. this.menuCameraSetting.AutomaticScript = false;
  90. //调节
  91. this.menuCameraAdjust = new PdnMenuItem(ActionType.CameraAdjust);
  92. this.menuCameraAdjust.AutomaticScript = false;
  93. //边框
  94. this.menuCameraFrame = new PdnMenuItem(ActionType.CameraFrame);
  95. this.menuCameraFrame.AutomaticScript = false;
  96. //常规
  97. this.menuCameraConventional = new PdnMenuItem(ActionType.CameraConventional);
  98. this.menuCameraConventional.AutomaticScript = false;
  99. //分隔线
  100. this.menuSeparator1 = new ToolStripSeparator();
  101. //HDR
  102. this.menuImageCollectHdr = new PdnMenuItem(ActionType.ImageCollectHdr);
  103. this.menuImageCollectHdr.AutomaticScript = false;
  104. this.menuImageCollectHdr.Checked = CameraConfigs.GetInstance().GetCurrentCameraParamModel().parame.HDR == 1;
  105. //分隔线
  106. this.menuSeparator2 = new ToolStripSeparator();
  107. //预览
  108. this.menuPreviewAction = new PdnMenuItem(ActionType.PreviewExtraction);
  109. this.menuPreviewAction.AutomaticScript = false;
  110. //预览
  111. this.menuPreview2Action = new PdnMenuItem(ActionType.Preview2);
  112. this.menuPreview2Action.AutomaticScript = false;
  113. this.menuPreview2Action.Image = PdnResources.GetImageResource("Icons.Preview2.png").Reference;
  114. //关闭预览
  115. this.menuPreviewActionClose = new PdnMenuItem(ActionType.PreviewExtractionClose);
  116. this.menuPreviewActionClose.AutomaticScript = false;
  117. //拍摄
  118. this.menuShootAction = new PdnMenuItem(ActionType.ShootAction);
  119. this.menuShootAction.AutomaticScript = false;
  120. //分隔线
  121. this.menuSeparator3 = new ToolStripSeparator();
  122. //自动聚焦
  123. this.menuAutofocus = new PdnMenuItem(ActionType.Autofocus);
  124. this.menuAutofocus.AutomaticScript = false;
  125. //实时景深扩展
  126. this.menuRealTimeDepth = new PdnMenuItem(ActionType.RealTimeDepth);
  127. this.menuRealTimeDepth.AutomaticScript = false;
  128. //实时图像拼接
  129. this.menuRealTimeStitching = new PdnMenuItem(ActionType.RealTimeStitching);
  130. this.menuRealTimeStitching.AutomaticScript = false;
  131. //自动景深扩展
  132. this.menuAutomaticDepth = new PdnMenuItem(ActionType.AutomaticDepth);
  133. this.menuAutomaticDepth.AutomaticScript = false;
  134. //图像拼接
  135. this.menuImageStitching = new PdnMenuItem(ActionType.ImageStitching);
  136. this.menuImageStitching.AutomaticScript = false;
  137. //实时拍摄
  138. this.menuTimedShooting = new PdnMenuItem(ActionType.TimedShooting);
  139. this.menuTimedShooting.AutomaticScript = false;
  140. //视频录像
  141. this.menuVideoRecording = new PdnMenuItem(ActionType.VideoRecording);
  142. this.menuVideoRecording.AutomaticScript = false;
  143. //z轴扫描
  144. this.menuZaxisScan = new PdnMenuItem(ActionType.ZaxisScan);
  145. this.menuZaxisScan.AutomaticScript = false;
  146. //多维图像合成
  147. this.menuMultiDimensional = new PdnMenuItem(ActionType.MultiDimensional);
  148. this.menuMultiDimensional.AutomaticScript = false;
  149. //位置列表
  150. this.menuListOfLocations = new PdnMenuItem(ActionType.ListOfLocations);
  151. this.menuListOfLocations.AutomaticScript = false;
  152. // 曝光
  153. this.menuAdjustExposure = new PdnMenuItem(ActionType.AdjustExposure);
  154. this.menuAdjustExposure.AutomaticScript = false;
  155. // 白平衡
  156. this.menuAdjustWhiteBalance = new PdnMenuItem(ActionType.AdjustWhiteBalance);
  157. this.menuAdjustWhiteBalance.AutomaticScript = false;
  158. // 增益
  159. this.menuAdjustGain = new PdnMenuItem(ActionType.AdjustGain);
  160. this.menuAdjustGain.AutomaticScript = false;
  161. // 饱和度
  162. this.menuAdjustSaturation = new PdnMenuItem(ActionType.AdjustSaturation);
  163. this.menuAdjustSaturation.AutomaticScript = false;
  164. // 相机模式
  165. this.menuFrameColorMode = new PdnMenuItem(ActionType.FrameCameraMode);
  166. this.menuFrameColorMode.AutomaticScript = false;
  167. // 相机设置
  168. this.menuResolution = new PdnMenuItem(ActionType.FrameCameraSet1);
  169. this.menuResolution.AutomaticScript = false;
  170. // 相机设置
  171. this.menuROI = new PdnMenuItem(ActionType.FrameCameraSet2);
  172. this.menuROI.AutomaticScript = false;
  173. // 图像旋转
  174. this.menuCommonRotate = new PdnMenuItem(ActionType.CommonRotate);
  175. this.menuCommonRotate.AutomaticScript = false;
  176. //Twain
  177. this.menuTwainSelection = new PdnMenuItem(ActionType.TwainSelect);
  178. this.menuTwainSelection.AutomaticScript = false;
  179. this.menuTwainSelection.Image = PdnResources.GetImageResource("Icons.TwainSelect.png").Reference;
  180. //Twain
  181. this.menuTwainOpen = new PdnMenuItem(ActionType.TwainOpen);
  182. this.menuTwainOpen.AutomaticScript = false;
  183. this.menuTwainOpen.Image = PdnResources.GetImageResource("Icons.TwainOpen.png").Reference;
  184. //
  185. // 主菜单
  186. //
  187. this.DropDownItems.AddRange(new ToolStripItem[] {
  188. this.menuCameraSelection,
  189. this.menuTwainSelection,
  190. this.menuTwainOpen,
  191. this.menuCameraSetting,
  192. this.menuCameraAdjust,
  193. this.menuCameraFrame,
  194. this.menuCameraConventional,
  195. this.menuSeparator1,
  196. this.menuImageCollectHdr,
  197. //this.menuSeparator2,
  198. this.menuPreviewAction,
  199. this.menuPreviewActionClose,
  200. this.menuPreview2Action,
  201. this.menuShootAction,
  202. this.menuSeparator3,
  203. this.menuAutofocus,
  204. this.menuRealTimeDepth,
  205. this.menuRealTimeStitching,
  206. this.menuAutomaticDepth,
  207. this.menuImageStitching,
  208. this.menuTimedShooting,
  209. this.menuVideoRecording,
  210. this.menuZaxisScan,
  211. this.menuMultiDimensional,
  212. this.menuListOfLocations
  213. });
  214. this.Name = "Menu.ImageCollection";
  215. this.Text = PdnResources.GetString("Menu.ImageCollection.Text");
  216. //
  217. // 相机设置
  218. //
  219. this.menuCameraSetting.Click += new System.EventHandler(this.CameraSeting_Click);
  220. //调节
  221. this.menuCameraAdjust.Click += null;
  222. this.menuCameraAdjust.DropDownItems.AddRange(new ToolStripItem[] {
  223. this.menuAdjustExposure,
  224. this.menuAdjustWhiteBalance,
  225. this.menuAdjustGain,
  226. this.menuAdjustSaturation
  227. });
  228. //
  229. // 调节 - 曝光时间
  230. //
  231. this.menuAdjustExposure.NeedWaitKey = true;
  232. this.menuAdjustExposure.Click += new System.EventHandler(this.AdjustExposure_Click);
  233. this.menuAdjustExposure.Text = PdnResources.GetString("Menu.timeofexposure.text");
  234. this.menuAdjustExposure.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionExposureTimeIcon.png").Reference;
  235. //
  236. // 调节 - 白平衡
  237. //
  238. this.menuAdjustWhiteBalance.NeedWaitKey = true;
  239. this.menuAdjustWhiteBalance.Click += new System.EventHandler(this.AdjustWhiteBalance_Click);
  240. this.menuAdjustWhiteBalance.Text = PdnResources.GetString("Menu.Image.WhiteBalance.Text");
  241. this.menuAdjustWhiteBalance.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionWhiteBalanceIcon.png").Reference;
  242. //
  243. // 调节 - 增益
  244. //
  245. this.menuAdjustGain.NeedWaitKey = true;
  246. this.menuAdjustGain.Click += new System.EventHandler(this.AdjustGain_Click);
  247. this.menuAdjustGain.Text = PdnResources.GetString("Menu.imagecapture.adjust.Gain.text");
  248. this.menuAdjustGain.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionGainIcon.png").Reference;
  249. //
  250. // 调节 - 饱和度
  251. //
  252. this.menuAdjustSaturation.NeedWaitKey = true;
  253. this.menuAdjustSaturation.Click += new System.EventHandler(this.AdjustSaturation_Click);
  254. this.menuAdjustSaturation.Text = PdnResources.GetString("Menu.saturation.text");
  255. this.menuAdjustSaturation.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionSaturationIcon.png").Reference;
  256. //
  257. // 边框
  258. //
  259. this.menuCameraFrame.Click += null;
  260. this.menuCameraFrame.DropDownItems.AddRange(new ToolStripItem[] {
  261. this.menuFrameColorMode,
  262. this.menuResolution,
  263. this.menuROI
  264. });
  265. //
  266. // 黑白、彩色 - 相机模式
  267. //
  268. this.menuFrameColorMode.NeedWaitKey = true;
  269. this.menuFrameColorMode.Click += new System.EventHandler(this.Color_Click);
  270. this.menuFrameColorMode.Text = PdnResources.GetString("Menu.Cameramode.text");
  271. this.menuFrameColorMode.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionFrameCameraModeIcon.png").Reference;
  272. //
  273. // 边框 - 相机分辨率
  274. //
  275. this.menuResolution.NeedWaitKey = true;
  276. this.menuResolution.Click += new System.EventHandler(this.Resolution_Click);
  277. this.menuResolution.Text = PdnResources.GetString("ImageCollectionMenu.Resolution");
  278. this.menuResolution.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionFrameCameraSetIcon.png").Reference;
  279. //
  280. // 边框 - ROI
  281. //
  282. this.menuROI.NeedWaitKey = true;
  283. this.menuROI.Click += new System.EventHandler(this.ROI_Click);
  284. this.menuROI.Text = "ROI";
  285. this.menuROI.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionROISetIcon.png").Reference;
  286. //
  287. // 常规
  288. //
  289. this.menuCameraConventional.Click += null;
  290. this.menuCameraConventional.DropDownItems.AddRange(new ToolStripItem[] {
  291. this.menuCommonRotate,
  292. });
  293. //
  294. // 边框 - 相机设置
  295. //
  296. this.menuCommonRotate.NeedWaitKey = true;
  297. this.menuCommonRotate.Click += new System.EventHandler(this.CommonRotate_Click);
  298. this.menuCommonRotate.Text = PdnResources.GetString("Menu.Imagerotation.text");
  299. this.menuCommonRotate.Image = PdnResources.GetImageResource("Icons.MenuImageCollectionCommonRotateIcon.png").Reference;
  300. //
  301. //Twain
  302. //
  303. this.menuTwainSelection.NeedWaitKey = true;
  304. this.menuTwainSelection.Click += MenuTwainSelection_Click;
  305. this.menuTwainSelection.Text = PdnResources.GetString("Menu.LabelAction.LabelSelect.Text") + "TWAIN";
  306. menuTwainSelection.ToolTipText = PdnResources.GetString("Menu.ImageCollection.ClickRefreshList.text");
  307. //
  308. //Twain
  309. //
  310. this.menuTwainOpen.NeedWaitKey = true;
  311. this.menuTwainOpen.Click += MenuTwainOpen_Click; ;
  312. this.menuTwainOpen.Text = PdnResources.GetString("CommonAction.Open") + "TWAIN";
  313. //
  314. // HDR
  315. //
  316. this.menuImageCollectHdr.Click += new System.EventHandler(this.HDR_Click);
  317. //
  318. // 预览窗口
  319. //
  320. this.menuPreviewAction.NeedWaitKey = true;
  321. this.menuPreviewAction.Click += new System.EventHandler(this.previewExtraction_Click);
  322. //
  323. // 预览窗口
  324. //
  325. this.menuPreview2Action.NeedWaitKey = true;
  326. this.menuPreview2Action.Click += new System.EventHandler(this.preview2_Click);
  327. //
  328. // 关闭预览
  329. //
  330. this.menuPreviewActionClose.NeedWaitKey = true;
  331. this.menuPreviewActionClose.Click += new System.EventHandler(this.PreviewActionClose_Click);
  332. //
  333. // 拍摄
  334. //
  335. this.menuShootAction.NeedWaitKey = true;
  336. this.menuShootAction.Click += new System.EventHandler(this.Shoot_Click);
  337. //
  338. // 自动聚焦
  339. //
  340. this.menuAutofocus.NeedWaitKey = true;
  341. this.menuAutofocus.Click += new System.EventHandler(this.Autofocus_Click);
  342. //
  343. // 实时景深扩展
  344. //
  345. this.menuRealTimeDepth.Click += new System.EventHandler(this.CameraRealTimeDepth_Click);
  346. //
  347. // 实时图像拼接
  348. //
  349. this.menuRealTimeStitching.Click += new System.EventHandler(this.CameraRealTimeStitching_Click);
  350. //
  351. // 自动景深扩展
  352. //
  353. this.menuAutomaticDepth.Click += new System.EventHandler(this.CameraAutomaticDepth_Click);
  354. //
  355. // 图像拼接
  356. //
  357. this.menuImageStitching.Click += new System.EventHandler(this.CameraImageStitching_Click);
  358. //
  359. // 实时拍摄
  360. //
  361. this.menuTimedShooting.Click += new System.EventHandler(this.CameraTimedShooting_Click);
  362. //
  363. // 视频录像
  364. //
  365. this.menuVideoRecording.Click += new System.EventHandler(this.CameraVideoRecording_Click);
  366. //
  367. // Z轴扫描
  368. //
  369. this.menuZaxisScan.Click += new System.EventHandler(this.CameraZaxisScan_Click);
  370. //
  371. // 多维图像合成
  372. //
  373. this.menuMultiDimensional.Click += new System.EventHandler(this.MultiDimensiona_Click);
  374. //
  375. // 位置列表
  376. //
  377. this.menuListOfLocations.Click += new System.EventHandler(this.ListOfLocations_Click);
  378. //
  379. // 加载菜单的文字和icon
  380. //
  381. this.LoadNames(this.Name);
  382. this.LoadIcons();
  383. }
  384. private void preview2_Click(object sender, EventArgs e)
  385. {
  386. if (!CameraManager.IsLive)
  387. {
  388. Tips();
  389. return;
  390. }
  391. Form form = Application.OpenForms["Preview2Dialog"];//尝试获取已经弹出的窗口对象
  392. FloatingFormMethod.ShowFloatForm(form, form == null ? new Preview2Dialog() { _app = AppWorkspace }/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  393. }
  394. #region Twain
  395. string _twainlistPath = Application.StartupPath + "\\twainlist";
  396. TwainMessageForm f1 = new TwainMessageForm();
  397. bool _isInited = false;
  398. private void InitTwain()
  399. {
  400. f1.CaptureCallBack = (s) =>
  401. {
  402. AppWorkspace.OpenFileInNewWorkspace(s);
  403. System.IO.File.Delete(s);
  404. };
  405. f1.ListCallBack = () =>
  406. {
  407. UpdateTwainList();
  408. };
  409. if (System.IO.File.Exists(_twainlistPath))
  410. {
  411. UpdateTwainList();
  412. }
  413. }
  414. int _twSelect = -1;
  415. dynamic _tagSelect;
  416. public void UpdateTwainList()
  417. {
  418. var list = System.IO.File.ReadAllLines(_twainlistPath);
  419. if (!IsTwainListChanged(list))
  420. return;
  421. menuTwainSelection.DropDownItems.Clear();
  422. _twSelect = -1;
  423. for (int i = 0; i < list.Length; i++)
  424. {
  425. ToolStripMenuItem mi = new ToolStripMenuItem();
  426. var name = list[i];
  427. mi.Tag = new { Name = name, Index = i };
  428. if (name == "TuCamera")
  429. {
  430. if (!CameraManager.IsLive) continue;
  431. mi.Text = CameraManager.CurrentCamera.GetName();
  432. }
  433. else
  434. {
  435. mi.Text = name;
  436. }
  437. mi.Alignment = ToolStripItemAlignment.Right;
  438. mi.ImageAlign = ContentAlignment.MiddleRight;
  439. mi.ImageScaling = ToolStripItemImageScaling.None;
  440. // mi.CanSelect = true;
  441. if (_twSelect == -1)
  442. {
  443. _tagSelect = mi.Tag;
  444. _twSelect = i;
  445. }
  446. mi.Checked = i == _twSelect;
  447. mi.Click += (s1, e1) =>
  448. {
  449. _tagSelect = (s1 as ToolStripMenuItem).Tag as dynamic;
  450. _twSelect = _tagSelect.Index;
  451. foreach (ToolStripMenuItem item in menuTwainSelection.DropDownItems)
  452. {
  453. item.Checked = s1 == item;
  454. }
  455. };
  456. this.menuTwainSelection.DropDownItems.Add(mi);
  457. }
  458. }
  459. private bool IsTwainListChanged(string[] list)
  460. {
  461. if (list.Length != menuTwainSelection.DropDownItems.Count)
  462. {
  463. return true;
  464. }
  465. for (int i = 0; i < list.Length; i++)
  466. {
  467. if (list[i] != menuTwainSelection.DropDownItems[i].Text)
  468. return true;
  469. }
  470. return false;
  471. }
  472. static string _procName = "Saraff.Twain";
  473. static string _procPath = Application.StartupPath + "\\" + _procName + ".exe";
  474. private void MenuTwainSelection_Click(object sender, EventArgs e)
  475. {
  476. try
  477. {
  478. Process.Start(_procPath, f1.Handle.ToString() + " list");
  479. }
  480. catch (Exception ex)
  481. {
  482. MessageBox.Show(ex.Message);
  483. }
  484. }
  485. private void MenuTwainOpen_Click(object sender, EventArgs e)
  486. {
  487. try
  488. {
  489. if (_twSelect != -1)
  490. {
  491. if (Process.GetProcessesByName(_procName).Length > 0) return;
  492. Process.Start(_procPath, f1.Handle.ToString() + " open " + _twSelect);
  493. new Task(MaxTopTwainForm).Start();
  494. }
  495. }
  496. catch (Exception ex)
  497. {
  498. MessageBox.Show(ex.Message);
  499. }
  500. }
  501. private const int HWND_TOPMOST = 0;
  502. private const int SWP_NOMOVE = 0x0002;
  503. private const int SWP_NOSIZE = 0x0001;
  504. [DllImport("user32.dll")]
  505. static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true)]
  506. [return: MarshalAs(UnmanagedType.Bool)]
  507. private static extern bool SetWindowPos(IntPtr hWnd, IntPtr WndInsertAfter, int x, int y, int cx, int cy, int uFlags);
  508. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  509. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  510. private void MaxTopTwainForm()
  511. {
  512. var timeout = 100000;//10秒
  513. var time = DateTime.Now;
  514. var name = _tagSelect.Name;
  515. if (name == "ZEISS TWAIN")
  516. {
  517. name = "Axiocam";
  518. }
  519. else
  520. { return; }
  521. while ((DateTime.Now - time).TotalMilliseconds < timeout)
  522. {
  523. IntPtr handle = FindWindow(null, name);
  524. if (handle == IntPtr.Zero)
  525. {
  526. Thread.Sleep(100);
  527. continue;
  528. }
  529. SetWindowPos(handle,
  530. new IntPtr(HWND_TOPMOST),
  531. 0, 0, 0, 0,
  532. SWP_NOMOVE | SWP_NOSIZE);
  533. int r = ShowWindow(handle, 3);
  534. return;
  535. }
  536. }
  537. class TwainMessageForm : Form
  538. {
  539. public Action<string> CaptureCallBack;
  540. public Action ListCallBack;
  541. protected override void DefWndProc(ref Message m)
  542. {
  543. switch (m.Msg)
  544. {
  545. case 444://处理消息
  546. int id = (int)m.WParam;
  547. string ss = id + ".jpg";
  548. CaptureCallBack?.Invoke(ss);
  549. break;
  550. case 445:
  551. ListCallBack?.Invoke();
  552. break;
  553. default:
  554. base.DefWndProc(ref m);
  555. break;
  556. }
  557. }
  558. }
  559. #endregion
  560. #region 自动聚焦
  561. /// <summary>
  562. /// 自动聚焦
  563. /// </summary>
  564. /// <param name="sender"></param>
  565. /// <param name="e"></param>
  566. private void Autofocus_Click(object sender, EventArgs e)
  567. {
  568. if (AutoFocusWorkflow.IsWorking) return;
  569. TransferProgressDialog.ShowDialog("自动聚焦", "聚焦中......", () =>
  570. AutoFocusWorkflow.AutoFocus(AxisController.GetInstance(), () => { return m_camera.OneShoot(); }), (s1, e1) => { AutoFocusWorkflow.Stop(); });
  571. }
  572. #endregion
  573. protected override void OnDropDownOpening(EventArgs e)
  574. {
  575. InitCameraList();
  576. if (!_isInited)
  577. {
  578. InitTwain();
  579. _isInited = true;
  580. }
  581. }
  582. public void InitCameraList()
  583. {
  584. this.menuCameraSelection.DropDownItems.Clear();
  585. Func<List<ICamera>> func = new Func<List<ICamera>>(() =>
  586. {
  587. return CameraManager.GetInstance().GetCameraList(); ;
  588. });
  589. AsyncCallback callback = new AsyncCallback((x) =>
  590. {
  591. var result = func.EndInvoke(x);
  592. UpdateCameraList ut = new UpdateCameraList(UpdateCameraListMehod);
  593. this.Parent.BeginInvoke(ut, result);
  594. });
  595. func.BeginInvoke(callback, "");
  596. }
  597. public void UpdateCameraListMehod(List<ICamera> cameras)
  598. {
  599. this.menuCameraSelection.DropDownItems.Clear();
  600. if (cameras != null && cameras.Count > 0)
  601. {
  602. for (int i = 0; i < cameras.Count; i++)
  603. {
  604. ToolStripMenuItem mi = new ToolStripMenuItem(cameras[i].GetName());
  605. mi.Alignment = ToolStripItemAlignment.Right;
  606. mi.ImageAlign = ContentAlignment.MiddleRight;
  607. mi.ImageScaling = ToolStripItemImageScaling.None;
  608. mi.Tag = i;
  609. mi.Click += new EventHandler(this.MenuCreameItem_Click);
  610. if (cameras[i].GetName() == CameraManager.CurrentCamera.GetName())
  611. mi.Checked = true;
  612. mi.Text = cameras[i].GetNameEx();
  613. this.menuCameraSelection.DropDownItems.Add(mi);
  614. }
  615. }
  616. else
  617. {
  618. index = 0;
  619. }
  620. }
  621. private void MenuCreameItem_Click(object sender, EventArgs e)
  622. {
  623. if (sender != null)
  624. {
  625. foreach (ToolStripMenuItem mi in this.menuCameraSelection.DropDownItems)
  626. {
  627. mi.Checked = false;
  628. }
  629. ToolStripMenuItem temp = (ToolStripMenuItem)sender;
  630. this.index = (int)(temp.Tag);
  631. // 选中相机
  632. CameraManager.GetInstance().SetCurrentCamera(index);
  633. temp.Checked = true;
  634. // 如果设置界面打开,则刷新相机名称和SN
  635. Form form = Application.OpenForms["CameraSettingDialog"];
  636. if (form != null && !form.IsDisposed)
  637. {
  638. ((CameraSettingDialog)form).InitializeData();
  639. }
  640. }
  641. }
  642. private void CommonRotate_Click(object sender, EventArgs e)
  643. {
  644. if (!CameraManager.IsLive)
  645. {
  646. Tips();
  647. return;
  648. }
  649. Form form = Application.OpenForms["CommonRotateDialog"];//尝试获取已经弹出的窗口对象
  650. FloatingFormMethod.ShowFloatForm(form, form == null ? new CommonRotateDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  651. }
  652. private void Resolution_Click(object sender, EventArgs e)
  653. {
  654. if (!CameraManager.IsLive)
  655. {
  656. Tips();
  657. return;
  658. }
  659. Form form = Application.OpenForms["FrameCameraModeDialog"];//尝试获取已经弹出的窗口对象
  660. FloatingFormMethod.ShowFloatForm(form, form == null ? new FrameCameraModeDialog(FrameCameraMode.Resolution)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  661. }
  662. private void ROI_Click(object sender, EventArgs e)
  663. {
  664. if (!CameraManager.IsLive)
  665. {
  666. Tips();
  667. return;
  668. }
  669. Form form = Application.OpenForms["FrameCameraModeDialog"];//尝试获取已经弹出的窗口对象
  670. FloatingFormMethod.ShowFloatForm(form, form == null ? new FrameCameraModeDialog(FrameCameraMode.ROI)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  671. }
  672. private void Color_Click(object sender, EventArgs e)
  673. {
  674. if (!CameraManager.IsLive)
  675. {
  676. Tips();
  677. return;
  678. }
  679. Form form = Application.OpenForms["FrameCameraModeDialog"];//尝试获取已经弹出的窗口对象
  680. FloatingFormMethod.ShowFloatForm(form, form == null ? new FrameCameraModeDialog(FrameCameraMode.Color)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  681. }
  682. private void AdjustSaturation_Click(object sender, EventArgs e)
  683. {
  684. if (!CameraManager.IsLive)
  685. {
  686. Tips();
  687. return;
  688. }
  689. Form form = Application.OpenForms["AdjustSaturationDialog"];//尝试获取已经弹出的窗口对象
  690. FloatingFormMethod.ShowFloatForm(form, form == null ? new AdjustSaturationDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  691. }
  692. private void AdjustGain_Click(object sender, EventArgs e)
  693. {
  694. if (!CameraManager.IsLive)
  695. {
  696. Tips();
  697. return;
  698. }
  699. Form form = Application.OpenForms["AdjustGainDialog"];//尝试获取已经弹出的窗口对象
  700. FloatingFormMethod.ShowFloatForm(form, form == null ? new AdjustGainDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  701. }
  702. private void AdjustWhiteBalance_Click(object sender, EventArgs e)
  703. {
  704. if (!CameraManager.IsLive)
  705. {
  706. Tips();
  707. return;
  708. }
  709. Form form = Application.OpenForms["AdjustWhiteBalanceDialog"];//尝试获取已经弹出的窗口对象
  710. FloatingFormMethod.ShowFloatForm(form, form == null ? new AdjustWhiteBalanceDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  711. }
  712. private void AdjustExposure_Click(object sender, EventArgs e)
  713. {
  714. if (!CameraManager.IsLive)
  715. {
  716. Tips();
  717. return;
  718. }
  719. Form form = Application.OpenForms["AdjustExposureDialog"];//尝试获取已经弹出的窗口对象
  720. FloatingFormMethod.ShowFloatForm(form, form == null ? new AdjustExposureDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  721. }
  722. private void HDR_Click(object sender, EventArgs e)
  723. {
  724. menuImageCollectHdr.Checked = !menuImageCollectHdr.Checked;
  725. CameraConfigs.GetInstance().GetCurrentCameraParamModel().parame.HDR = menuImageCollectHdr.Checked ? 1 : 0;
  726. m_camera.HDR = menuImageCollectHdr.Checked ? 1 : 0;
  727. }
  728. /// <summary>
  729. /// 预览窗口
  730. /// </summary>
  731. /// <param name="sender"></param>
  732. /// <param name="e"></param>
  733. private void previewExtraction_Click(object sender, System.EventArgs e)
  734. {
  735. if (CameraManager.IsLive)
  736. {
  737. Form form = Application.OpenForms["CameraPreviewDialog"];//尝试获取已经弹出的窗口对象
  738. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraPreviewDialog(AppWorkspace) : null, AppWorkspace);
  739. }
  740. else
  741. {
  742. Tips();
  743. }
  744. // if (CameraManager.IsLive)
  745. //{
  746. // Form form = Application.OpenForms["Preview2Dialog"];//尝试获取已经弹出的窗口对象
  747. // FloatingFormMethod.ShowFloatForm(form, form == null ? new Preview2Dialog() { _app = AppWorkspace }/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  748. //}
  749. //else
  750. //{
  751. // MessageBox.Show("未找到可用相机");
  752. //}
  753. // new Preview2.Preview2Dialog() { _app = AppWorkspace }.Show();
  754. }
  755. /// <summary>
  756. /// 关闭预览
  757. /// </summary>
  758. /// <param name="sender"></param>
  759. /// <param name="e"></param>
  760. private void PreviewActionClose_Click(object sender, System.EventArgs e)
  761. {
  762. if (CameraPreviewDialog.cameraPreviewDialog != null)
  763. {
  764. CameraPreviewDialog.cameraPreviewDialog.Close();
  765. }
  766. Form form = Application.OpenForms["Preview2Dialog"];
  767. if (form != null)
  768. form.Close();
  769. }
  770. #region 拍摄
  771. private ICamera m_camera
  772. {
  773. get
  774. {
  775. return CameraManager.CurrentCamera;
  776. }
  777. }
  778. /// <summary>
  779. /// 拍摄
  780. /// </summary>
  781. /// <param name="sender"></param>
  782. /// <param name="e"></param>
  783. private void Shoot_Click(object sender, EventArgs e)
  784. {
  785. if (!CameraManager.IsLive)
  786. {
  787. Tips();
  788. return;
  789. }
  790. CameraManager.Shoot((bitmap) =>
  791. {
  792. if (bitmap == null)
  793. return;
  794. var mode = Startup.instance.configModel.CaptureSaveMode;
  795. this.Parent.Invoke(new Action(() =>
  796. {
  797. AppWorkspace.DisplayShoot(bitmap, null, Preview2Context.P2Config.CaptureRuler, mode);
  798. }));
  799. });
  800. }
  801. #endregion
  802. /// <summary>
  803. /// 相机设置点击
  804. /// </summary>
  805. /// <param name="sender"></param>
  806. /// <param name="e"></param>
  807. private void CameraSeting_Click(object sender, System.EventArgs e)
  808. {
  809. if (!CameraManager.IsLive)
  810. {
  811. Tips();
  812. return;
  813. }
  814. Form form = Application.OpenForms["CameraSettingDialog"];//尝试获取已经弹出的窗口对象
  815. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraSettingDialog()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  816. }
  817. /// <summary>
  818. /// 实时拍摄
  819. /// </summary>
  820. /// <param name="sender"></param>
  821. /// <param name="e"></param>
  822. private void CameraTimedShooting_Click(object sender, System.EventArgs e)
  823. {
  824. if (!CameraManager.IsLive)
  825. {
  826. Tips();
  827. return;
  828. }
  829. Form form = Application.OpenForms["CameraTimedShooting"];//尝试获取已经弹出的窗口对象
  830. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraTimedShooting(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  831. }
  832. /// <summary>
  833. /// 视频录像
  834. /// </summary>
  835. /// <param name="sender"></param>
  836. /// <param name="e"></param>
  837. private void CameraVideoRecording_Click(object sender, System.EventArgs e)
  838. {
  839. if (!CameraManager.IsLive)
  840. {
  841. Tips();
  842. return;
  843. }
  844. Form form = Application.OpenForms["CameraVideoRecording"];//尝试获取已经弹出的窗口对象
  845. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraVideoRecording()/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  846. }
  847. /// <summary>
  848. /// Z轴扫描
  849. /// </summary>
  850. /// <param name="sender"></param>
  851. /// <param name="e"></param>
  852. private void CameraZaxisScan_Click(object sender, System.EventArgs e)
  853. {
  854. if (!CameraManager.IsLive)
  855. {
  856. Tips();
  857. return;
  858. }
  859. Form form = Application.OpenForms["CameraZaxisScan"];//尝试获取已经弹出的窗口对象
  860. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraZaxisScan(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  861. }
  862. /// <summary>
  863. /// 实时景深扩展
  864. /// </summary>
  865. /// <param name="sender"></param>
  866. /// <param name="e"></param>
  867. private void CameraRealTimeDepth_Click(object sender, System.EventArgs e)
  868. {
  869. if (!CameraManager.IsLive)
  870. {
  871. Tips();
  872. return;
  873. }
  874. Form form = Application.OpenForms["CameraRealTimeDepth"];//尝试获取已经弹出的窗口对象
  875. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraRealTimeDepth(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  876. }
  877. /// <summary>
  878. /// 自动景深扩展
  879. /// </summary>
  880. /// <param name="sender"></param>
  881. /// <param name="e"></param>
  882. private void CameraAutomaticDepth_Click(object sender, System.EventArgs e)
  883. {
  884. if (!CameraManager.IsLive)
  885. {
  886. Tips();
  887. return;
  888. }
  889. Form form = Application.OpenForms["CameraAutomaticDepth"];//尝试获取已经弹出的窗口对象
  890. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraAutomaticDepth(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  891. }
  892. /// <summary>
  893. /// 图像拼接
  894. /// </summary>
  895. /// <param name="sender"></param>
  896. /// <param name="e"></param>
  897. private void CameraImageStitching_Click(object sender, System.EventArgs e)
  898. {
  899. if (!CameraManager.IsLive)
  900. {
  901. Tips();
  902. return;
  903. }
  904. Form form = Application.OpenForms["CameraImageStitching"];
  905. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraImageStitching(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  906. }
  907. /// <summary>
  908. /// 实时图像拼接
  909. /// </summary>
  910. /// <param name="sender"></param>
  911. /// <param name="e"></param>
  912. private void CameraRealTimeStitching_Click(object sender, System.EventArgs e)
  913. {
  914. if (!CameraManager.IsLive)
  915. {
  916. Tips();
  917. return;
  918. }
  919. Form form = Application.OpenForms["CameraRealTimeStitchingDialog"];//尝试获取已经弹出的窗口对象
  920. FloatingFormMethod.ShowFloatForm(form, form == null ? new CameraRealTimeStitchingDialog(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  921. }
  922. /// <summary>
  923. /// 多维图像合成
  924. /// </summary>
  925. /// <param name="sender"></param>
  926. /// <param name="e"></param>
  927. private void MultiDimensiona_Click(object sender, EventArgs e)
  928. {
  929. if (!CameraManager.IsLive)
  930. {
  931. Tips();
  932. return;
  933. }
  934. Form form = Application.OpenForms["MultiDimensionaDialog"];
  935. FloatingFormMethod.ShowFloatForm(form, form == null ? new MultiDimensionaDialog(AppWorkspace) : null, AppWorkspace);
  936. //Form form = Application.OpenForms["MultiDimensionaDialog"];
  937. //if ((form == null) || (form.IsDisposed))
  938. //{
  939. // MultiDimensionaDialog af = new MultiDimensionaDialog(AppWorkspace);
  940. // af.StartPosition = FormStartPosition.CenterScreen;
  941. // af.Show ##### Dialog();
  942. //}
  943. //else
  944. //{
  945. // form.Activate();
  946. // form.WindowState = FormWindowState.Normal;
  947. //}
  948. }
  949. /// <summary>
  950. /// 位置列表
  951. /// </summary>
  952. /// <param name="sender"></param>
  953. /// <param name="e"></param>
  954. private void ListOfLocations_Click(object sender, EventArgs e)
  955. {
  956. if (!CameraManager.IsLive)
  957. {
  958. Tips();
  959. return;
  960. }
  961. Form form = Application.OpenForms["ListOfLocationsDialog"];//尝试获取已经弹出的窗口对象
  962. //using (var form = new ListOfLocationsDialog(AppWorkspace)) form.ShowDialog() ;
  963. FloatingFormMethod.ShowFloatForm(form, form == null ? new ListOfLocationsDialog(AppWorkspace)/*没有弹出的窗口对象则创建*/ : null, AppWorkspace);
  964. }
  965. private string GetCultureInfoName(CultureInfo ci)
  966. {
  967. CultureInfo en_US = new CultureInfo("en-US");
  968. if (ci.Equals(en_US))
  969. {
  970. return GetCultureInfoName(ci.Parent);
  971. }
  972. else
  973. {
  974. return ci.NativeName;
  975. }
  976. }
  977. void Tips()
  978. {
  979. MessageBox.Show(PdnResources.GetString("Menu.ImageCollection.AvailableCamera"));
  980. }
  981. }
  982. }