using System; using System.Drawing; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; using PaintDotNet.Adjust.BaseImage; using PaintDotNet.Base.SettingModel; using PaintDotNet.Camera; namespace PaintDotNet.ImageCollect.CameraEDOF { internal class CameraRealTimeDepth : FloatingToolForm { #region /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.operation.text"); this.btnStart.Text = PdnResources.GetString("Menu.Started.text"); this.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text"); this.btnStop.Text = PdnResources.GetString("Menu.stop.text"); this.groupBox2.Text = PdnResources.GetString("Menu.Preview.text"); this.groupBox3.Text = PdnResources.GetString("Menu.ImageCollection.RealTimeDepth.Text"); this.Text = PdnResources.GetString("Menu.ImageCollection.RealTimeDepth.Text"); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnStart = new System.Windows.Forms.Button(); this.btnCreatImgFile = new System.Windows.Forms.Button(); this.btnStop = new System.Windows.Forms.Button(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.picDepth = new System.Windows.Forms.PictureBox(); this.picPreview = new System.Windows.Forms.PictureBox(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picDepth)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit(); this.tableLayoutPanel1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.btnStart); this.groupBox1.Controls.Add(this.btnCreatImgFile); this.groupBox1.Controls.Add(this.btnStop); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox1.Location = new System.Drawing.Point(0, 0); this.groupBox1.Margin = new System.Windows.Forms.Padding(2); this.groupBox1.Name = "groupBox1"; this.groupBox1.Padding = new System.Windows.Forms.Padding(2); this.groupBox1.Size = new System.Drawing.Size(1243, 77); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // btnStart // this.btnStart.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnStart.Location = new System.Drawing.Point(1082, 29); this.btnStart.Margin = new System.Windows.Forms.Padding(2); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(65, 29); this.btnStart.TabIndex = 0; this.btnStart.Text = "开始"; this.btnStart.UseVisualStyleBackColor = true; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // btnCreatImgFile // this.btnCreatImgFile.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnCreatImgFile.Location = new System.Drawing.Point(1160, 29); this.btnCreatImgFile.Margin = new System.Windows.Forms.Padding(2); this.btnCreatImgFile.Name = "btnCreatImgFile"; this.btnCreatImgFile.Size = new System.Drawing.Size(65, 29); this.btnCreatImgFile.TabIndex = 2; this.btnCreatImgFile.Text = "创建图片"; this.btnCreatImgFile.UseVisualStyleBackColor = true; this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click); // // btnStop // this.btnStop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnStop.Location = new System.Drawing.Point(1082, 29); this.btnStop.Margin = new System.Windows.Forms.Padding(2); this.btnStop.Name = "btnStop"; this.btnStop.Size = new System.Drawing.Size(65, 29); this.btnStop.TabIndex = 1; this.btnStop.UseVisualStyleBackColor = true; this.btnStop.Click += new System.EventHandler(this.btnStop_Click); // // groupBox2 // this.groupBox2.Controls.Add(this.picPreview); this.groupBox2.Dock = System.Windows.Forms.DockStyle.Fill; this.groupBox2.Location = new System.Drawing.Point(2, 2); this.groupBox2.Margin = new System.Windows.Forms.Padding(2); this.groupBox2.Name = "groupBox2"; this.groupBox2.Padding = new System.Windows.Forms.Padding(8); this.groupBox2.Size = new System.Drawing.Size(617, 554); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "预览"; // // groupBox3 // this.groupBox3.Controls.Add(this.picDepth); this.groupBox3.Dock = System.Windows.Forms.DockStyle.Fill; this.groupBox3.Location = new System.Drawing.Point(623, 2); this.groupBox3.Margin = new System.Windows.Forms.Padding(2); this.groupBox3.Name = "groupBox3"; this.groupBox3.Padding = new System.Windows.Forms.Padding(8); this.groupBox3.Size = new System.Drawing.Size(618, 554); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "实时景深扩展"; // // picDepth // this.picDepth.BackColor = System.Drawing.SystemColors.Control; this.picDepth.Dock = System.Windows.Forms.DockStyle.Fill; this.picDepth.Location = new System.Drawing.Point(8, 22); this.picDepth.Margin = new System.Windows.Forms.Padding(2); this.picDepth.Name = "picDepth"; this.picDepth.Size = new System.Drawing.Size(602, 524); this.picDepth.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picDepth.TabIndex = 0; this.picDepth.TabStop = false; // // picPreview // this.picPreview.BackColor = System.Drawing.SystemColors.Control; this.picPreview.Dock = System.Windows.Forms.DockStyle.Fill; this.picPreview.Location = new System.Drawing.Point(8, 22); this.picPreview.Margin = new System.Windows.Forms.Padding(2); this.picPreview.Name = "picPreview"; this.picPreview.Size = new System.Drawing.Size(601, 524); this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.picPreview.TabIndex = 0; this.picPreview.TabStop = false; // // tableLayoutPanel1 // this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Controls.Add(this.groupBox3, 1, 0); this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 0); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 77); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.tableLayoutPanel1.Size = new System.Drawing.Size(1243, 558); this.tableLayoutPanel1.TabIndex = 4; // // CameraRealTimeDepth // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1243, 635); this.Controls.Add(this.tableLayoutPanel1); this.Controls.Add(this.groupBox1); this.Margin = new System.Windows.Forms.Padding(2); this.Name = "CameraRealTimeDepth"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "实时景深扩展"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraRealTimeDepth_FormClosing); this.Load += new System.EventHandler(this.CameraRealTimeDepth_Load); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.tableLayoutPanel1, 0); this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.groupBox3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.picDepth)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit(); this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Button btnCreatImgFile; private System.Windows.Forms.Button btnStop; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.PictureBox picDepth; #endregion private AppWorkspace m_appWorkspace; private Mat m_mat; private Thread m_depthThread; private bool _isWorking; private Mat[] Coll_Ori = new Mat[2]; private Mat m_merge; private PictureBox picPreview; private TableLayoutPanel tableLayoutPanel1; private string m_filePath = Application.StartupPath + "\\Picture\\"; public CameraRealTimeDepth(AppWorkspace appWorkspace) { this.m_appWorkspace = appWorkspace; InitializeComponent(); InitializeLanguageText(); } private void CameraRealTimeDepth_Load(object sender, EventArgs e) { btnStop.Visible = false; CameraConfigs cameraConfig = CameraConfigs.GetInstance(); cameraConfig.CameraParamInit(); CameraManager.FrameCallback += CallbackDraw; } private byte[] m_pbuf; /// /// 绘制 /// /// /// public void CallbackDraw(Bitmap bitmap) { if (bitmap == null) return; var mat = PaintDotNet.Camera.Tools.ToMat(bitmap); picPreview.Image = (Image)bitmap.Clone(); m_mat = mat.Clone(); picPreview.Invalidate(); new Task(new Action(() => { WaitForDepthThreadEntry(mat); })).Start(); } private void btnStart_Click(object sender, EventArgs e) { if (_isWorking) { return; } _isWorking = true; picDepth.Image = null; m_merge = null; updateBtn(); t = 0; } private void updateBtn() { if (_isWorking) { btnStop.Visible = true; btnStart.Visible = false; btnCreatImgFile.Enabled = false; } else { btnStop.Visible = false; btnStart.Visible = true; } } int t = 0; /// /// 景深扩展 /// private void WaitForDepthThreadEntry(Mat mat) { if (!_isWorking) return; if (t > 0) return; try { t++; if (m_merge == null) { m_merge = mat; return; } //原始图 Coll_Ori[1] = mat; Coll_Ori[0] = m_merge; m_merge = Merge.GetMergeMatForCamera(Coll_Ori); this.Invoke(new Action(() => { picDepth.Image = BitmapConverter.ToBitmap(m_merge); })); } catch { } finally { t--; } } private void CameraRealTimeDepth_FormClosing(object sender, FormClosingEventArgs e) { if (m_depthThread != null) { _isWorking = false; updateBtn(); m_depthThread.Abort(); } CameraManager.FrameCallback -= CallbackDraw; if (m_mat != null && !m_mat.IsDisposed) { m_mat.Dispose(); } if (m_merge != null && !m_merge.IsDisposed) { m_merge.Dispose(); } GC.Collect(); } private void btnStop_Click(object sender, EventArgs e) { _isWorking = false; updateBtn(); btnCreatImgFile.Enabled = true; } private void btnCreatImgFile_Click(object sender, EventArgs e) { if (m_merge == null) { MessageBox.Show(PdnResources.GetString("Menu.ofimageshavenotyetbeengene.Text")); return; } DocumentWorkspace dw = m_appWorkspace.AddNewDocumentWorkspace(); //dw.rules = Startup.instance.rules; dw.xmlSaveModel = Startup.instance.ruleDB; dw.InitRulerInfo(); Document document = Document.FromImageMat(m_merge.Clone()); dw.Document = document; dw.fileText = string.Format("{0}-{1:D3}", "实时景深", Startup.instance.step_length++); m_appWorkspace.ActiveDocumentWorkspace = dw; } } }