|
- using Google.Protobuf.Collections;
- using HalconDotNet;
- using ImageToolKits;
- using MaiMuAOI.SysCtrl;
- using MaiMuControl.Device;
- using MaiMuControl.Device.AxisDev;
- using MaiMuControl.Device.CamDev;
- using MaiMuControl.Device.LightDev;
- using OpenCvSharp.Flann;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using System.Windows.Forms;
-
- namespace MaiMuAOI.SysUI.DefectPicShow
- {
- public partial class ImageShowFrm : Form
- {
- private Control.ControlCollection picBoxList;
- private int picIndex;
- private int tagIndex;
- private double Xmm, Ymm;
- private Bitmap currBmp;
-
- private bool IsMove;
- public ImageShowFrm(Control.ControlCollection picBoxColl, int index, bool isDev = false)
- {
- InitializeComponent();
- UIStyle.SetUIStyle(this);
- this.uiTitel1.FatherForm = this;
-
- this.picBoxList = picBoxColl;
- this.picIndex = index;
-
- logsts.BackColor = Color.White;
- logsts.ForeColor = Color.Black;
-
- tsStateStep.BackColor = Color.White;
- tsStateStep.ForeColor = Color.Black;
- IsMove = isDev;
-
- }
-
- private void ImageShowFrm_Shown(object sender, EventArgs e)
- {
- showPic(this.picIndex);
- }
-
- private void tsbtnPre_Click(object sender, EventArgs e)
- {
- showPic(--this.picIndex);
- }
-
- private void tsbtnNext_Click(object sender, EventArgs e)
- {
- showPic(++this.picIndex);
- }
-
- private void ImageShowFrm_Load(object sender, EventArgs e)
- {
- if (!IsMove)
- {
- imageBox2.Visible = false;
- imageBox1.Top = 0;
- imageBox1.Left = this.panel1.Left;
- imageBox1.Width = this.panel1.Width;
- imageBox1.Height = this.panel1.Height;
- tsbDebug.Visible = false;
- }
- else
- {
- tsbDebug.Visible=true;
- imageBox2.Visible = true;
- imageBox1.Top = 0;
- imageBox1.Left = this.panel1.Left;
- imageBox1.Width = this.panel1.Width / 2 - 5;
- imageBox1.Height = this.panel1.Height;
-
- imageBox2.Top = 0;
- imageBox2.Left = this.imageBox1.Width + 10;
- imageBox2.Width = this.panel1.Width / 2 - 5;
- imageBox2.Height = this.panel1.Height;
- }
-
- PictureBox picBox = picBoxList[this.picIndex] as PictureBox;
- this.ActiveControl = this.imageBox1; // 设置焦点
- this.logsts.Text = "";
- this.Refresh();
- }
-
- private void FrmPhoto_KeyDown(object sender, KeyEventArgs e)
- {
- switch (e.KeyCode)
- {
- case Keys.Up:
- case Keys.Left:
- if (!this.tsbtnPre.Enabled) return;
- tsbtnPre_Click(null, null);
- break;
- case Keys.Down:
- case Keys.Right:
- if (!this.tsbtnNext.Enabled) return;
- tsbtnNext_Click(null, null);
- break;
- }
- }
-
- private void tsbtnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private void showPic(int index)
- {
- try
- {
- PictureBox picBox = picBoxList[index] as PictureBox;
- string[] tags = picBox.Tag.ToString().Split(',');
- this.tagIndex = Convert.ToInt32(tags[0]);
- this.Xmm = Convert.ToDouble(tags[1]);
- this.Ymm = Convert.ToDouble(tags[2]);
- //tsStateMsg.Text = $"索引:{tagIndex}, X:{Xmm}mm, Y:{Ymm}mm";
- tsStateStep.Text = $"第{index + 1}/{picBoxList.Count}张";
- currBmp = (Bitmap)picBox.Image;
- OpenCvSharp.Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(currBmp);//用bitmap转换为mat
- this.imageBox1.RefreshWindow(mat);
-
- //
- this.tsbtnPre.Enabled = (index > 0);
- this.tsbtnNext.Enabled = (index < picBoxList.Count - 1);
-
- if (IsMove)
- moveAxis();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- private void ImageShowFrm_Resize(object sender, EventArgs e)
- {
- if (!IsMove)
- {
- imageBox2.Visible = false;
- imageBox1.Top = 0;
- imageBox1.Left = this.panel1.Left;
- imageBox1.Width = this.panel1.Width;
- imageBox1.Height = this.panel1.Height;
- }
- else
- {
- imageBox2.Visible = true;
- imageBox1.Top = 0;
- imageBox1.Left = this.panel1.Left;
- imageBox1.Width = this.panel1.Width / 2 - 5;
- imageBox1.Height = this.panel1.Height;
-
- imageBox2.Top = 0;
- imageBox2.Left = this.imageBox1.Width + 10;
- imageBox2.Width = this.panel1.Width / 2 - 5;
- imageBox2.Height = this.panel1.Height;
- }
- this.Refresh();
- }
-
- DebugTestFrm frm;
- private void tsbDebug_Click(object sender, EventArgs e)
- {
- if ((frm == null) || (frm.IsDisposed))
- frm = new DebugTestFrm();
- //frm.TopMost = true;
- frm.Show();
- }
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- SysMgr.Instance.CamDevFront.ClearImageQueue();
- Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 600);
- if (acq.GrabStatus != "GrabPass")
- {
- //MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- return;
- }
- if (imageBox2.Image == null)
- imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Zoom);//可以不显示区域
- else
- imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image), ImageBox.ImageModeEnum.Part);//可以不显示区域
- //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
- }
-
- private void ImageShowFrm_FormClosing(object sender, FormClosingEventArgs e)
- {
- this.timer1.Enabled = false;
- }
-
- //移动电机拍照
- private void moveAxis()
- {
- this.tsbtnPre.Enabled = this.tsbtnNext.Enabled = false;
- this.logsts.Text = " 正在移动相机...";
-
- // 开启光源
- SysMgr.Instance.LightDev.OpenLight((int)ConfMgr.Instance.SysConfigParams.LightCH);
- SysMgr.Instance.LightDev.SetLightDigitalValue((int)ConfMgr.Instance.SysConfigParams.LightCH, ConfMgr.Instance.SysConfigParams.LightLut);
-
- //变焦
- SysMgr.Instance.LensMotorDev.MoveAbsPulse(0, new VelocityCurveParams(), (int)SmallAxCmdPos.倍率4_5X);
- //判断到位
- SysMgr.Instance.LensMotorDev.CheckDone(0, 10000);
- // 移动电机
- if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60)<0 )||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
- {
- MessageBox.Show("轴状态为不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- return;
- }
- var list = SysMgr.Instance.DefectLib.viewTag(tagIndex, Xmm, Ymm, currBmp.Width, currBmp.Height);
-
- double val = SysMgr.Instance.DefaultSpeed;
- int st = SysMgr.Instance.MotionST;
-
- this.logsts.Text = $" 图像信息{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
- //Z移动
- VelocityCurveParams Velocity0 = new VelocityCurveParams(100, 100, 0, 10, 2, SysMgr.Instance.GetMMtoPlus(AxisName.Axis3));
- SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis3, Velocity0, ConfMgr.Instance.SysConfigParams.LensAxisZPos.Z_4_5X);
- //XY移动
- VelocityCurveParams Velocity1 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis0));
- VelocityCurveParams Velocity2 = new VelocityCurveParams(val, val, 0, val, st, SysMgr.Instance.GetMMtoPlus(AxisName.Axis2));
- SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis0, Velocity1, list[0]);
- SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis2, Velocity2, list[1]);
-
- this.logsts.Text = "";
- this.tsbtnPre.Enabled = (picIndex > 0);
- this.tsbtnNext.Enabled = (picIndex < picBoxList.Count - 1);
-
- if ((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60) < 0) ||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0) ||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis3, 60) < 0))
- {
- MessageBox.Show("到位失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- return;
- }
- Thread.Sleep(300);
- //拍照
- SysMgr.Instance.CamDevFront.SetExposure(ConfMgr.Instance.SysConfigParams.CamExposureTime);
- this.logsts.Text = $" 拍照{tagIndex}-{Xmm}-{Ymm}-{currBmp.Width}-{currBmp.Height},正在移动相机{list[0]}-{list[1]}";
- this.timer1.Enabled = true;
- //SysMgr.Instance.CamDevFront.ClearImageQueue();
- //Acquisition acq = SysMgr.Instance.CamDevFront.Snap(1, 6000);
- //if (acq.GrabStatus != "GrabPass")
- //{
- // MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- // return;
- //}
- //imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
- }
- }
- }
|