using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using OpenCvSharp;
using PaintDotNet.Camera;
using StageController;
namespace PaintDotNet.ImageCollect.CameraEDOF
{
///
/// 自动景深扩展
///
internal class CameraAutomaticDepth : FloatingToolForm
{
///
/// 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.btnCreatImgFile.Text = PdnResources.GetString("Menu.Createpicture.text");
this.btnWorking.Text = PdnResources.GetString("Menu.Started.text");
this.groupBox2.Text = PdnResources.GetString("Menu.paramssettings.text");
this.rdoMod2.Text = PdnResources.GetString("Menu.Model2.Text");
this.rdoMod1.Text = PdnResources.GetString("Menu.imagecapture.Autoeldexpansion.patternone.text");
this.groupBox4.Text = PdnResources.GetString("Menu.ImageCollection.AutomaticDepth.Text");
this.label1.Text = PdnResources.GetString("Menu.imagecapture.Autoeldexpansion.Shootingmode.text") + ":";
this.Text = PdnResources.GetString("Menu.ImageCollection.AutomaticDepth.Text");
this.groupBox3.Text = PdnResources.GetString("Menu.Preview.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.btnZClear = new System.Windows.Forms.Button();
this.btnCreatImgFile = new System.Windows.Forms.Button();
this.btnWorking = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.controlPanel = new System.Windows.Forms.Panel();
this.rdoMod2 = new System.Windows.Forms.RadioButton();
this.rdoMod1 = new System.Windows.Forms.RadioButton();
this.label1 = new System.Windows.Forms.Label();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.picPreview = new System.Windows.Forms.PictureBox();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.picDepth = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.groupBox3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.picPreview)).BeginInit();
this.groupBox4.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.picDepth)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.btnZClear);
this.groupBox1.Controls.Add(this.btnCreatImgFile);
this.groupBox1.Controls.Add(this.btnWorking);
this.groupBox1.Location = new System.Drawing.Point(9, 10);
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(935, 70);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "操作";
//
// btnZClear
//
this.btnZClear.Location = new System.Drawing.Point(661, 24);
this.btnZClear.Name = "btnZClear";
this.btnZClear.Size = new System.Drawing.Size(75, 23);
this.btnZClear.TabIndex = 2;
this.btnZClear.Text = "Z Clear";
this.btnZClear.UseVisualStyleBackColor = true;
this.btnZClear.Click += new System.EventHandler(this.btnZClear_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(852, 22);
this.btnCreatImgFile.Margin = new System.Windows.Forms.Padding(2);
this.btnCreatImgFile.Name = "btnCreatImgFile";
this.btnCreatImgFile.Size = new System.Drawing.Size(69, 27);
this.btnCreatImgFile.TabIndex = 1;
this.btnCreatImgFile.Text = "创建图片";
this.btnCreatImgFile.UseVisualStyleBackColor = true;
this.btnCreatImgFile.Click += new System.EventHandler(this.btnCreatImgFile_Click);
//
// btnWorking
//
this.btnWorking.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnWorking.Location = new System.Drawing.Point(758, 22);
this.btnWorking.Margin = new System.Windows.Forms.Padding(2);
this.btnWorking.Name = "btnWorking";
this.btnWorking.Size = new System.Drawing.Size(67, 27);
this.btnWorking.TabIndex = 0;
this.btnWorking.Text = "开始";
this.btnWorking.UseVisualStyleBackColor = true;
this.btnWorking.Click += new System.EventHandler(this.btnWorking_Click);
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.groupBox2.Controls.Add(this.controlPanel);
this.groupBox2.Controls.Add(this.rdoMod2);
this.groupBox2.Controls.Add(this.rdoMod1);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Location = new System.Drawing.Point(9, 94);
this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Padding = new System.Windows.Forms.Padding(2);
this.groupBox2.Size = new System.Drawing.Size(295, 420);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "参数设置";
//
// controlPanel
//
this.controlPanel.Location = new System.Drawing.Point(4, 59);
this.controlPanel.Margin = new System.Windows.Forms.Padding(2);
this.controlPanel.Name = "controlPanel";
this.controlPanel.Size = new System.Drawing.Size(281, 350);
this.controlPanel.TabIndex = 3;
//
// rdoMod2
//
this.rdoMod2.AutoSize = true;
this.rdoMod2.Location = new System.Drawing.Point(164, 30);
this.rdoMod2.Margin = new System.Windows.Forms.Padding(2);
this.rdoMod2.Name = "rdoMod2";
this.rdoMod2.Size = new System.Drawing.Size(14, 13);
this.rdoMod2.TabIndex = 2;
this.rdoMod2.UseVisualStyleBackColor = true;
this.rdoMod2.Visible = false;
this.rdoMod2.CheckedChanged += new System.EventHandler(this.rdoMod2_CheckedChanged);
//
// rdoMod1
//
this.rdoMod1.AutoSize = true;
this.rdoMod1.Checked = true;
this.rdoMod1.Location = new System.Drawing.Point(90, 29);
this.rdoMod1.Margin = new System.Windows.Forms.Padding(2);
this.rdoMod1.Name = "rdoMod1";
this.rdoMod1.Size = new System.Drawing.Size(14, 13);
this.rdoMod1.TabIndex = 1;
this.rdoMod1.TabStop = true;
this.rdoMod1.UseVisualStyleBackColor = true;
this.rdoMod1.CheckedChanged += new System.EventHandler(this.rdoMod1_CheckedChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(16, 30);
this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 0;
//
// splitContainer1
//
this.splitContainer1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.splitContainer1.Location = new System.Drawing.Point(309, 91);
this.splitContainer1.Margin = new System.Windows.Forms.Padding(2);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.groupBox3);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.groupBox4);
this.splitContainer1.Size = new System.Drawing.Size(636, 427);
this.splitContainer1.SplitterDistance = 279;
this.splitContainer1.SplitterWidth = 3;
this.splitContainer1.TabIndex = 4;
//
// groupBox3
//
this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox3.Controls.Add(this.picPreview);
this.groupBox3.Location = new System.Drawing.Point(2, 2);
this.groupBox3.Margin = new System.Windows.Forms.Padding(2);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Padding = new System.Windows.Forms.Padding(2);
this.groupBox3.Size = new System.Drawing.Size(274, 422);
this.groupBox3.TabIndex = 3;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "预览";
//
// picPreview
//
this.picPreview.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picPreview.Location = new System.Drawing.Point(15, 59);
this.picPreview.Margin = new System.Windows.Forms.Padding(2);
this.picPreview.Name = "picPreview";
this.picPreview.Size = new System.Drawing.Size(247, 338);
this.picPreview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.picPreview.TabIndex = 0;
this.picPreview.TabStop = false;
//
// groupBox4
//
this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox4.Controls.Add(this.picDepth);
this.groupBox4.Location = new System.Drawing.Point(2, 2);
this.groupBox4.Margin = new System.Windows.Forms.Padding(2);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Padding = new System.Windows.Forms.Padding(2);
this.groupBox4.Size = new System.Drawing.Size(355, 422);
this.groupBox4.TabIndex = 4;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "自动景深扩展";
//
// picDepth
//
this.picDepth.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picDepth.Location = new System.Drawing.Point(13, 59);
this.picDepth.Margin = new System.Windows.Forms.Padding(2);
this.picDepth.Name = "picDepth";
this.picDepth.Size = new System.Drawing.Size(327, 338);
this.picDepth.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.picDepth.TabIndex = 1;
this.picDepth.TabStop = false;
//
// CameraAutomaticDepth
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(953, 523);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "CameraAutomaticDepth";
this.Text = "自动景深扩展";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CameraAutomaticDepth_FormClosing);
this.Load += new System.EventHandler(this.CameraAutomaticDepth_Load);
this.Controls.SetChildIndex(this.groupBox1, 0);
this.Controls.SetChildIndex(this.groupBox2, 0);
this.Controls.SetChildIndex(this.splitContainer1, 0);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.picPreview)).EndInit();
this.groupBox4.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.picDepth)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region 控件
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnCreatImgFile;
private System.Windows.Forms.Button btnWorking;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RadioButton rdoMod2;
private System.Windows.Forms.RadioButton rdoMod1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel controlPanel;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.PictureBox picPreview;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.PictureBox picDepth;
#endregion
private AppWorkspace m_appWorkspace;
// 已移动次数
private int m_moveTimes;
// 当前位置
private double m_currentStep => m_Stage.Z;
private ICamera m_camera => CameraManager.CurrentCamera;
private AxisController m_Stage;
private ParameterOneControl m_parameterOne;
private bool m_isWorking;
private Mat m_matClone;
private Mat[] Coll_Ori = new Mat[2];
private Mat m_merge;
private Button btnZClear;
public CameraAutomaticDepth(AppWorkspace appWorkspace)
{
this.m_appWorkspace = appWorkspace;
// this.DoubleBuffered = true;//设置本窗体
//SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
//SetStyle(ControlStyles.UserPaint, true);
//SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
//SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
InitializeComponent();
InitializeLanguageText();
}
///
/// 加载数据
///
public void InitializeData()
{
try
{
m_Stage = AxisController.GetInstance();
CameraManager.FrameCallback += CallbackDraw;
// 默认选中显示模式一
rdoMod1.Checked = true;
controlPanel.Controls.Clear();
m_parameterOne = new ParameterOneControl(m_Stage);
controlPanel.Controls.Add(m_parameterOne);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CameraAutomaticDepth_Load(object sender, EventArgs e)
{
InitializeData();
}
object obj = new object();
///
/// 绘制
///
///
///
public void CallbackDraw(Bitmap frame)
{
if (frame == null)
return;
m_matClone = PaintDotNet.Camera.Tools.ToMat(frame);
picPreview.Image = (Image)frame.Clone();
}
private void rdoMod1_CheckedChanged(object sender, EventArgs e)
{
controlPanel.Controls.Clear();
ParameterOneControl parameterOne = new ParameterOneControl(m_Stage);
controlPanel.Controls.Add(parameterOne);
}
private void rdoMod2_CheckedChanged(object sender, EventArgs e)
{
controlPanel.Controls.Clear();
ParameterTwoControl parameterTwo = new ParameterTwoControl();
controlPanel.Controls.Add(parameterTwo);
}
private void CameraAutomaticDepth_FormClosing(object sender, FormClosingEventArgs e)
{
CameraManager.FrameCallback -= CallbackDraw;
}
private void btnWorking_Click(object sender, EventArgs e)
{
m_merge = null;
try
{
if (!m_camera.IsOpen())
{
MessageBox.Show(PdnResources.GetString("Menu.merafoundpleaseconfirthatthecameraisconne.Text"));
return;
}
if (m_isWorking)
{
StopWorking();
return;
}
if (m_parameterOne.StopPos == 0 || m_parameterOne.StartPos == 0)
{
MessageBox.Show(PdnResources.GetString("Menu.Pleasesetthestartandstoppositions2.Text"));
return;
}
// 重新自动计算,防止没有手动点击计算
m_parameterOne.Calculate();
var zscanparm = m_parameterOne.ZScanParameter;
m_Stage.ZScan(zscanparm.Start, zscanparm.Track, zscanparm.Times, Shooting, null, StopWorking);
m_isWorking = true;
btnWorking.Text = PdnResources.GetString("Menu.stop.text");
m_moveTimes = 0;
// 禁用控件
m_parameterOne.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void StopWorking()
{
m_Stage.FreeZ();
m_isWorking = false;
this.Invoke(new Action(() =>
{
// 更改控件状态
m_parameterOne.Enabled = true;
btnWorking.Text = PdnResources.GetString("Menu.Started.text");
}));
}
int m_captureWait = 200;
private void Shooting()
{
Thread.Sleep(m_captureWait);
// 修改拍摄高度
this.Invoke(new Action(() =>
{
m_parameterOne.updatelblHeight(m_currentStep);
}));
if (m_merge == null)
{
m_merge = m_matClone;
}
else
{
Coll_Ori[1] = m_matClone;
Coll_Ori[0] = m_merge;
m_merge = Merge.GetMergeMatForCamera(Coll_Ori);
}
picDepth.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(m_merge);
GC.Collect();
++m_moveTimes;
if (m_moveTimes == m_parameterOne.Times)
{
// StopWorking();
return;
}
}
#region 等待机制
bool _waitFlag = false;
private void ResetWait()
{ _waitFlag = false; }
private void Wait()
{
_waitFlag = true;
while (_waitFlag)
{
Thread.Sleep(100);
}
}
#endregion
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();
Document document = Document.FromImageMat(m_merge.Clone());
dw.Document = document;
dw.xmlSaveModel = Startup.instance.ruleDB;
dw.InitRulerInfo();
m_appWorkspace.ActiveDocumentWorkspace = dw;
}
#region StageEvents
public void OnUpdatePosition()
{
try //可能出现窗口销毁后回调
{
if (!this.IsDisposed)
this.Invoke(new Action(() =>
{
m_parameterOne.PositionZ = m_Stage.Z;
}));
}
catch { }
}
public void OnTimeoutConnect()
{
StopWorking();
MessageBox.Show(PdnResources.GetString("Menu.Theconsoleresponsetimeout.Text"));
}
public void OnErrorSend()
{
StopWorking();
MessageBox.Show(PdnResources.GetString("Menu.Replydataarsingerror.Text"));
}
#endregion
private void btnZClear_Click(object sender, EventArgs e)
{
m_Stage.ClearPosZ();
}
}
}