|
- using HalconDotNet;
- using ImageToolKits;
- using MaiMuAOI.SysCtrl;
- using MaiMuControl.Device;
- using MaiMuControl.Device.AxisDev;
- using MaiMuControl.Device.CamDev;
- 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;
-
- 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;
- }
- 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;
- }
-
- 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();
- }
-
- //移动电机拍照
- private void moveAxis()
- {
- this.tsbtnPre.Enabled = this.tsbtnNext.Enabled = false;
- this.logsts.Text = " 正在移动相机...";
-
- // 开启光源
- SysMgr.Instance.LightDev.OpenLight((int)LightChannelEnum.CH1);
- // 移动电机
- if((SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis0, 60)<0 )||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis1, 60) < 0) ||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 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;
- 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.Axis1));
- SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis0, Velocity1, list[0]);
- SysMgr.Instance.AxisDev.MoveAbsValue((int)AxisName.Axis1, 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.Axis1, 60) < 0) ||
- (SysMgr.Instance.AxisDev.CheckDone((int)AxisName.Axis2, 60) < 0))
- {
- MessageBox.Show("到位失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- return;
- }
- Thread.Sleep(300);
- //拍照
- this.logsts.Text = " 拍照...";
- Acquisition acq = SysMgr.Instance.CamDevBack.Snap(1, 6000);
- if (acq.GrabStatus != "GrabPass")
- {
- MessageBox.Show("拍照失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
- return;
- }
- imageBox2.RefreshWindow(CamDev.HImageToMat(acq.Image));
- }
- }
- }
|