CameraPreviewDialog.cs 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. using OpenCvSharp;
  2. using OpenCvSharp.Extensions;
  3. using OpenCvSharp.ImgHash;
  4. using PaintDotNet.Adjust;
  5. using PaintDotNet.Base.CommTool;
  6. using PaintDotNet.DbOpreate.DbBll;
  7. using PaintDotNet.DbOpreate.DbModel;
  8. using PaintDotNet.ImageCollect.CameraManager;
  9. using PaintDotNet.ImageCollect.CameraPreviewComponent;
  10. using PaintDotNet.ImageCollect.CameraPreviewComponent.Properties;
  11. using PaintDotNet.Instrument;
  12. using PaintDotNet.Measuring;
  13. using PaintDotNet.Menus;
  14. using PaintDotNet.Base.SettingModel;
  15. using PaintDotNet.SystemLayer;
  16. using StageController;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Drawing;
  20. using System.IO;
  21. using System.Runtime.InteropServices;
  22. using System.Threading;
  23. using System.Windows.Forms;
  24. using TUCAMAPI;
  25. using TUCamera;
  26. using PaintDotNet.Adjust.BaseImage;
  27. using PaintDotNet.Annotation.Measure;
  28. using PaintDotNet.Annotation.Enum;
  29. using PaintDotNet.Annotation;
  30. using Point = System.Drawing.Point;
  31. using StageController.M3H;
  32. using System.Threading.Tasks;
  33. using System.Linq;
  34. using Metis.AutoAnalysis;
  35. namespace PaintDotNet.ImageCollect
  36. {
  37. internal class CameraPreviewDialog : FloatingToolForm, IStageEvent
  38. {
  39. /// <summary>
  40. /// [原图取色]设置是否生效
  41. /// </summary>
  42. public bool o_use = false;
  43. //private Bitmap m_bitmat;
  44. ////private Mat m_mat; // [区域选择]白平衡结果存储
  45. /// <summary>
  46. /// [区域选择]设置是否生效(,设置到相机)
  47. /// </summary>
  48. public bool m_use = false;
  49. public Point m_startP; //画框的起始点
  50. private bool m_blnDraw = false;
  51. private Rectangle m_rect;//初始设置一个拖拽的显示框
  52. SolidBrush m_brush = new SolidBrush(Color.Black);
  53. int m_handle = 0;
  54. public bool isLeave = false;//鼠标是否离开窗体
  55. public static CameraPreviewDialog cameraPreviewDialog;
  56. private AppWorkspace appWorkspace;
  57. public DocumentWorkspaceWindow documentWorkspace;
  58. private GroupBox groupBox2;
  59. private GroupBox groupBox3;
  60. private GroupBox groupBox4;
  61. private ProgressBar progressBar1;
  62. private Button btnRestStep;
  63. public ComboBox comboBox1;
  64. private Panel panel1;
  65. private Panel panel2;
  66. private GroupBox groupBox1;
  67. private Panel panel3;
  68. private BaseActionsStrip baseActionsStrip;
  69. private ShootActionsStrip shootActionsStrip;
  70. private TheoreticalScaleStrip theoreticalScaleStrip;
  71. //private Mat m_mat;
  72. public static Mat m_matClone;
  73. public static Mat m_matOrig;
  74. private Document document;
  75. private ComboBox cmbConfigs;
  76. private Button btnConfigDel;
  77. private TextBox gainNumbertxt;
  78. private string picturePath = Application.StartupPath + "\\Picture\\";
  79. /// <summary>
  80. /// 显示图像
  81. /// </summary>
  82. private TUCamera.TUCamera m_camera;
  83. private CameraParamModel m_cameraParamModel;
  84. private CameraConfigs m_cameraConfig;
  85. private AxisController m_Stage;
  86. PropertiesSettingDialog fmSetting;
  87. private bool maxAndMinButtonEnable = false;//最大最小按钮点击状态
  88. private bool bestButtonEnable = false;//最佳按钮点击状态
  89. private bool gamma45ButtonEnable = false;//伽马45按钮点击状态
  90. private int trackBar1Value = -50;//亮度
  91. private int trackBar2Value = 100;//对比度
  92. private int trackBar3Value = 100;//gamma值
  93. //用法为:
  94. //label5.Text = (trackBar1.Value / 100.0).ToString("f2");
  95. //label6.Text = (trackBar2.Value / 100.0).ToString("f2");
  96. //label7.Text = (trackBar3.Value / 100.0).ToString("f2");
  97. private bool underexposedButtonEnable = false;//曝光不足按钮点击状态
  98. private bool overexposedButton = false;//过曝光按钮点击状态
  99. private double micronRatio;//每像素多少微米
  100. private CheckBox checkBox1;
  101. private bool isFitToWindow = false;//辅助判断当前尺寸是否为适应大小
  102. private TheoreticalScaleDialog theoreticalScaleDialog;//理论比例尺窗口
  103. private Dictionary<string, mic_rulers> micRulersDictonary;//标尺下拉菜单绑定数据
  104. private BindingSource bs;//绑定combobox的数据源
  105. private System.Windows.Forms.Timer tmrUpdate;
  106. private System.ComponentModel.IContainer components;
  107. private Panel panelZ;
  108. private Label lblZPos;
  109. private Button btnZClear;
  110. private Button btuZDown;
  111. private Button btnZUp;
  112. private NumericUpDown numZLen;
  113. /// <summary>
  114. /// 预览属性
  115. /// </summary>
  116. // ScriptEditDialog histogramDialog;
  117. //public bool comboBox_Change = false;
  118. public string comboBox_Value = string.Empty;
  119. public CameraPreviewDialog(AppWorkspace appWorkspace)
  120. {
  121. cameraPreviewDialog = this;
  122. this.DoubleBuffered = true;//设置本窗体
  123. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  124. SetStyle(ControlStyles.UserPaint, true);
  125. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  126. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  127. this.appWorkspace = appWorkspace;
  128. this.appWorkspace.cameraPreviewDialog = this;
  129. this.Text = PdnResources.GetString("Menu.ImageCollection.PreviewExtraction.Text");
  130. //this.FormBorderStyle = FormBorderStyle.FixedSingle;
  131. this.MinimizeBox = false;
  132. this.MaximizeBox = true;
  133. m_cameraParamModel = Startup.instance.cameraParamModel;
  134. m_cameraConfig = CameraConfigs.GetInstance();
  135. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  136. m_camera.m_bufHandler += CallbackDraw;
  137. m_camera.StartWaitForFrame();
  138. // BIN
  139. if (m_camera.IsSupportBinng())
  140. {
  141. //m_camera.SetBinningSum((BinningSum)m_cameraParamModel.parame.BinningSumation);
  142. }
  143. InitStage();
  144. InitializeComponent();
  145. InitializeLanguageText();
  146. InitializeBaseTool();
  147. InitializeShootTool();
  148. InitializeTheoreticalScaleStripTool();
  149. // 加载配置文件列表
  150. initCmbConfigs();
  151. InitializeImage();
  152. // 分辨率 非2448 * 2048的时, ROI在StartWaitForFrame后设置
  153. if (m_camera.GetResolutionId() != 0)
  154. {
  155. }
  156. //m_showThread = new Thread(new ThreadStart(ShowFrameThreadEntry));
  157. //m_showThread.IsBackground = true;
  158. // m_showThread.Start();
  159. this.Resize += new EventHandler(Form_Resize);
  160. BindingRule();
  161. getRulerMicronRatio();//计算每像素多少微米
  162. this.MinimumSize = new System.Drawing.Size(748, 656);//设置窗口最小尺寸
  163. comboBox_Value = comboBox1.Text;//获取下拉值
  164. CurrentImage = () => { return BitmapConverter.ToBitmap(m_matClone); };
  165. }
  166. private void InitStage()
  167. {
  168. try
  169. {
  170. m_Stage = AxisController.GetInstance();
  171. m_Stage.AddApp(this);
  172. }
  173. catch { }
  174. }
  175. /// <summary>
  176. /// 绑定标尺
  177. /// </summary>
  178. public void BindingRule()
  179. {
  180. this.comboBox1.Items.Clear();
  181. //绑定标尺下拉菜单
  182. List<mic_rulers> list = Startup.instance.mic_rulersAll;
  183. string rulerName = "";
  184. if (list != null)
  185. {
  186. for (int ruleI = 0; ruleI < list.Count; ruleI++)
  187. {
  188. mic_rulers rule = list[ruleI];
  189. int rulid = rule.id;
  190. if (rulid == Startup.instance.configModel.RulerId)
  191. {
  192. rulerName = rule.ruler_name;
  193. }
  194. this.comboBox1.Items.Add(rule.ruler_name);
  195. }
  196. }
  197. for (int i = 0; i < this.comboBox1.Items.Count; i++)
  198. {
  199. if (rulerName.Equals(this.comboBox1.Items[i].ToString()))
  200. {
  201. this.comboBox1.SelectedIndex = i;
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 加载数据
  207. /// </summary>
  208. public void InitializeData()
  209. {
  210. m_cameraParamModel = Startup.instance.cameraParamModel;
  211. m_cameraConfig.CameraParamInit();
  212. }
  213. /// <summary>
  214. /// 加载配置文件列表
  215. /// </summary>
  216. public void initCmbConfigs()
  217. {
  218. string selected = "";
  219. this.cmbConfigs.Items.Clear();
  220. List<string> fileNames = CameraConfigs.GetInstance().GetAllConfigFiles();
  221. foreach (string fileName in fileNames)
  222. {
  223. this.cmbConfigs.Items.Add(fileName);
  224. if (fileName.Equals(CameraConfigs.GetInstance().CurrentConfigFileName))
  225. {
  226. selected = fileName;
  227. }
  228. }
  229. if (selected.Equals("") && fileNames.Count > 0)
  230. {
  231. selected = fileNames[0];
  232. }
  233. this.cmbConfigs.SelectedItem = selected;
  234. }
  235. private void InitializeLanguageText()
  236. {
  237. this.groupBox1.Text = PdnResources.GetString("Menu.Preview.text");
  238. this.groupBox2.Text = PdnResources.GetString("Menu.operation.text");
  239. this.groupBox3.Text = PdnResources.GetString("Menu.Ruler.text");
  240. this.checkBox1.Text = PdnResources.GetString("Menu.imagecapture.Previewwindow.Shootingruler.text");
  241. this.groupBox4.Text = PdnResources.GetString("Menu.ImageCollection.CameraSetting.Text");
  242. this.btnRestStep.Text = PdnResources.GetString("Menu.reset.Text");
  243. }
  244. /// <summary>
  245. /// 实例化预览窗口同期组件
  246. /// </summary>
  247. public void InitializeComponent()
  248. {
  249. this.components = new System.ComponentModel.Container();
  250. this.groupBox1 = new System.Windows.Forms.GroupBox();
  251. this.panel2 = new System.Windows.Forms.Panel();
  252. this.panel1 = new System.Windows.Forms.Panel();
  253. this.groupBox2 = new System.Windows.Forms.GroupBox();
  254. this.panelZ = new System.Windows.Forms.Panel();
  255. this.lblZPos = new System.Windows.Forms.Label();
  256. this.btnZClear = new System.Windows.Forms.Button();
  257. this.btuZDown = new System.Windows.Forms.Button();
  258. this.btnZUp = new System.Windows.Forms.Button();
  259. this.numZLen = new System.Windows.Forms.NumericUpDown();
  260. this.groupBox3 = new System.Windows.Forms.GroupBox();
  261. this.panel3 = new System.Windows.Forms.Panel();
  262. this.checkBox1 = new System.Windows.Forms.CheckBox();
  263. this.comboBox1 = new System.Windows.Forms.ComboBox();
  264. this.groupBox4 = new System.Windows.Forms.GroupBox();
  265. this.btnConfigDel = new System.Windows.Forms.Button();
  266. this.cmbConfigs = new System.Windows.Forms.ComboBox();
  267. this.progressBar1 = new System.Windows.Forms.ProgressBar();
  268. this.btnRestStep = new System.Windows.Forms.Button();
  269. this.tmrUpdate = new System.Windows.Forms.Timer(this.components);
  270. this.groupBox1.SuspendLayout();
  271. this.groupBox2.SuspendLayout();
  272. this.panelZ.SuspendLayout();
  273. ((System.ComponentModel.ISupportInitialize)(this.numZLen)).BeginInit();
  274. this.groupBox3.SuspendLayout();
  275. this.groupBox4.SuspendLayout();
  276. this.SuspendLayout();
  277. //
  278. // groupBox1
  279. //
  280. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  281. | System.Windows.Forms.AnchorStyles.Left)
  282. | System.Windows.Forms.AnchorStyles.Right)));
  283. this.groupBox1.Controls.Add(this.panel2);
  284. this.groupBox1.Controls.Add(this.panel1);
  285. this.groupBox1.Location = new System.Drawing.Point(20, 20);
  286. this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  287. this.groupBox1.Name = "groupBox1";
  288. this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
  289. this.groupBox1.Size = new System.Drawing.Size(1060, 662);
  290. this.groupBox1.TabIndex = 1;
  291. this.groupBox1.TabStop = false;
  292. this.groupBox1.Text = "预览";
  293. //
  294. // panel2
  295. //
  296. this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  297. | System.Windows.Forms.AnchorStyles.Left)
  298. | System.Windows.Forms.AnchorStyles.Right)));
  299. this.panel2.Location = new System.Drawing.Point(10, 32);
  300. this.panel2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  301. this.panel2.Name = "panel2";
  302. this.panel2.Size = new System.Drawing.Size(1026, 552);
  303. this.panel2.TabIndex = 2;
  304. //
  305. // panel1
  306. //
  307. this.panel1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
  308. this.panel1.Location = new System.Drawing.Point(10, 592);
  309. this.panel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  310. this.panel1.Name = "panel1";
  311. this.panel1.Size = new System.Drawing.Size(1026, 60);
  312. this.panel1.TabIndex = 1;
  313. //
  314. // groupBox2
  315. //
  316. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  317. | System.Windows.Forms.AnchorStyles.Right)));
  318. this.groupBox2.Controls.Add(this.panelZ);
  319. this.groupBox2.Location = new System.Drawing.Point(20, 690);
  320. this.groupBox2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  321. this.groupBox2.Name = "groupBox2";
  322. this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
  323. this.groupBox2.Size = new System.Drawing.Size(1060, 82);
  324. this.groupBox2.TabIndex = 2;
  325. this.groupBox2.TabStop = false;
  326. this.groupBox2.Text = "操作";
  327. //
  328. // panelZ
  329. //
  330. this.panelZ.Controls.Add(this.lblZPos);
  331. this.panelZ.Controls.Add(this.btnZClear);
  332. this.panelZ.Controls.Add(this.btuZDown);
  333. this.panelZ.Controls.Add(this.btnZUp);
  334. this.panelZ.Controls.Add(this.numZLen);
  335. this.panelZ.Dock = System.Windows.Forms.DockStyle.Right;
  336. this.panelZ.Location = new System.Drawing.Point(765, 25);
  337. this.panelZ.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  338. this.panelZ.Name = "panelZ";
  339. this.panelZ.Size = new System.Drawing.Size(291, 53);
  340. this.panelZ.TabIndex = 0;
  341. this.panelZ.Visible = false;
  342. //
  343. // lblZPos
  344. //
  345. this.lblZPos.BackColor = System.Drawing.SystemColors.ButtonHighlight;
  346. this.lblZPos.Dock = System.Windows.Forms.DockStyle.Fill;
  347. this.lblZPos.Location = new System.Drawing.Point(198, 0);
  348. this.lblZPos.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
  349. this.lblZPos.Name = "lblZPos";
  350. this.lblZPos.Size = new System.Drawing.Size(93, 53);
  351. this.lblZPos.TabIndex = 3;
  352. this.lblZPos.Text = "0000";
  353. this.lblZPos.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  354. //
  355. // btnZClear
  356. //
  357. this.btnZClear.Dock = System.Windows.Forms.DockStyle.Left;
  358. this.btnZClear.Location = new System.Drawing.Point(150, 0);
  359. this.btnZClear.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  360. this.btnZClear.Name = "btnZClear";
  361. this.btnZClear.Size = new System.Drawing.Size(48, 53);
  362. this.btnZClear.TabIndex = 2;
  363. this.btnZClear.Text = "C";
  364. this.btnZClear.UseVisualStyleBackColor = true;
  365. this.btnZClear.Click += new System.EventHandler(this.btnZClear_Click);
  366. //
  367. // btuZDown
  368. //
  369. this.btuZDown.Dock = System.Windows.Forms.DockStyle.Left;
  370. this.btuZDown.Location = new System.Drawing.Point(102, 0);
  371. this.btuZDown.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  372. this.btuZDown.Name = "btuZDown";
  373. this.btuZDown.Size = new System.Drawing.Size(48, 53);
  374. this.btuZDown.TabIndex = 1;
  375. this.btuZDown.Text = "↓";
  376. this.btuZDown.UseVisualStyleBackColor = true;
  377. this.btuZDown.Click += new System.EventHandler(this.btuZDown_Click);
  378. //
  379. // btnZUp
  380. //
  381. this.btnZUp.Dock = System.Windows.Forms.DockStyle.Left;
  382. this.btnZUp.Location = new System.Drawing.Point(57, 0);
  383. this.btnZUp.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  384. this.btnZUp.Name = "btnZUp";
  385. this.btnZUp.Size = new System.Drawing.Size(45, 53);
  386. this.btnZUp.TabIndex = 0;
  387. this.btnZUp.Text = "↑";
  388. this.btnZUp.UseVisualStyleBackColor = true;
  389. this.btnZUp.Click += new System.EventHandler(this.btnZUp_Click);
  390. //
  391. // numZLen
  392. //
  393. this.numZLen.Dock = System.Windows.Forms.DockStyle.Left;
  394. this.numZLen.Font = new System.Drawing.Font("Calibri", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  395. this.numZLen.Location = new System.Drawing.Point(0, 0);
  396. this.numZLen.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  397. this.numZLen.Minimum = new decimal(new int[] {
  398. 1,
  399. 0,
  400. 0,
  401. 0});
  402. this.numZLen.Name = "numZLen";
  403. this.numZLen.Size = new System.Drawing.Size(57, 33);
  404. this.numZLen.TabIndex = 4;
  405. this.numZLen.Value = new decimal(new int[] {
  406. 1,
  407. 0,
  408. 0,
  409. 0});
  410. //
  411. // groupBox3
  412. //
  413. this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  414. | System.Windows.Forms.AnchorStyles.Right)));
  415. this.groupBox3.Controls.Add(this.panel3);
  416. this.groupBox3.Controls.Add(this.checkBox1);
  417. this.groupBox3.Controls.Add(this.comboBox1);
  418. this.groupBox3.Location = new System.Drawing.Point(20, 783);
  419. this.groupBox3.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  420. this.groupBox3.Name = "groupBox3";
  421. this.groupBox3.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
  422. this.groupBox3.Size = new System.Drawing.Size(662, 82);
  423. this.groupBox3.TabIndex = 3;
  424. this.groupBox3.TabStop = false;
  425. this.groupBox3.Text = "标尺";
  426. //
  427. // panel3
  428. //
  429. this.panel3.BackColor = System.Drawing.Color.Transparent;
  430. this.panel3.Location = new System.Drawing.Point(435, 28);
  431. this.panel3.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  432. this.panel3.Name = "panel3";
  433. this.panel3.Size = new System.Drawing.Size(166, 36);
  434. this.panel3.TabIndex = 2;
  435. //
  436. // checkBox1
  437. //
  438. this.checkBox1.AutoSize = true;
  439. this.checkBox1.Location = new System.Drawing.Point(288, 36);
  440. this.checkBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  441. this.checkBox1.Name = "checkBox1";
  442. this.checkBox1.Size = new System.Drawing.Size(106, 22);
  443. this.checkBox1.TabIndex = 1;
  444. this.checkBox1.Text = "拍摄标尺";
  445. this.checkBox1.UseVisualStyleBackColor = true;
  446. this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
  447. //
  448. // comboBox1
  449. //
  450. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  451. this.comboBox1.FormattingEnabled = true;
  452. this.comboBox1.Location = new System.Drawing.Point(10, 32);
  453. this.comboBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  454. this.comboBox1.Name = "comboBox1";
  455. this.comboBox1.Size = new System.Drawing.Size(226, 26);
  456. this.comboBox1.TabIndex = 0;
  457. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  458. this.comboBox1.SelectionChangeCommitted += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
  459. //
  460. // groupBox4
  461. //
  462. this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  463. this.groupBox4.Controls.Add(this.btnConfigDel);
  464. this.groupBox4.Controls.Add(this.cmbConfigs);
  465. this.groupBox4.Location = new System.Drawing.Point(690, 783);
  466. this.groupBox4.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  467. this.groupBox4.Name = "groupBox4";
  468. this.groupBox4.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
  469. this.groupBox4.Size = new System.Drawing.Size(390, 82);
  470. this.groupBox4.TabIndex = 4;
  471. this.groupBox4.TabStop = false;
  472. this.groupBox4.Text = "相机设置";
  473. //
  474. // btnConfigDel
  475. //
  476. this.btnConfigDel.Location = new System.Drawing.Point(320, 30);
  477. this.btnConfigDel.Name = "btnConfigDel";
  478. this.btnConfigDel.Size = new System.Drawing.Size(46, 27);
  479. this.btnConfigDel.TabIndex = 1;
  480. this.btnConfigDel.Text = "Del";
  481. this.btnConfigDel.UseVisualStyleBackColor = true;
  482. this.btnConfigDel.Click += new System.EventHandler(this.btnConfigDel_Click);
  483. //
  484. // cmbConfigs
  485. //
  486. this.cmbConfigs.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  487. this.cmbConfigs.FormattingEnabled = true;
  488. this.cmbConfigs.Location = new System.Drawing.Point(15, 32);
  489. this.cmbConfigs.Name = "cmbConfigs";
  490. this.cmbConfigs.Size = new System.Drawing.Size(276, 26);
  491. this.cmbConfigs.TabIndex = 0;
  492. this.cmbConfigs.SelectedIndexChanged += new System.EventHandler(this.cmbConfigs_SelectedIndexChanged);
  493. //
  494. // progressBar1
  495. //
  496. this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
  497. | System.Windows.Forms.AnchorStyles.Right)));
  498. this.progressBar1.Location = new System.Drawing.Point(20, 876);
  499. this.progressBar1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  500. this.progressBar1.Name = "progressBar1";
  501. this.progressBar1.Size = new System.Drawing.Size(962, 34);
  502. this.progressBar1.TabIndex = 5;
  503. //
  504. // btnRestStep
  505. //
  506. this.btnRestStep.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  507. this.btnRestStep.Location = new System.Drawing.Point(992, 876);
  508. this.btnRestStep.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
  509. this.btnRestStep.Name = "btnRestStep";
  510. this.btnRestStep.Size = new System.Drawing.Size(88, 34);
  511. this.btnRestStep.TabIndex = 6;
  512. this.btnRestStep.Text = "复位";
  513. this.btnRestStep.UseVisualStyleBackColor = true;
  514. this.btnRestStep.Click += new System.EventHandler(this.btnRestStep_Click);
  515. //
  516. // tmrUpdate
  517. //
  518. this.tmrUpdate.Interval = 1000;
  519. this.tmrUpdate.Tick += new System.EventHandler(this.tmrUpdate_Tick);
  520. //
  521. // CameraPreviewDialog
  522. //
  523. this.AutoScaleDimensions = new System.Drawing.SizeF(144F, 144F);
  524. this.ClientSize = new System.Drawing.Size(1098, 927);
  525. this.Controls.Add(this.btnRestStep);
  526. this.Controls.Add(this.progressBar1);
  527. this.Controls.Add(this.groupBox4);
  528. this.Controls.Add(this.groupBox3);
  529. this.Controls.Add(this.groupBox2);
  530. this.Controls.Add(this.groupBox1);
  531. this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
  532. this.Name = "CameraPreviewDialog";
  533. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraPreviewDialog_FormClosing);
  534. this.Load += new System.EventHandler(this.CameraPreviewDialog_Load);
  535. this.Controls.SetChildIndex(this.groupBox1, 0);
  536. this.Controls.SetChildIndex(this.groupBox2, 0);
  537. this.Controls.SetChildIndex(this.groupBox3, 0);
  538. this.Controls.SetChildIndex(this.groupBox4, 0);
  539. this.Controls.SetChildIndex(this.progressBar1, 0);
  540. this.Controls.SetChildIndex(this.btnRestStep, 0);
  541. this.groupBox1.ResumeLayout(false);
  542. this.groupBox2.ResumeLayout(false);
  543. this.panelZ.ResumeLayout(false);
  544. ((System.ComponentModel.ISupportInitialize)(this.numZLen)).EndInit();
  545. this.groupBox3.ResumeLayout(false);
  546. this.groupBox3.PerformLayout();
  547. this.groupBox4.ResumeLayout(false);
  548. this.ResumeLayout(false);
  549. }
  550. public void InitializeImage()
  551. {
  552. m_rect = new Rectangle();
  553. this.documentWorkspace = new DocumentWorkspaceWindow(appWorkspace);
  554. this.documentWorkspace.panel.MouseDown += DocumentWorkspace_MouseDown;
  555. this.documentWorkspace.panel.MouseMove += PictureBox1_MouseMove;
  556. this.documentWorkspace.panel.MouseUp += PictureBox1_MouseUp;
  557. this.documentWorkspace.panel.Paint += Panel_Paint;
  558. this.documentWorkspace.refueshZoomTrackValue = false;
  559. Bitmap bitmap = new Bitmap(m_cameraParamModel.parame.previewRectW, m_cameraParamModel.parame.previewRectH);
  560. document = Document.FromImage(bitmap);
  561. //if (bitmap.Width > 3 && bitmap.Height > 3)
  562. //{
  563. // m_rect.X = Math.Min(10, bitmap.Width / 2 - 1);
  564. // m_rect.Y = Math.Min(10, bitmap.Height / 2 - 1);
  565. // m_rect.Width = bitmap.Width - 2 * m_rect.X;
  566. // m_rect.Height = bitmap.Height - 2 * m_rect.Y;
  567. // Console.WriteLine(m_rect);
  568. //}
  569. //else
  570. {
  571. m_rect.X = 0;
  572. m_rect.Y = 0;
  573. m_rect.Width = 0;
  574. m_rect.Height = 0;
  575. }
  576. this.documentWorkspace.AppWorkspaceTop = this.appWorkspace;
  577. this.documentWorkspace.Visible = true;
  578. //this.documentWorkspace.PanelBottom.Visible = false;
  579. this.documentWorkspace.Dock = DockStyle.Fill;
  580. //this.documentWorkspace.RulersEnabled = true;
  581. this.panel2.Controls.Add(this.documentWorkspace);
  582. this.documentWorkspace.Document = document;
  583. this.documentWorkspace.ScaleFactorChanged += new EventHandler(DocumentWorkspace_ScaleFactorChanged);
  584. if (this.documentWorkspace.oldDrawTreeView == null && MeasureListDialog.drawNodes != null)
  585. {
  586. this.documentWorkspace.oldDrawTreeView = MeasureListDialog.drawNodes;
  587. //this.documentWorkspace.previewMeasure = true;
  588. this.documentWorkspace.rules = Startup.instance.rules;
  589. }
  590. }
  591. private void Panel_Paint(object sender, PaintEventArgs e)
  592. {
  593. if (m_use && documentWorkspace.CompositionSurface.Width != 0 && documentWorkspace.CompositionSurface.Height != 0)
  594. {
  595. if (m_rect.Width > 0 && m_rect.Height > 0)
  596. {
  597. //
  598. // 以下是计算绘制图片的位置和大小并绘制图片
  599. //
  600. Rectangle rc = this.documentWorkspace.panel.ClientRectangle;
  601. int width = (int)(this.documentWorkspace.CompositionSurface.Width * this.documentWorkspace.ScaleFactor.Ratio);
  602. int height = (int)(this.documentWorkspace.CompositionSurface.Height * this.documentWorkspace.ScaleFactor.Ratio);
  603. int x = (rc.Width < width) ? this.documentWorkspace.panel.AutoScrollPosition.X : (rc.Width - width) / 2;
  604. int y = (rc.Height < height) ? this.documentWorkspace.panel.AutoScrollPosition.Y : (rc.Height - height) / 2;
  605. //if (m_mat != null)
  606. //{
  607. // oldMPatch.Dispose();
  608. // oldMPatch = m_mat;
  609. //}
  610. //m_mat = new Data.Action.Action5012().PerformProcess(BitmapConverter.ToMat(m_bitmap).Clone(new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height)));
  611. ////
  612. //// 以下是绘制网格、标注、测量、视场等开始
  613. ////
  614. e.Graphics.TranslateTransform(x, y);
  615. e.Graphics.ScaleTransform((float)this.documentWorkspace.ScaleFactor.Ratio, (float)this.documentWorkspace.ScaleFactor.Ratio);
  616. //if (m_bitmat != null)
  617. //{
  618. // e.Graphics.DrawImage(m_bitmat, m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height);
  619. //}
  620. if (!m_blnDraw)
  621. {
  622. e.Graphics.DrawRectangle(new Pen(Color.Black/*Red*/, 2), m_rect);//重新绘制颜色为红色
  623. for (int i = 1; i <= 8; i++)
  624. {
  625. e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
  626. }
  627. }
  628. e.Graphics.ScaleTransform(1 / (float)this.documentWorkspace.ScaleFactor.Ratio, 1 / (float)this.documentWorkspace.ScaleFactor.Ratio);
  629. e.Graphics.TranslateTransform(-x, -y);
  630. }
  631. }
  632. else
  633. {
  634. m_rect.Width = 0;
  635. m_rect.Height = 0;
  636. }
  637. }
  638. private void DocumentWorkspace_MouseDown(object sender, MouseEventArgs e)
  639. {
  640. if (!m_use && !o_use)
  641. return;
  642. // 换算后的点
  643. Point point1 = documentWorkspace.GetScalePoint(e.Location);//PointF
  644. m_handle = HitTest(point1);
  645. if (m_handle < 0)
  646. {
  647. if (m_rect.Contains(point1))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框
  648. {
  649. m_handle = 9;
  650. }
  651. }
  652. this.Cursor = GetHandleCursor(m_handle);
  653. m_startP = point1;
  654. Invalidate();
  655. m_blnDraw = true;
  656. }
  657. private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
  658. {
  659. if (!m_use)
  660. return;
  661. if (m_handle > 0 ||/*&&*/ m_blnDraw)
  662. {
  663. if (e.Button != MouseButtons.Left)//判断是否按下左键
  664. return;
  665. MoveHandleTo(documentWorkspace.GetScalePoint(e.Location), m_handle);
  666. }
  667. }
  668. private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
  669. {
  670. if (!m_use)
  671. return;
  672. m_blnDraw = false; //结束绘制
  673. this.Cursor = Cursors.Default;
  674. SetROI();
  675. this.documentWorkspace/*pictureBox1*/.Refresh();
  676. }
  677. private void SetROI()
  678. {
  679. if (m_use)
  680. {
  681. //m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
  682. //this.m_cameraParamModel.parame.previewRectX = m_targetX;
  683. //this.m_cameraParamModel.parame.previewRectY = m_targetY;
  684. //this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  685. //this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  686. }
  687. }
  688. public Rectangle GetHandleRectangle(int handleNumber)
  689. {
  690. Point point = GetHandle(handleNumber);
  691. return new Rectangle(point.X - 4, point.Y - 4, 8, 8);
  692. }
  693. public int HitTest(Point point)
  694. {
  695. for (int i = 1; i <= 8; i++)
  696. {
  697. if (GetHandleRectangle(i).Contains(point))
  698. return i;
  699. }
  700. return -1;
  701. }
  702. public Point GetHandle(int handleNumber)
  703. {
  704. int x, y, xCenter, yCenter;
  705. xCenter = m_rect.X + m_rect.Width / 2;
  706. yCenter = m_rect.Y + m_rect.Height / 2;
  707. x = m_rect.X;
  708. y = m_rect.Y;
  709. switch (handleNumber)
  710. {
  711. case 1:
  712. x = m_rect.X;
  713. y = m_rect.Y;
  714. break;
  715. case 2:
  716. x = xCenter;
  717. y = m_rect.Y;
  718. break;
  719. case 3:
  720. x = m_rect.Right;
  721. y = m_rect.Y;
  722. break;
  723. case 4:
  724. x = m_rect.Right;
  725. y = yCenter;
  726. break;
  727. case 5:
  728. x = m_rect.Right;
  729. y = m_rect.Bottom;
  730. break;
  731. case 6:
  732. x = xCenter;
  733. y = m_rect.Bottom;
  734. break;
  735. case 7:
  736. x = m_rect.X;
  737. y = m_rect.Bottom;
  738. break;
  739. case 8:
  740. x = m_rect.X;
  741. y = yCenter;
  742. break;
  743. }
  744. return new Point(x, y);
  745. }
  746. public Cursor GetHandleCursor(int handleNumber)
  747. {
  748. switch (handleNumber)
  749. {
  750. case 1:
  751. return Cursors.SizeNWSE;
  752. case 2:
  753. return Cursors.SizeNS;
  754. case 3:
  755. return Cursors.SizeNESW;
  756. case 4:
  757. return Cursors.SizeWE;
  758. case 5:
  759. return Cursors.SizeNWSE;
  760. case 6:
  761. return Cursors.SizeNS;
  762. case 7:
  763. return Cursors.SizeNESW;
  764. case 8:
  765. return Cursors.SizeWE;
  766. default:
  767. return Cursors.Default;
  768. }
  769. }
  770. public void MoveHandleTo(Point point, int handleNumber)
  771. {
  772. if (point.X > this.documentWorkspace.CompositionSurface/*pictureBox1*/.Width)
  773. {
  774. point.X = documentWorkspace.CompositionSurface/*pictureBox1*/.Width;
  775. }
  776. if (point.X < 0)
  777. {
  778. point.X = 0;
  779. }
  780. if (point.Y < 0)
  781. {
  782. point.Y = 0;
  783. }
  784. if (point.Y > documentWorkspace.CompositionSurface/*pictureBox1*/.Height)
  785. {
  786. point.Y = documentWorkspace.CompositionSurface/*pictureBox1*/.Height;
  787. }
  788. Point point1 = new Point(m_startP.X, m_startP.Y);
  789. if (point1.X > this.documentWorkspace.CompositionSurface/*pictureBox1*/.Width)
  790. {
  791. point1.X = documentWorkspace.CompositionSurface/*pictureBox1*/.Width;
  792. }
  793. if (point1.X < 0)
  794. {
  795. point1.X = 0;
  796. }
  797. if (point1.Y < 0)
  798. {
  799. point1.Y = 0;
  800. }
  801. if (point1.Y > documentWorkspace.CompositionSurface/*pictureBox1*/.Height)
  802. {
  803. point1.Y = documentWorkspace.CompositionSurface/*pictureBox1*/.Height;
  804. }
  805. m_rect.X = Math.Min(point.X, point1.X);
  806. m_rect.Y = Math.Min(point.Y, point1.Y);
  807. m_rect.Width = Math.Abs(point.X - point1.X);
  808. m_rect.Height = Math.Abs(point.Y - point1.Y);
  809. return;
  810. }
  811. /// <summary>
  812. /// 接收相机frame
  813. /// </summary>
  814. /// <param name="frame"></param>
  815. /// <param name="obj"></param>
  816. public void CallbackDraw(Bitmap bitmap)
  817. {
  818. try
  819. {
  820. var mat = BitmapConverter.ToMat(bitmap);
  821. m_matClone = mat.Clone();
  822. if (!this.IsShown)
  823. {
  824. return;
  825. }
  826. if (fmSetting != null)
  827. {
  828. m_matOrig = mat.Clone();
  829. try
  830. {
  831. trackBar1Value = fmSetting.displayCameraControl.trackBar1Value;
  832. trackBar2Value = fmSetting.displayCameraControl.trackBar2Value;
  833. trackBar3Value = fmSetting.displayCameraControl.trackBar3Value;
  834. }
  835. catch (Exception e)
  836. {
  837. System.Console.WriteLine("nullException can be catched here:" + e.Message);
  838. }
  839. }
  840. if (trackBar1Value != -50 || trackBar2Value != 100 || trackBar3Value != 100)
  841. {
  842. mat = PaintDotNet.Setting.LabelComponent.DisplayCameraControl.scrollMethod(mat, (trackBar1Value / 100.0).ToString("f2"), (trackBar2Value / 100.0).ToString("f2"), (trackBar3Value / 100.0).ToString("f2"));
  843. }
  844. else if (bestButtonEnable || maxAndMinButtonEnable || gamma45ButtonEnable)
  845. {
  846. if (bestButtonEnable)//最佳
  847. mat = AdjustIntent.CalHistMethod(mat, 1, gamma45ButtonEnable);
  848. if (maxAndMinButtonEnable)//最大最小
  849. mat = AdjustIntent.CalHistMethod(mat, 2, gamma45ButtonEnable);
  850. if (!bestButtonEnable && !maxAndMinButtonEnable)//仅伽马0.45
  851. mat = AdjustIntent.CalHistMethod(mat, 0, gamma45ButtonEnable);
  852. }
  853. if (underexposedButtonEnable)//曝光不足
  854. mat = ExposedIntent.UnderExposed(mat, 30, 0.5f);
  855. if (overexposedButton)//过曝光
  856. mat = ExposedIntent.OverExposed(mat, 225, 0.5f);
  857. //17461
  858. if (m_cameraParamModel.parame.ShadingCorrection == 1)//阴影矫正
  859. mat = AdjustIntent.ShadingCorrection(mat);
  860. ShowFrameThreadEntry(mat);
  861. //this.appWorkspace.UpdatePixelTracking(mat.Clone());
  862. }
  863. catch (Exception ex)
  864. {
  865. Console.WriteLine(ex.Message);
  866. }
  867. }
  868. private void ShowFrameThreadEntry(Mat matClone)
  869. {
  870. try
  871. {
  872. Bitmap m_bitmap = BitmapConverter.ToBitmap(matClone);
  873. if (o_use && m_startP.X >= 0 && m_startP.Y >= 0)
  874. {
  875. int x = m_startP.X;
  876. int y = m_startP.Y;
  877. if (x >= 0 && y >= 0 && x < m_bitmap.Width && y < m_bitmap.Height)
  878. {
  879. OpenCvSharp.Mat srt = OpenCvSharp.Extensions.BitmapConverter.ToMat(m_bitmap);
  880. OpenCvSharp.Vec3b bgr = srt.At<OpenCvSharp.Vec3b>(y, x);
  881. Color color = Color.FromArgb(bgr[2], bgr[1], bgr[0]);
  882. /// <summary>
  883. /// 当前处理的程序
  884. /// </summary>
  885. Data.Param.ParamObject action = new Data.Action.Action5012();
  886. //重新初始化选项列表
  887. Base.Args args = action.Lists[0];
  888. args.value = 3;
  889. for (int j = 0; j < args.choiseList.Count; j++)
  890. {
  891. Data.Param.BooleanObject argsChoise = args.choiseList[j] as Data.Param.BooleanObject;
  892. if (argsChoise.Lists.Count > 0)
  893. {
  894. Data.Param.ColorNumber phaseColor = argsChoise.Lists[0] as Data.Param.ColorNumber;
  895. if (phaseColor.Key.Equals("imageColor"))//原图取色
  896. {
  897. phaseColor.Value = color.ToArgb();// this.panel3.BackColor.ToArgb();
  898. }
  899. }
  900. }
  901. OpenCvSharp.Mat mat = action.PerformProcess(srt);
  902. //string filename2 = "E:\\show1" + (misshow++) + ".jpg";
  903. //Cv2.ImWrite(filename2, mat);
  904. this.documentWorkspace.Document = Document.FromByteArr(mat);
  905. }
  906. }
  907. else if (m_use && m_rect.Width > 0 && m_rect.Height > 0)
  908. {
  909. Mat m_mat = new Data.Action.Action5012().PerformProcess(matClone.Clone(new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height)));
  910. //复制用户选中的区域到原图像
  911. //Mat mask0 = m_matClone.CvtColor(ColorConversionCodes.RGBA2GRAY);
  912. Mat pos0 = new Mat(matClone, new Rect(m_rect.X, m_rect.Y, m_rect.Width, m_rect.Height));
  913. m_mat.CopyTo(pos0, m_mat);
  914. this.documentWorkspace.Document = Document.FromImage(BitmapConverter.ToBitmap(matClone));
  915. m_mat.Dispose();
  916. }
  917. else
  918. this.documentWorkspace.Document = Document.FromByteArr(matClone/*BitmapConverter.ToBitmap(m_matClone)*/);
  919. if (fmSetting != null && m_matOrig != null)
  920. {
  921. this.BeginInvoke(new Action(() => { fmSetting?.resetMat(m_matOrig); }));
  922. }
  923. }
  924. catch (Exception)
  925. {
  926. }
  927. }
  928. /// <summary>
  929. /// 初始化基础工具
  930. /// </summary>
  931. public void InitializeBaseTool()
  932. {
  933. this.baseActionsStrip = new BaseActionsStrip();
  934. //十字线按钮点击
  935. this.baseActionsStrip.crossButton.Click += new System.EventHandler(this.crossButton_Click);
  936. //网格按钮点击
  937. this.baseActionsStrip.gridButton.Click += new System.EventHandler(this.gridButton_Click);
  938. //方形按钮点击
  939. this.baseActionsStrip.squareButton.Click += new System.EventHandler(this.squareButton_Click);
  940. //圆形按钮点击
  941. this.baseActionsStrip.roundButton.Click += new System.EventHandler(this.roundButton_Click);
  942. //实际大小按钮点击
  943. this.baseActionsStrip.actualSizeButton.Click += new System.EventHandler(this.actualSizeButton_Click);
  944. //合适大小按钮点击
  945. this.baseActionsStrip.zoomToWindowButton.Click += new System.EventHandler(this.zoomToWindowButton_Click);
  946. //全屏按钮点击
  947. this.baseActionsStrip.fullScreenButton.Click += new System.EventHandler(this.fullScreenButton_Click);
  948. //缩小按钮点击
  949. this.baseActionsStrip.zoomOutButton.Click += new System.EventHandler(this.zoomOutButton_Click);
  950. //放大按钮点击
  951. this.baseActionsStrip.zoomInButton.Click += new System.EventHandler(this.zoomInButton_Click);
  952. this.baseActionsStrip.GripStyle = ToolStripGripStyle.Hidden;
  953. this.baseActionsStrip.Dock = DockStyle.None;
  954. this.baseActionsStrip.Location = new System.Drawing.Point(136, 0);
  955. this.panel1.Controls.Add(baseActionsStrip);
  956. this.gainNumbertxt = new TextBox();
  957. this.gainNumbertxt.Location = new System.Drawing.Point(554, 8);
  958. this.gainNumbertxt.Size = new System.Drawing.Size(62, 22);
  959. this.gainNumbertxt.Anchor = AnchorStyles.Left;
  960. this.gainNumbertxt.Text = PdnResources.GetString("Menu.nothing.Text");
  961. this.gainNumbertxt.ReadOnly = true;
  962. this.panel1.Controls.Add(this.gainNumbertxt);
  963. }
  964. /// <summary>
  965. /// 初始化快捷工具
  966. /// </summary>
  967. public void InitializeShootTool()
  968. {
  969. this.shootActionsStrip = new ShootActionsStrip();
  970. //拍摄按钮点击
  971. this.shootActionsStrip.shootButton.Click += new System.EventHandler(this.camera_Click);
  972. //单次拍摄按钮点击
  973. this.shootActionsStrip.aloneShootButton.Click += new System.EventHandler(this.aloneShootButton_Click);
  974. //速度按钮点击
  975. this.shootActionsStrip.speedButton.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(CopyDetail_DropDownItemClicked);
  976. //曝光时间点击事件
  977. this.shootActionsStrip.exposureTimeButton.Click += new System.EventHandler(this.exposureTimeButton_Click);
  978. //白平衡点击事件
  979. this.shootActionsStrip.whiteBalanceButton.Click += new System.EventHandler(this.whiteBalance_Click);
  980. //最佳点击事件
  981. this.shootActionsStrip.bestButton.Click += new System.EventHandler(this.bestButton_Click);
  982. //最小最大点击事件
  983. this.shootActionsStrip.maxAndMinButton.Click += new System.EventHandler(this.maxAndMinButton_Click);
  984. //原始状态事件
  985. this.shootActionsStrip.originalStateButton.Click += new System.EventHandler(this.originalStateButton_Click);
  986. //伽马0.45事件
  987. this.shootActionsStrip.gamma45Button.Click += new System.EventHandler(this.gamma45Button_Click);
  988. //曝光不足事件
  989. this.shootActionsStrip.underexposedButton.Click += new System.EventHandler(this.underexposedButton_Click);
  990. //过曝光事件
  991. this.shootActionsStrip.overexposedButton.Click += new System.EventHandler(this.overexposedButton_Click);
  992. //预览属性事件
  993. this.shootActionsStrip.propertiesButton.Click += new System.EventHandler(this.propertiesButton_Click);
  994. //自动聚焦事件
  995. this.shootActionsStrip.autoFocusButton.Click += new System.EventHandler(this.autoFocusButton_Click);
  996. this.shootActionsStrip.GripStyle = ToolStripGripStyle.Hidden;
  997. shootActionsStrip.Dock = DockStyle.Fill;
  998. this.groupBox2.Controls.Add(this.shootActionsStrip);
  999. }
  1000. /// <summary>
  1001. /// 理论比例尺初始化
  1002. /// </summary>
  1003. private void InitializeTheoreticalScaleStripTool()
  1004. {
  1005. this.theoreticalScaleStrip = new TheoreticalScaleStrip();
  1006. this.theoreticalScaleStrip.theoreticalScaleButton.Click += new System.EventHandler(this.theoreticalScaleButton_Click);
  1007. this.theoreticalScaleStrip.GripStyle = ToolStripGripStyle.Hidden;
  1008. this.theoreticalScaleStrip.Dock = DockStyle.None;
  1009. this.theoreticalScaleStrip.BackColor = Color.Transparent;
  1010. this.panel3.Controls.Add(theoreticalScaleStrip);
  1011. }
  1012. /// <summary>
  1013. /// 拍照按钮被点击
  1014. /// </summary>
  1015. /// <param name="sender"></param>
  1016. /// <param name="e"></param>
  1017. public void camera_Click(object sender, EventArgs e)
  1018. {
  1019. Capture1();
  1020. }
  1021. /// <summary>
  1022. /// 单次拍摄被点击
  1023. /// </summary>
  1024. /// <param name="sender"></param>
  1025. /// <param name="e"></param>
  1026. public void aloneShootButton_Click(object sender, EventArgs e)
  1027. {
  1028. Capture1();
  1029. this.Close();
  1030. }
  1031. public void Capture1()
  1032. {
  1033. var bitmap = documentWorkspace.CompositionSurface.CreateAliasedBitmap();
  1034. var mode = Startup.instance.configModel.CaptureSaveMode;
  1035. var r = appWorkspace.DisplayShoot(bitmap, this.documentWorkspace.GraphicsList, Startup.instance.configModel.CaptureHasRoler, mode);
  1036. if (!r) return;
  1037. }
  1038. /// <summary>
  1039. /// 白平衡点击
  1040. /// </summary>
  1041. /// <param name="sender"></param>
  1042. /// <param name="e"></param>
  1043. public void whiteBalance_Click(object sender, EventArgs e)
  1044. {
  1045. m_camera.SetWhiteBalanceMode(WhiteBalanceMode.AUTO);
  1046. }
  1047. /// <summary>
  1048. /// 速度点击
  1049. /// </summary>
  1050. void CopyDetail_DropDownItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
  1051. {
  1052. foreach (ToolStripMenuItem item in shootActionsStrip.speedButton.DropDownItems)
  1053. {
  1054. if (item.Name != e.ClickedItem.Name)
  1055. {
  1056. item.Checked = false;
  1057. }
  1058. else
  1059. {
  1060. item.Checked = true;
  1061. this.shootActionsStrip.speedButton.Text = e.ClickedItem.Text;
  1062. }
  1063. }
  1064. }
  1065. /// <summary>
  1066. /// 曝光时间被点击
  1067. /// </summary>
  1068. /// <param name="sender"></param>
  1069. /// <param name="e"></param>
  1070. public void exposureTimeButton_Click(object sender, EventArgs e)
  1071. {
  1072. m_camera.SetExposureMode(ExposureMode.AUTO);
  1073. }
  1074. /// <summary>
  1075. /// 最佳被点击
  1076. /// </summary>
  1077. /// <param name="sender"></param>
  1078. /// <param name="e"></param>
  1079. public void bestButton_Click(object sender, EventArgs e)
  1080. {
  1081. if (this.shootActionsStrip == null || this.shootActionsStrip.bestButton == null)
  1082. return;
  1083. // TUCamAPI.TUCAM_Capa_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATEXPOSURE_MODE, 0);
  1084. // TUCamAPI.TUCAM_Prop_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_EXPOSURETM, (double)(20000000 / 1000.0f), 0);
  1085. //MessageBox.Show("临时调整曝光时长为20000000us");
  1086. bestButtonEnable = !bestButtonEnable;
  1087. var button = this.shootActionsStrip.bestButton;
  1088. button.Checked = !button.Checked;
  1089. if (maxAndMinButtonEnable)
  1090. {
  1091. maxAndMinButtonEnable = !maxAndMinButtonEnable;
  1092. this.shootActionsStrip.maxAndMinButton.Checked = false;
  1093. }
  1094. }
  1095. /// <summary>
  1096. /// 最大最小被点击
  1097. /// </summary>
  1098. /// <param name="sender"></param>
  1099. /// <param name="e"></param>
  1100. public void maxAndMinButton_Click(object sender, EventArgs e)
  1101. {
  1102. if (this.shootActionsStrip == null || this.shootActionsStrip.maxAndMinButton == null)
  1103. return;
  1104. // TUCamAPI.TUCAM_Capa_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDCAPA.TUIDC_ATWBALANCE, 0);
  1105. // TUCamAPI.TUCAM_Prop_SetValue(m5pro.m_opCamList[m5pro.m_indexCam].hIdxTUCam, (int)TUCAM_IDPROP.TUIDP_CHNLGAIN, 1220.0, 2);
  1106. //MessageBox.Show("最大最小被点击临时变绿色");
  1107. maxAndMinButtonEnable = !maxAndMinButtonEnable;
  1108. var button = this.shootActionsStrip.maxAndMinButton;
  1109. button.Checked = !button.Checked;
  1110. if (bestButtonEnable)
  1111. {
  1112. bestButtonEnable = !bestButtonEnable;
  1113. this.shootActionsStrip.bestButton.Checked = false;
  1114. }
  1115. }
  1116. /// <summary>
  1117. /// 原始状态被点击
  1118. /// </summary>
  1119. /// <param name="sender"></param>
  1120. /// <param name="e"></param>
  1121. public void originalStateButton_Click(object sender, EventArgs e)
  1122. {
  1123. if (this.shootActionsStrip == null || this.shootActionsStrip.originalStateButton == null)//1012###19464
  1124. return;
  1125. bestButtonEnable = false;
  1126. maxAndMinButtonEnable = false;
  1127. gamma45ButtonEnable = false;
  1128. this.shootActionsStrip.bestButton.Checked = false;
  1129. this.shootActionsStrip.maxAndMinButton.Checked = false;
  1130. this.shootActionsStrip.gamma45Button.Checked = false;
  1131. }
  1132. /// <summary>
  1133. /// 伽马0.45被点击
  1134. /// </summary>
  1135. /// <param name="sender"></param>
  1136. /// <param name="e"></param>
  1137. public void gamma45Button_Click(object sender, EventArgs e)
  1138. {
  1139. if (this.shootActionsStrip == null || this.shootActionsStrip.gamma45Button == null)
  1140. return;
  1141. gamma45ButtonEnable = !gamma45ButtonEnable;
  1142. var button = this.shootActionsStrip.gamma45Button;
  1143. button.Checked = !button.Checked;
  1144. }
  1145. /// <summary>
  1146. /// 曝光不足被点击
  1147. /// </summary>
  1148. /// <param name="sender"></param>
  1149. /// <param name="e"></param>
  1150. public void underexposedButton_Click(object sender, EventArgs e)
  1151. {
  1152. underexposedButtonEnable = !underexposedButtonEnable;
  1153. //if (overexposedButton)
  1154. //{
  1155. // overexposedButton = !overexposedButton;
  1156. // this.shootActionsStrip.overexposedButton.Checked = false;
  1157. //}
  1158. var button = (ToolStripButton)sender;
  1159. button.Checked = !button.Checked;
  1160. }
  1161. /// <summary>
  1162. /// 预览属性按钮被点击
  1163. /// </summary>
  1164. /// <param name="sender"></param>
  1165. /// <param name="e"></param>
  1166. public void propertiesButton_Click(object sender, EventArgs e)
  1167. {
  1168. Form form = Application.OpenForms["PropertiesSettingDialog"];//尝试获取已经弹出的窗口对象
  1169. if (form == null || form.IsDisposed)
  1170. {
  1171. if (fmSetting != null)
  1172. {
  1173. fmSetting.Dispose();
  1174. fmSetting = null;
  1175. }
  1176. form = null;
  1177. fmSetting = new PropertiesSettingDialog(this);
  1178. //最佳事件
  1179. fmSetting.displayCameraControl.button2.Click += new System.EventHandler(this.bestButton_Click);
  1180. //最小最大点击事件
  1181. fmSetting.displayCameraControl.button3.Click += new System.EventHandler(this.maxAndMinButton_Click);
  1182. //原始状态事件
  1183. fmSetting.displayCameraControl.button5.Click += new System.EventHandler(this.originalStateButton_Click);
  1184. //伽马0.45事件
  1185. fmSetting.displayCameraControl.button4.Click += new System.EventHandler(this.gamma45Button_Click);
  1186. fmSetting.FormClosing += Fm_FormClosing;
  1187. //fm.ShowDialog();
  1188. FloatingFormMethod.ShowFloatForm(form, form == null ? fmSetting/*没有弹出的窗口对象则创建*/ : null, this.appWorkspace);
  1189. }
  1190. else
  1191. {
  1192. form.Activate();
  1193. form.WindowState = FormWindowState.Normal;
  1194. }
  1195. }
  1196. private void Fm_FormClosing(object sender, FormClosingEventArgs e)
  1197. {
  1198. if (fmSetting != null)//0915###避免引起异常问题
  1199. {
  1200. fmSetting.Dispose();
  1201. fmSetting = null;
  1202. }
  1203. }
  1204. /// <summary>
  1205. /// 过曝光被点击
  1206. /// </summary>
  1207. /// <param name="sender"></param>
  1208. /// <param name="e"></param>
  1209. public void overexposedButton_Click(object sender, EventArgs e)
  1210. {
  1211. overexposedButton = !overexposedButton;
  1212. //if (underexposedButtonEnable)
  1213. //{
  1214. // underexposedButtonEnable = !underexposedButtonEnable;
  1215. // this.shootActionsStrip.underexposedButton.Checked = false;
  1216. //}
  1217. var button = (ToolStripButton)sender;
  1218. button.Checked = !button.Checked;
  1219. }
  1220. /// <summary>
  1221. /// 十字线按钮
  1222. /// </summary>
  1223. /// <param name="sender"></param>
  1224. /// <param name="e"></param>
  1225. public void crossButton_Click(object sender, EventArgs e)
  1226. {
  1227. if (this.documentWorkspace != null)
  1228. {
  1229. this.documentWorkspace.GridCrossCurveEnabled = !this.documentWorkspace.GridCrossCurveEnabled;
  1230. this.documentWorkspace.Refresh();
  1231. var button = (ToolStripButton)sender;
  1232. button.Checked = !button.Checked;
  1233. }
  1234. }
  1235. /// <summary>
  1236. /// 网格按钮
  1237. /// </summary>
  1238. /// <param name="sender"></param>
  1239. /// <param name="e"></param>
  1240. public void gridButton_Click(object sender, EventArgs e)
  1241. {
  1242. if (this.documentWorkspace != null)
  1243. {
  1244. this.documentWorkspace.GridLineEnabled = !this.documentWorkspace.GridLineEnabled;
  1245. this.documentWorkspace.Refresh();
  1246. var button = (ToolStripButton)sender;
  1247. button.Checked = !button.Checked;
  1248. }
  1249. }
  1250. /// <summary>
  1251. /// 方形按钮
  1252. /// </summary>
  1253. /// <param name="sender"></param>
  1254. /// <param name="e"></param>
  1255. public void squareButton_Click(object sender, EventArgs e)
  1256. {
  1257. if (this.documentWorkspace != null)
  1258. {
  1259. this.documentWorkspace.GridRectangleEnabled = !this.documentWorkspace.GridRectangleEnabled;
  1260. this.documentWorkspace.Refresh();
  1261. var button = (ToolStripButton)sender;
  1262. button.Checked = !button.Checked;
  1263. }
  1264. }
  1265. /// <summary>
  1266. /// 圆形按钮
  1267. /// </summary>
  1268. /// <param name="sender"></param>
  1269. /// <param name="e"></param>
  1270. public void roundButton_Click(object sender, EventArgs e)
  1271. {
  1272. if (this.documentWorkspace != null)
  1273. {
  1274. this.documentWorkspace.GridRoundEnabled = !this.documentWorkspace.GridRoundEnabled;
  1275. this.documentWorkspace.Refresh();
  1276. var button = (ToolStripButton)sender;
  1277. button.Checked = !button.Checked;
  1278. }
  1279. }
  1280. /// <summary>
  1281. /// 实际大小按钮
  1282. /// </summary>
  1283. /// <param name="sender"></param>
  1284. /// <param name="e"></param>
  1285. public void actualSizeButton_Click(object sender, EventArgs e)
  1286. {
  1287. if (this.documentWorkspace.CompositionSurface != null)
  1288. {
  1289. this.documentWorkspace.ZoomBasis = ZoomBasis.ScaleFactor;
  1290. this.documentWorkspace.ScaleFactor = ScaleFactor.OneToOne;
  1291. this.isFitToWindow = false;
  1292. }
  1293. }
  1294. /// <summary>
  1295. /// 合适大小按钮
  1296. /// </summary>
  1297. /// <param name="sender"></param>
  1298. /// <param name="e"></param>
  1299. public void zoomToWindowButton_Click(object sender, EventArgs e)
  1300. {
  1301. if (this.documentWorkspace.CompositionSurface != null)
  1302. {
  1303. this.documentWorkspace.ZoomToWindow();
  1304. this.isFitToWindow = true;
  1305. }
  1306. }
  1307. /// <summary>
  1308. /// 全屏显示按钮
  1309. /// </summary>
  1310. /// <param name="sender"></param>
  1311. /// <param name="e"></param>
  1312. public void fullScreenButton_Click(object sender, EventArgs e)
  1313. {
  1314. var button = (ToolStripButton)sender;
  1315. if (this.WindowState == FormWindowState.Normal)
  1316. {
  1317. this.WindowState = FormWindowState.Maximized;
  1318. button.Checked = true;
  1319. }
  1320. else
  1321. {
  1322. this.WindowState = FormWindowState.Normal;
  1323. button.Checked = false;
  1324. }
  1325. if (isFitToWindow && this.documentWorkspace.CompositionSurface != null)
  1326. this.documentWorkspace.ZoomToWindow();
  1327. this.Invalidate();
  1328. }
  1329. /// <summary>
  1330. /// 缩小按钮
  1331. /// </summary>
  1332. /// <param name="sender"></param>
  1333. /// <param name="e"></param>
  1334. public void zoomOutButton_Click(object sender, EventArgs e)
  1335. {
  1336. if (this.documentWorkspace.CompositionSurface != null)
  1337. {
  1338. this.documentWorkspace.ZoomOut();
  1339. }
  1340. }
  1341. /// <summary>
  1342. /// 放大按钮
  1343. /// </summary>
  1344. /// <param name="sender"></param>
  1345. /// <param name="e"></param>
  1346. public void zoomInButton_Click(object sender, EventArgs e)
  1347. {
  1348. if (this.documentWorkspace.CompositionSurface != null)
  1349. {
  1350. this.documentWorkspace.ZoomIn();
  1351. }
  1352. }
  1353. private void CameraPreviewDialog_FormClosing(object sender, FormClosingEventArgs e)
  1354. {
  1355. AutoFocusWorkflow.Stop();
  1356. Thread.Sleep(50);
  1357. Dispose();
  1358. tmrUpdate.Enabled = false;
  1359. }
  1360. public new void Dispose()
  1361. {
  1362. //m_Stage.Close();
  1363. m_Stage.RemoveApp(this);
  1364. m_camera.m_bufHandler -= CallbackDraw;
  1365. m_camera.StopWaitForFrame();
  1366. if (m_matClone != null)
  1367. {
  1368. m_matClone.Dispose();
  1369. }
  1370. if (fmSetting != null)
  1371. fmSetting.Close();
  1372. if (this.documentWorkspace != null && !this.documentWorkspace.IsDisposed)
  1373. this.documentWorkspace.Dispose();
  1374. if (this.document != null)
  1375. this.document.Dispose();
  1376. base.Dispose();
  1377. GC.Collect();
  1378. }
  1379. /// <summary>
  1380. /// 判断颜色灰度值 如果是true 则设置为灰色
  1381. /// </summary>
  1382. /// <param name="isGray"></param>
  1383. public void SetColorModel(bool isGray)
  1384. {
  1385. //m5pro.SetColorModel(isGray);
  1386. }
  1387. /// <summary>
  1388. /// 选择相机设置
  1389. /// </summary>
  1390. /// <param name="sender"></param>
  1391. /// <param name="e"></param>
  1392. private void cmbConfigs_SelectedIndexChanged(object sender, EventArgs e)
  1393. {
  1394. ComboBox cb = (ComboBox)sender;
  1395. Startup.instance.cameraParamModel = m_cameraConfig.GetCameraParamModel(cb.SelectedItem.ToString());
  1396. m_cameraParamModel = Startup.instance.cameraParamModel;
  1397. m_cameraConfig.CameraParamInit();
  1398. }
  1399. /// <summary>
  1400. /// 删除相机设置
  1401. /// </summary>
  1402. /// <param name="sender"></param>
  1403. /// <param name="e"></param>
  1404. private void btnConfigDel_Click(object sender, EventArgs e)
  1405. {
  1406. if (this.cmbConfigs.SelectedItem != null)
  1407. {
  1408. if (this.cmbConfigs.SelectedItem.ToString().Equals("CameraParam"))
  1409. {
  1410. MessageBox.Show(PdnResources.GetString("Menu.hedefaultconfiguratallowdeletion.Text"), PdnResources.GetString("Menu.ensure.text"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1411. return;
  1412. }
  1413. DialogResult result = MessageBox.Show(PdnResources.GetString("Menu.suredeletit.Text") + "?", PdnResources.GetString("Menu.Tips.text"), MessageBoxButtons.OKCancel);
  1414. if (result == DialogResult.OK)
  1415. {
  1416. CameraConfigs.GetInstance().Delete();
  1417. initCmbConfigs();
  1418. }
  1419. }
  1420. }
  1421. /// <summary>
  1422. /// 用于修改全屏按钮状态
  1423. /// </summary>
  1424. /// <param name="sender"></param>
  1425. /// <param name="e"></param>
  1426. private void Form_Resize(object sender, EventArgs e)
  1427. {
  1428. if (this.WindowState == FormWindowState.Normal)
  1429. {
  1430. baseActionsStrip.fullScreenButton.Checked = false;
  1431. }
  1432. else
  1433. {
  1434. baseActionsStrip.fullScreenButton.Checked = true;
  1435. }
  1436. if (isFitToWindow && this.documentWorkspace.CompositionSurface != null)
  1437. this.documentWorkspace.ZoomToWindow();
  1438. this.Invalidate();
  1439. }
  1440. /// <summary>
  1441. /// 获取当前标尺并换算为微米
  1442. /// </summary>
  1443. public void getRulerMicronRatio()
  1444. {
  1445. Startup.instance.rules.TryGetValue(MeasurementUnit.Micron, out this.micronRatio);
  1446. this.documentWorkspace.MicronRatio = this.micronRatio;
  1447. }
  1448. /// <summary>
  1449. /// 标尺下拉菜单选项改变
  1450. /// </summary>
  1451. /// <param name="sender"></param>
  1452. /// <param name="e"></param>
  1453. private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
  1454. {
  1455. //getRulerMicronRatio();
  1456. //this.documentWorkspace.Refresh();
  1457. }
  1458. /// <summary>
  1459. /// 理论比例尺按钮
  1460. /// </summary>
  1461. /// <param name="sender"></param>
  1462. /// <param name="e"></param>
  1463. public void theoreticalScaleButton_Click(object sender, EventArgs e)
  1464. {
  1465. if (theoreticalScaleDialog == null)
  1466. {
  1467. theoreticalScaleDialog = new TheoreticalScaleDialog(this.appWorkspace, MeasureActionMenu.ArrayList1, MeasureActionMenu.ArrayList2,
  1468. MeasureActionMenu.ArrayList3);
  1469. theoreticalScaleDialog.button7.Click += new EventHandler(button7_Click);
  1470. //theoreticalScaleDialog.FormClosing += new FormClosingEventHandler(theoreticalScaleDialog_FormClosing);
  1471. }
  1472. theoreticalScaleDialog.StartPosition = FormStartPosition.CenterParent;
  1473. theoreticalScaleDialog.ShowDialog();
  1474. }
  1475. /// <summary>
  1476. /// 理论比例尺窗口关闭
  1477. /// </summary>
  1478. /// <param name="sender"></param>
  1479. /// <param name="e"></param>
  1480. private void theoreticalScaleDialog_FormClosing(object sender, FormClosingEventArgs e)
  1481. {
  1482. int selectedNum = this.comboBox1.SelectedIndex;
  1483. this.comboBox1.DataSource = null;
  1484. bs = new BindingSource();
  1485. bs.DataSource = micRulersDictonary;
  1486. this.comboBox1.DataSource = bs;
  1487. this.comboBox1.DisplayMember = "Key";
  1488. this.comboBox1.ValueMember = "Value";
  1489. this.comboBox1.SelectedIndex = selectedNum;
  1490. this.comboBox1.Refresh();
  1491. }
  1492. /// <summary>
  1493. /// 理论比例尺保存
  1494. /// </summary>
  1495. /// <param name="sender"></param>
  1496. /// <param name="e"></param>
  1497. private void button7_Click(object sender, EventArgs e)
  1498. {
  1499. }
  1500. private double GrayHistogramArea(Bitmap bitmap)
  1501. {
  1502. Mat image = BitmapConverter.ToMat(bitmap);
  1503. if (image.Channels() == 3)
  1504. {
  1505. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  1506. }
  1507. int[] list = new int[256];
  1508. var date = image.ToBytes();
  1509. foreach (int value in date)
  1510. {
  1511. list[value]++;
  1512. }
  1513. return 0;
  1514. }
  1515. public static Mat ToGrayMat(Bitmap bitmap)
  1516. {
  1517. Mat image = BitmapConverter.ToMat(bitmap);
  1518. if (image.Channels() == 3)
  1519. {
  1520. Cv2.CvtColor(image, image, ColorConversionCodes.BGR2GRAY);
  1521. }
  1522. return image;
  1523. }
  1524. /// <summary>
  1525. /// 高斯距离
  1526. /// </summary>
  1527. /// <param name="im"></param>
  1528. public static double GaussianDiff(Mat im)
  1529. {
  1530. if (im.Channels() == 3)
  1531. {
  1532. Cv2.CvtColor(im, im, ColorConversionCodes.BGR2GRAY);
  1533. }
  1534. Mat blurred = new Mat();
  1535. Cv2.GaussianBlur(im, blurred, new OpenCvSharp.Size(7, 7), 1.66);
  1536. Cv2.Absdiff(im, blurred, blurred);
  1537. return Cv2.Mean(blurred)[0];
  1538. }
  1539. public static double Sobel(Bitmap bitmap)
  1540. {
  1541. var graymat = ToGrayMat(bitmap);
  1542. Mat imageSobel = new Mat();
  1543. Cv2.Sobel(graymat, imageSobel, 2, 1, 1);
  1544. return Cv2.Mean(imageSobel)[0];
  1545. }
  1546. /// <summary>
  1547. /// 获取输入图片的清晰度
  1548. /// </summary>
  1549. /// <param name="bitmap"></param>
  1550. /// <returns></returns>
  1551. private static double getMeanValueOfBitmap(Bitmap bitmap)
  1552. {
  1553. Mat converted = BitmapConverter.ToMat(bitmap);
  1554. Mat imageGrey = new Mat();
  1555. Mat imageSobel = new Mat();
  1556. try
  1557. {
  1558. Cv2.CvtColor(converted, imageGrey, ColorConversionCodes.RGB2GRAY);
  1559. }
  1560. catch (Exception)
  1561. {
  1562. imageGrey = converted;
  1563. }
  1564. Mat meanValueImage = new Mat();
  1565. Mat meanStdValueImage = new Mat();
  1566. //求灰度图像的标准差 值越大越好
  1567. OpenCvSharp.Cv2.MeanStdDev(imageGrey, meanValueImage, meanStdValueImage);
  1568. return meanStdValueImage.At<double>(0, 0);
  1569. }
  1570. #region AutoFocus
  1571. private static bool _focusing;
  1572. public static Func<Bitmap> CurrentImage;
  1573. private void autoFocusButton_Click(object sender, EventArgs e)
  1574. {
  1575. m_camera.SetExposureMode(ExposureMode.MANUAL);
  1576. _focusing = true;
  1577. new Thread(new ThreadStart(() =>
  1578. {
  1579. var result = AutoFocusWorkflow.AutoFocus(m_Stage, CurrentImage);
  1580. if (string.IsNullOrEmpty(result))
  1581. {
  1582. MessageBox.Show("聚焦成功");
  1583. }
  1584. else
  1585. {
  1586. MessageBox.Show(result);
  1587. }
  1588. _focusing = false;
  1589. })).Start();
  1590. }
  1591. bool _cameraDown = false;
  1592. private void tmrUpdate_Tick(object sender, EventArgs e)
  1593. {
  1594. if (_cameraDown)
  1595. {
  1596. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  1597. if (m_camera != null && m_camera.IsOpen())
  1598. {
  1599. m_camera.m_bufHandler += CallbackDraw;
  1600. m_camera.StartWaitForFrame();
  1601. _cameraDown = false;
  1602. }
  1603. }
  1604. else if (!m_camera.IsOpen())
  1605. {
  1606. _cameraDown = true;
  1607. //MessageBox.Show("相机已断开");
  1608. //this.Close();
  1609. }
  1610. // label1.Text = m_CurrentValue.ToString("F2");
  1611. if (_focusing)
  1612. progressBar1.Value = (int)(Math.Min(AutoFocusWorkflow.CurrentValue, progressBar1.Maximum));
  1613. else progressBar1.Value = 0;
  1614. // setBarValue((int)m_MeanValue);
  1615. }
  1616. private static double m_resetStep1 = 0;
  1617. private void btnRestStep_Click(object sender, EventArgs e)
  1618. {
  1619. if (_focusing) return;
  1620. if (m_resetStep1 != 0)
  1621. {
  1622. m_Stage.UpTo(m_resetStep1);
  1623. m_resetStep1 = 0;
  1624. }
  1625. }
  1626. #endregion
  1627. private void DocumentWorkspace_ScaleFactorChanged(object sender, EventArgs e)
  1628. {
  1629. this.gainNumbertxt.Text = Math.Round((this.documentWorkspace.ScaleRatio * 100), 2) + "%";
  1630. }
  1631. public void RefreshDrawNodes()
  1632. {
  1633. TreeView drawTreeView = this.documentWorkspace.oldDrawTreeView;
  1634. if (drawTreeView != null)
  1635. {
  1636. for (int i = 0; i < this.documentWorkspace.GraphicsList.Count; i++)
  1637. {
  1638. if (this.documentWorkspace.GraphicsList[i].objectType == DrawClass.Measure)
  1639. {
  1640. int count1 = drawTreeView.Nodes.Count;
  1641. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).drawingProperties.Clear();
  1642. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Clear();
  1643. for (int k = 0; k < count1; k++)
  1644. {
  1645. int count2 = drawTreeView.Nodes[k].Nodes.Count;
  1646. for (int j = 0; j < count2; j++)
  1647. {
  1648. if (this.documentWorkspace.GraphicsList[i].drawToolType == (DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name))
  1649. {
  1650. bool drawb = false;
  1651. if (drawTreeView.Nodes[k].Nodes[j].Nodes.Count == 0)
  1652. {
  1653. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).drawingProperties.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), new string[] { "nothing" });
  1654. if (this.documentWorkspace.oldDrawTreeView != null)
  1655. {
  1656. if (this.documentWorkspace.oldDrawTreeView.Nodes[k].Nodes[j].Nodes.Count != 0)
  1657. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), true);
  1658. }
  1659. else
  1660. {
  1661. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), true);
  1662. }
  1663. }
  1664. else
  1665. {
  1666. int a = 0;
  1667. string[] arr = new string[drawTreeView.Nodes[k].Nodes[j].Nodes.Count];
  1668. foreach (TreeNode node2 in drawTreeView.Nodes[k].Nodes[j].Nodes)
  1669. {
  1670. arr[a] = node2.Name;
  1671. a++;
  1672. }
  1673. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).drawingProperties.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), arr);
  1674. if (this.documentWorkspace.oldDrawTreeView != null)
  1675. {
  1676. if (drawTreeView.Nodes[k].Nodes[j].Nodes.Count != this.documentWorkspace.oldDrawTreeView.Nodes[k].Nodes[j].Nodes.Count)
  1677. {
  1678. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), true);
  1679. }
  1680. else
  1681. {
  1682. foreach (TreeNode oldNode in this.documentWorkspace.oldDrawTreeView.Nodes[k].Nodes[j].Nodes)
  1683. {
  1684. /*if (!arr.Contains(oldNode.Name))
  1685. {
  1686. drawb = true;
  1687. }*/
  1688. }
  1689. if (drawb)
  1690. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), drawb);
  1691. }
  1692. }
  1693. else
  1694. {
  1695. ((MeasureDrawObject)(this.documentWorkspace.GraphicsList[i])).pointChangeObject.Add((DrawToolType)Enum.Parse(typeof(DrawToolType), drawTreeView.Nodes[k].Nodes[j].Name), true);
  1696. }
  1697. }
  1698. this.documentWorkspace.Refresh();
  1699. }
  1700. }
  1701. }
  1702. }
  1703. }
  1704. }
  1705. }
  1706. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  1707. {
  1708. if (((ComboBox)sender).SelectedItem != null)
  1709. {
  1710. if (comboBox_Value != ((ComboBox)sender).SelectedItem.ToString())
  1711. {
  1712. comboBox_Value = ((ComboBox)sender).SelectedItem.ToString();
  1713. //comboBox_Change = true;
  1714. List<mic_rulers> list = Startup.instance.mic_rulersAll;
  1715. if (list != null)
  1716. {
  1717. for (int ruleI = 0; ruleI < list.Count; ruleI++)
  1718. {
  1719. mic_rulers rule = list[ruleI];
  1720. if (rule.ruler_name.Equals(((ComboBox)sender).SelectedItem.ToString()))
  1721. {
  1722. int rulid = rule.id;
  1723. string rulerName2 = rule.ruler_name;
  1724. if (rulid != Startup.instance.configModel.RulerId)
  1725. {
  1726. //设置当前选中标尺
  1727. this.appWorkspace.SetActiveRulerIndex(((ComboBox)sender).SelectedIndex);
  1728. //comboBox_Change = false;
  1729. this.documentWorkspace.rules = Startup.instance.rules;
  1730. }
  1731. break;
  1732. }
  1733. }
  1734. }
  1735. }
  1736. getRulerMicronRatio();
  1737. this.documentWorkspace.Refresh();
  1738. }
  1739. }
  1740. public void OnUpdatePosition()
  1741. {
  1742. try
  1743. {
  1744. this.Invoke(new Action(() =>
  1745. {
  1746. lblZPos.Text = m_Stage.Z.ToString("f2");
  1747. }));
  1748. }
  1749. catch { }
  1750. }
  1751. public void OnTimeoutConnect()
  1752. {
  1753. MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
  1754. }
  1755. public void OnErrorSend()
  1756. {
  1757. MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
  1758. }
  1759. private void CameraPreviewDialog_Load(object sender, EventArgs e)
  1760. {
  1761. tmrUpdate.Enabled = true;
  1762. progressBar1.Maximum = 65;
  1763. checkBox1.Checked = Startup.instance.configModel.CaptureHasRoler;
  1764. }
  1765. private void btnZUp_Click(object sender, EventArgs e)
  1766. {
  1767. var value = (double)numZLen.Value;
  1768. m_Stage.Up(value);
  1769. }
  1770. private void btuZDown_Click(object sender, EventArgs e)
  1771. {
  1772. var value = (double)numZLen.Value;
  1773. m_Stage.Up(-value);
  1774. }
  1775. private void btnZClear_Click(object sender, EventArgs e)
  1776. {
  1777. m_Stage.FreeZ();
  1778. m_Stage.ClearPosZ();
  1779. }
  1780. private void checkBox1_Click(object sender, EventArgs e)
  1781. {
  1782. Startup.instance.configModel.CaptureHasRoler = checkBox1.Checked;
  1783. }
  1784. /// <summary>
  1785. /// 判断鼠标是否在预览窗口内
  1786. /// </summary>
  1787. /// <returns></returns>
  1788. public bool IsLeave()
  1789. {
  1790. try
  1791. {
  1792. Point point = this.PointToClient(Control.MousePosition);
  1793. isLeave = point.X > this.ClientSize.Width || point.X < 0 || point.Y > this.ClientSize.Height || point.Y < 0 ? false : true;
  1794. }
  1795. catch (Exception)
  1796. {
  1797. isLeave = false;
  1798. }
  1799. return isLeave;
  1800. }
  1801. }
  1802. }