using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using PaintDotNet.Base.SettingModel; using TUCamera; using TUCAMAPI; using System.Runtime.InteropServices; namespace PaintDotNet.ImageCollect.CameraComponent { public partial class FrameCameraSetControl : UserControl { private TUCamera.TUCamera m_camera; private CameraParamModel m_cameraParamModel; private bool m_use; private Point m_startP; //鼠标按下时坐标 private bool m_blnDraw = false; private Rectangle m_rect;//初始设置一个拖拽的显示框 private SolidBrush m_brush = new SolidBrush(Color.Black); private int m_handle = 0; private int m_targetX = 0; // 起始点X坐标 对应分辨率 private int m_targetY = 0; // 起始点Y坐标 对应分辨率 private int m_targetWidth = 320; //显示框的宽度 对应分辨率 private int m_targetHeight = 200; //显示框的高度 对应分辨率 private int m_tempX = 0; private int m_tempY = 0; private int m_tempW = 0; private int m_tempH = 0; private TUCameraIFrameProcess m_process = null; private Size m_resolution; // 分辨率 private bool m_isWaiting; private void InitializeLanguageText() { this.groupBox3.Text = PdnResources.GetString("Menu.Setting.Text"); this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text"); this.zhongxinButton.Text = PdnResources.GetString("Menu.center.text"); this.label4.Text = PdnResources.GetString("Menu.Imagesize.text") + ":"; this.label1.Text = PdnResources.GetString("Menu.Startingcoordinates.text") + ":"; this.shuaxinButton.Text = PdnResources.GetString("Menu.Refreshthumbnail.text"); this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text"); } public FrameCameraSetControl(CameraParamModel model, bool use) { InitializeComponent(); InitializeLanguageText(); m_use = use; m_camera = TUCameraManager.GetInstance().GetCurrentCamera(); m_cameraParamModel = model; InitializeControlData(); //InitializeData(); } public void ReLoad(CameraParamModel model, bool use) { m_cameraParamModel = model; m_use = use; InitializeControlData(); InitializeData(); } private void InitializeControlData() { cmbResolution.Items.Clear(); List resolutions = this.m_camera.GetResolutionRange(); for (int i = 0; i < resolutions.Count; i++) { cmbResolution.Items.Add(resolutions[i]); } if (resolutions.Count > 0) { cmbResolution.SelectedIndex = m_cameraParamModel.parame.Resolution; } // this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString(); this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString(); this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString(); this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString(); } /// /// 绑定样式数据 /// private void InitializeData() { m_targetX = this.m_cameraParamModel.parame.previewRectX; m_targetY = this.m_cameraParamModel.parame.previewRectY; m_targetWidth = this.m_cameraParamModel.parame.previewRectW; m_targetHeight = this.m_cameraParamModel.parame.previewRectH; SetROI(); m_rect = new Rectangle(); drawingImg(); if (m_resolution.Width > 0 && m_resolution.Height > 0) { this.pictureBox1.Height = (this.pictureBox1.Width * m_resolution.Height) / m_resolution.Width; m_rect.X = this.m_cameraParamModel.parame.previewRectX * this.pictureBox1.Width / m_resolution.Width; m_rect.Y = this.m_cameraParamModel.parame.previewRectY * this.pictureBox1.Height / m_resolution.Height; m_rect.Width = this.m_cameraParamModel.parame.previewRectW * this.pictureBox1.Width / m_resolution.Width; m_rect.Height = this.m_cameraParamModel.parame.previewRectH * this.pictureBox1.Height / m_resolution.Height; } else { m_resolution.Width = pictureBox1.Width; m_resolution.Height = pictureBox1.Height; m_rect.X = 0; m_rect.Y = 0; m_rect.Width = this.pictureBox1.Width; m_rect.Height = this.pictureBox1.Height; } } private void zhongxinButton_Click(object sender, EventArgs e) { if (zhongxinButton.Text.Equals(PdnResources.GetString("Menu.center.text"))) { zhongxinButton.Text = PdnResources.GetString("Menu.Start.text"); int X = pictureBox1.Width / 2 - m_rect.Width / 2; int Y = pictureBox1.Height / 2 - m_rect.Height / 2; m_rect.X = X; m_rect.Y = Y; m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width; m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height; } else { zhongxinButton.Text = PdnResources.GetString("Menu.center.text"); int X = 0; int Y = 0; m_rect.X = X; m_rect.Y = Y; m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width; m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height; } SetROI(); this.pictureBox1.Refresh(); } private void quanfuButton_Click(object sender, EventArgs e) { if (this.quanfuButton.Text.Equals(PdnResources.GetString("Menu.Lasttime.text"))) { this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text"); if (m_resolution.Width>0 && m_resolution.Height>0) { m_rect.X = m_tempX * this.pictureBox1.Width / m_resolution.Width; m_rect.Y = m_tempY * this.pictureBox1.Height / m_resolution.Height; m_rect.Width = m_tempW * this.pictureBox1.Width / m_resolution.Width; m_rect.Height = m_tempH * this.pictureBox1.Height / m_resolution.Height; m_targetX = m_tempX; m_targetY = m_tempY; m_targetWidth = m_tempW; m_targetHeight = m_tempH; } } else { this.quanfuButton.Text = PdnResources.GetString("Menu.Lasttime.text"); m_tempX = m_targetX; m_tempY = m_targetY; m_tempW = m_targetWidth; m_tempH = m_targetHeight; int X = 0; int Y = 0; m_rect.X = X; m_rect.Y = Y; m_rect.Width = this.pictureBox1.Width; m_rect.Height = this.pictureBox1.Height; m_targetX = 0; m_targetY = 0; m_targetWidth = m_resolution.Width; m_targetHeight = m_resolution.Height; } SetROI(); this.pictureBox1.Refresh(); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { m_handle = HitTest(e.Location); if (m_handle < 0) { if (m_rect.Contains(e.Location))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框 { m_handle = 9; } } this.Cursor = GetHandleCursor(m_handle); m_startP = e.Location; Invalidate(); m_blnDraw = true; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (m_handle > 0 && m_blnDraw) { if (e.Button != MouseButtons.Left)//判断是否按下左键 return; MoveHandleTo(e.Location, m_handle); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { m_blnDraw = false; //结束绘制 this.Cursor = Cursors.Default; SetROI(); this.pictureBox1.Refresh(); } public Rectangle GetHandleRectangle(int handleNumber) { Point point = GetHandle(handleNumber); return new Rectangle(point.X - 4, point.Y - 4, 8, 8); } public int HitTest(Point point) { for (int i = 1; i <= 8; i++) { if (GetHandleRectangle(i).Contains(point)) return i; } return -1; } public Point GetHandle(int handleNumber) { int x, y, xCenter, yCenter; xCenter = m_rect.X + m_rect.Width / 2; yCenter = m_rect.Y + m_rect.Height / 2; x = m_rect.X; y = m_rect.Y; switch (handleNumber) { case 1: x = m_rect.X; y = m_rect.Y; break; case 2: x = xCenter; y = m_rect.Y; break; case 3: x = m_rect.Right; y = m_rect.Y; break; case 4: x = m_rect.Right; y = yCenter; break; case 5: x = m_rect.Right; y = m_rect.Bottom; break; case 6: x = xCenter; y = m_rect.Bottom; break; case 7: x = m_rect.X; y = m_rect.Bottom; break; case 8: x = m_rect.X; y = yCenter; break; } return new Point(x, y); } public Cursor GetHandleCursor(int handleNumber) { switch (handleNumber) { case 1: return Cursors.SizeNWSE; case 2: return Cursors.SizeNS; case 3: return Cursors.SizeNESW; case 4: return Cursors.SizeWE; case 5: return Cursors.SizeNWSE; case 6: return Cursors.SizeNS; case 7: return Cursors.SizeNESW; case 8: return Cursors.SizeWE; default: return Cursors.Default; } } public void MoveHandleTo(Point point, int handleNumber) { if (point.X > pictureBox1.Width) { point.X = pictureBox1.Width; } if (point.X < 0) { point.X = 0; } if (point.Y < 0) { point.Y = 0; } if (point.Y > pictureBox1.Height) { point.Y = pictureBox1.Height; } int left = m_rect.Left; int top = m_rect.Top; int right = m_rect.Right; int bottom = m_rect.Bottom; switch (handleNumber) { case 1: left = point.X; top = point.Y; break; case 2: top = point.Y; break; case 3: right = point.X; top = point.Y; break; case 4: right = point.X; break; case 5: right = point.X; bottom = point.Y; break; case 6: bottom = point.Y; break; case 7: left = point.X; bottom = point.Y; break; case 8: left = point.X; break; case 9: left = m_rect.Left +(point.X - m_startP.X); top = m_rect.Top + (point.Y - m_startP.Y); //判断是否超过左上角 if (left < 0) left = 0; if (top < 0) top = 0; //判断是否超过右下 角 if (left > (pictureBox1.Width - m_rect.Width)) left = pictureBox1.Width - m_rect.Width; if (top > (pictureBox1.Height - m_rect.Height)) top = pictureBox1.Height - m_rect.Height; right = left + m_rect.Width; bottom = top + m_rect.Height; break; } m_rect.X = left; m_rect.Y = top; m_rect.Width = right - left; m_rect.Height = bottom - top; m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width; m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height; m_targetWidth = m_rect.Width * m_resolution.Width / this.pictureBox1.Width; m_targetHeight = m_rect.Height * m_resolution.Height / this.pictureBox1.Height; pictureBox1.Refresh(); m_startP = point; } private void drawingImg() { if (!m_camera.IsOpen()) { return; } m_isWaiting = true; int resolution_width = pictureBox1.Width; int resolution_height = pictureBox1.Height; Console.WriteLine("点击刷新缩略图"); if (m_camera.IsOpen()) { m_camera.m_drawAllHandler += new DrawAllHandler(CallbackDraw); m_camera.StartWaitForFrame(); m_process = m_camera.StartDrawing(ref resolution_width, ref resolution_height); } m_resolution.Width = resolution_width; m_resolution.Height = resolution_height; if (resolution_width == 0 || resolution_height == 0) { m_resolution.Width = pictureBox1.Width; m_resolution.Height = pictureBox1.Height; } } private byte[] m_pbuf; /// /// 绘制 /// /// /// public void CallbackDraw(byte[] pBuf, object obj, int channel) { lock (obj) { try { m_isWaiting = false; Console.WriteLine("接收全图buf"); m_camera.m_drawAllHandler -= new DrawAllHandler(CallbackDraw); OpenCvSharp.Mat mat; if (channel == 1) { mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC1, pBuf); } else { mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC3, pBuf); } OpenCvSharp.Cv2.Flip(mat, mat, 0); this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat); stopDrawing(); if (mat != null) { mat.Dispose(); } GC.Collect(); } catch (Exception) { } } } public void CallMaxDraw(Bitmap bitmap) { m_camera.m_bufHandler -= CallMaxDraw; this.pictureBox1.Image = bitmap; } private void stopDrawing() { //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法 Action invokeAction = new Action(stopDrawing); //判断操作控件的线程是否创建控件的线程 //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法 if (this.InvokeRequired) { //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用) this.Invoke(invokeAction); } else { m_camera.StopDrawing(m_process); m_camera.StopWaitForFrame(); //窗体线程,即主线程 shuaxinButton.Enabled = true; } } private void shuaxinButton_Click(object sender, EventArgs e) { drawingImg(); shuaxinButton.Enabled = false; } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (m_isWaiting) { return; } if (m_rect.Width != pictureBox1.Width || m_rect.Height != pictureBox1.Height) { this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text"); } this.m_cameraParamModel.parame.previewRectX = m_targetX; this.m_cameraParamModel.parame.previewRectY = m_targetY; this.m_cameraParamModel.parame.previewRectW = m_targetWidth; this.m_cameraParamModel.parame.previewRectH = m_targetHeight; if (m_resolution.Width != 0 && m_resolution.Height != 0) { if (m_rect.Width > 0 && m_rect.Height > 0) { e.Graphics.DrawRectangle(new Pen(Color.Red, 2), m_rect);//重新绘制颜色为红色 for (int i = 1; i <= 8; i++) { e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i)); } } } this.zuobiaoXTextBox.Text = m_targetX.ToString(); this.zuobiaoYTextBox.Text = m_targetY.ToString(); this.chicunWTextBox.Text = m_targetWidth.ToString(); this.chicunHTextBox.Text = m_targetHeight.ToString(); } private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { int val = Convert.ToInt32(this.zuobiaoXTextBox.Text); if (val < 0) { this.zuobiaoXTextBox.Text = "0"; } else if ((val + m_targetWidth) > m_resolution.Width) { zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString(); } TextChange(); } if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void zuobiaoYTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { int val = Convert.ToInt32(this.zuobiaoYTextBox.Text); if (val < 0) { this.zuobiaoYTextBox.Text = "0"; } else if ((val + m_targetHeight) > m_resolution.Height) { this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString(); } TextChange(); } if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void chicunWTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { int val = Convert.ToInt32(this.chicunWTextBox.Text); if (val < 0) { this.chicunWTextBox.Text = "1"; } else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width) { this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString(); } TextChange(); } if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void chicunHTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { int val = Convert.ToInt32(this.chicunHTextBox.Text); if (val < 0) { this.chicunHTextBox.Text = "1"; } else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height) { this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString(); } TextChange(); } if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void TextChange() { m_targetX = Convert.ToInt32(this.zuobiaoXTextBox.Text); m_targetY = Convert.ToInt32(this.zuobiaoYTextBox.Text); m_targetWidth = Convert.ToInt32(this.chicunWTextBox.Text); m_targetHeight = Convert.ToInt32(this.chicunHTextBox.Text); if (m_resolution.Width > 0 && m_resolution.Height > 0) { m_rect.X = m_targetX * this.pictureBox1.Width / m_resolution.Width; m_rect.Y = m_targetY * this.pictureBox1.Height / m_resolution.Height; m_rect.Width = m_targetWidth * this.pictureBox1.Width / m_resolution.Width; m_rect.Height = m_targetHeight * this.pictureBox1.Height / m_resolution.Height; } else { m_rect.X = m_targetX; m_rect.Y = m_targetY; m_rect.Width = m_targetWidth; m_rect.Height = m_targetHeight; } SetROI(); this.pictureBox1.Refresh(); } private void SetROI() { if (m_use) { m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight); this.m_cameraParamModel.parame.previewRectX = m_targetX; this.m_cameraParamModel.parame.previewRectY = m_targetY; this.m_cameraParamModel.parame.previewRectW = m_targetWidth; this.m_cameraParamModel.parame.previewRectH = m_targetHeight; } } private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e) { m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex; int resolutionId = m_camera.GetResolutionId(); if (resolutionId != m_cameraParamModel.parame.Resolution) { // 分辨率 m_camera.SetResolution(m_cameraParamModel.parame.Resolution); //m_camera.m_bufHandler += new BufHandler(CallMaxDraw); } m_camera.GetROI(ref m_cameraParamModel.parame.previewRectX, ref m_cameraParamModel.parame.previewRectY, ref m_cameraParamModel.parame.previewRectW, ref m_cameraParamModel.parame.previewRectH); InitializeData(); this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString(); this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString(); this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString(); this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString(); } private void chicunHTextBox_Leave(object sender, EventArgs e) { int val = Convert.ToInt32(this.chicunHTextBox.Text); if (val < 0) { this.chicunHTextBox.Text = "1"; } else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height) { this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString(); } TextChange(); } private void chicunWTextBox_Leave(object sender, EventArgs e) { int val = Convert.ToInt32(this.chicunWTextBox.Text); if (val < 0) { this.chicunWTextBox.Text = "1"; } else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width) { this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString(); } TextChange(); } private void zuobiaoYTextBox_Leave(object sender, EventArgs e) { int val = Convert.ToInt32(this.zuobiaoYTextBox.Text); if (val < 0) { this.zuobiaoYTextBox.Text = "0"; } else if ((val + m_targetHeight) > m_resolution.Height) { this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString(); } TextChange(); } private void zuobiaoXTextBox_Leave(object sender, EventArgs e) { int val = Convert.ToInt32(this.zuobiaoXTextBox.Text); if (val < 0) { this.zuobiaoXTextBox.Text = "0"; } else if ((val + m_targetWidth) > m_resolution.Width) { zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString(); } TextChange(); } } }