diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs index ca6fd53..c0918c3 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs @@ -19,6 +19,9 @@ namespace Camera // 定时器 private System.Timers.Timer _timer; + // 当前图像 + private Image _currentImage; + // 事件定义 public event EventHandler ImageCaptured; @@ -29,8 +32,6 @@ namespace Camera /// 对应区域的颜色 public Color GetColor(int AreaNum) { - // 这里可以根据实际需求实现颜色获取逻辑 - // 暂时返回一个默认颜色 switch (AreaNum) { case 1: @@ -44,15 +45,21 @@ namespace Camera } } + /// + /// 获取当前图像 + /// + public Image GetCurrentImage() + { + return _currentImage; + } + /// /// 开始获取图像 /// public void Start() { - // 停止现有的定时器 Stop(); - // 创建新的定时器,设置为300ms触发一次,关闭自动重置 _timer = new System.Timers.Timer(300); _timer.AutoReset = false; _timer.Elapsed += Timer_Elapsed; @@ -72,6 +79,19 @@ namespace Camera } } + /// + /// 打开设置窗口 + /// + public void SetArea() + { + Setting settingForm = new Setting(); + if (_currentImage != null) + { + settingForm.SetImage(_currentImage); + } + settingForm.ShowDialog(); + } + /// /// 定时器触发事件 /// @@ -79,22 +99,23 @@ namespace Camera { try { - // 获取图像 Image image = GetCameraImage(); if (image != null) { - // 发布事件 + if (_currentImage != null) + { + _currentImage.Dispose(); + } + _currentImage = image; OnImageCaptured(new ImageEventArgs { Image = image }); } } catch (Exception ex) { - // 忽略错误,继续下一次获取 Console.WriteLine("获取图像失败: " + ex.Message); } finally { - // 重新启动定时器 if (_timer != null && _timer.Enabled == false) { _timer.Start(); @@ -107,37 +128,29 @@ namespace Camera /// private Image GetCameraImage() { - // 构建请求URL string url = "http://" + IP + SNAPSHOT_URI; - // 创建Web请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; HttpWebResponse response = null; try { - // 发送请求,获取响应 response = (HttpWebResponse)request.GetResponse(); } catch (System.Net.WebException ex) { - // 检查是否是401未授权错误 if (ex.Response != null && ((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Unauthorized) { - // 获取鉴权信息 response = (HttpWebResponse)ex.Response; string authHeader = response.Headers["WWW-Authenticate"]; response.Close(); - // 提取realm和nonce string realm = ExtractAuthParam(authHeader, "realm"); string nonce = ExtractAuthParam(authHeader, "nonce"); - // 生成鉴权信息 string authorization = GenerateDigestAuthorization(USER, PASSWD, realm, nonce, "GET", SNAPSHOT_URI); - // 重新发送请求,带上鉴权信息 request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Headers.Add("Authorization", authorization); @@ -145,15 +158,12 @@ namespace Camera } else { - // 其他Web异常,抛出 throw; } } - // 读取响应流中的图片数据 using (Stream stream = response.GetResponseStream()) { - // 将流转换为Image对象 Image image = Image.FromStream(stream); response.Close(); return image; @@ -179,14 +189,10 @@ namespace Camera /// private string GenerateDigestAuthorization(string username, string password, string realm, string nonce, string method, string uri) { - // 计算HA1 = MD5(username:realm:password) string ha1 = CalculateMD5(username + ":" + realm + ":" + password); - // 计算HA2 = MD5(method:uri) string ha2 = CalculateMD5(method + ":" + uri); - // 计算response = MD5(ha1:nonce:ha2) string response = CalculateMD5(ha1 + ":" + nonce + ":" + ha2); - // 构建Authorization头 return string.Format("Digest username=\"{0}\", realm=\"{1}\", nonce=\"{2}\", uri=\"{3}\", response=\"{4}\"", username, realm, nonce, uri, response); } @@ -201,7 +207,6 @@ namespace Camera byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); byte[] hashBytes = md5.ComputeHash(inputBytes); - // 将字节数组转换为十六进制字符串 System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < hashBytes.Length; i++) { @@ -228,9 +233,6 @@ namespace Camera /// public class ImageEventArgs : EventArgs { - /// - /// 捕获的图像 - /// public Image Image { get; set; } } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index 679e5cc..154adad 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -2,15 +2,8 @@ namespace Camera { partial class Setting { - /// - /// 必需的设计器变量。 - /// private System.ComponentModel.IContainer components = null; - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) @@ -22,23 +15,25 @@ namespace Camera #region Windows 窗体设计器生成的代码 - /// - /// 设计器支持所需的方法 - 不要修改 - /// 使用代码编辑器修改此方法的内容。 - /// private void InitializeComponent() { this.splitContainer1 = new System.Windows.Forms.SplitContainer(); - this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.picBoxCamera = new System.Windows.Forms.PictureBox(); + this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); - this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); + this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.listBox1 = new System.Windows.Forms.ListBox(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit(); + this.splitContainer1.Panel1.SuspendLayout(); + this.splitContainer1.Panel2.SuspendLayout(); + this.splitContainer1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picBoxCamera)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.toolStrip1)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit(); + this.splitContainer2.Panel1.SuspendLayout(); + this.splitContainer2.Panel2.SuspendLayout(); + this.splitContainer2.SuspendLayout(); + this.toolStrip1.SuspendLayout(); this.SuspendLayout(); // // splitContainer1 @@ -46,96 +41,118 @@ namespace Camera this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 0); this.splitContainer1.Name = "splitContainer1"; - // 左侧面板宽度 - this.splitContainer1.Panel1MinSize = 300; - // 右侧面板宽度 + // + // splitContainer1.Panel1 + // + this.splitContainer1.Panel1.Controls.Add(this.picBoxCamera); + this.splitContainer1.Panel1MinSize = 0; + // + // splitContainer1.Panel2 + // + this.splitContainer1.Panel2.Controls.Add(this.splitContainer2); this.splitContainer1.Panel2MinSize = 200; this.splitContainer1.Size = new System.Drawing.Size(850, 469); - this.splitContainer1.SplitterDistance = 500; + this.splitContainer1.SplitterDistance = 100; this.splitContainer1.TabIndex = 0; // // picBoxCamera // - this.picBoxCamera = new System.Windows.Forms.PictureBox(); + this.picBoxCamera.BackColor = System.Drawing.Color.Gray; this.picBoxCamera.Dock = System.Windows.Forms.DockStyle.Fill; this.picBoxCamera.Location = new System.Drawing.Point(0, 0); this.picBoxCamera.Name = "picBoxCamera"; - this.picBoxCamera.Size = new System.Drawing.Size(500, 469); + this.picBoxCamera.Size = new System.Drawing.Size(100, 469); + this.picBoxCamera.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.picBoxCamera.TabIndex = 0; this.picBoxCamera.TabStop = false; - this.splitContainer1.Panel1.Controls.Add(this.picBoxCamera); + this.picBoxCamera.Paint += new System.Windows.Forms.PaintEventHandler(this.PicBoxCamera_Paint); + this.picBoxCamera.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseDown); + this.picBoxCamera.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseMove); + this.picBoxCamera.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PicBoxCamera_MouseUp); // // splitContainer2 // - this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; + this.splitContainer2.IsSplitterFixed = false; this.splitContainer2.Location = new System.Drawing.Point(0, 0); this.splitContainer2.Name = "splitContainer2"; this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal; - // 上方面板高度 - this.splitContainer2.Panel1MinSize = 50; - // 下方面板高度 + // + // splitContainer2.Panel1 + // + this.splitContainer2.Panel1.Controls.Add(this.toolStrip1); + this.splitContainer2.Panel1MinSize = 40; + // + // splitContainer2.Panel2 + // + this.splitContainer2.Panel2.Controls.Add(this.listBox1); this.splitContainer2.Panel2MinSize = 100; - this.splitContainer2.Size = new System.Drawing.Size(346, 469); - this.splitContainer2.SplitterDistance = 100; + this.splitContainer2.Size = new System.Drawing.Size(746, 469); this.splitContainer2.TabIndex = 0; // // toolStrip1 // - this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Fill; + this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.toolStripButton2, + this.toolStripButton1}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; - this.toolStrip1.Size = new System.Drawing.Size(346, 100); + this.toolStrip1.Size = new System.Drawing.Size(746, 50); this.toolStrip1.TabIndex = 0; - this.toolStrip1.Text = "toolStrip1"; - // 添加一些示例按钮 - this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); - this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); - this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.toolStripButton1, - this.toolStripButton2}); - // - // toolStripButton1 - // - this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; - this.toolStripButton1.Name = "toolStripButton1"; - this.toolStripButton1.Size = new System.Drawing.Size(52, 97); - this.toolStripButton1.Text = "按钮1"; + this.toolStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStrip1_ItemClicked); // // toolStripButton2 // this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; this.toolStripButton2.Name = "toolStripButton2"; - this.toolStripButton2.Size = new System.Drawing.Size(52, 97); - this.toolStripButton2.Text = "按钮2"; - this.splitContainer2.Panel1.Controls.Add(this.toolStrip1); + this.toolStripButton2.Size = new System.Drawing.Size(43, 47); + this.toolStripButton2.Text = "新建"; + this.toolStripButton2.Click += new System.EventHandler(this.ToolStripButton2_Click); + // + // toolStripButton1 + // + this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; + this.toolStripButton1.Name = "toolStripButton1"; + this.toolStripButton1.Size = new System.Drawing.Size(43, 47); + this.toolStripButton1.Text = "保存"; + this.toolStripButton1.Click += new System.EventHandler(this.ToolStripButton1_Click); // // listBox1 // - this.listBox1 = new System.Windows.Forms.ListBox(); this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.listBox1.FormattingEnabled = true; + this.listBox1.ItemHeight = 15; this.listBox1.Location = new System.Drawing.Point(0, 0); this.listBox1.Name = "listBox1"; - this.listBox1.Size = new System.Drawing.Size(346, 365); + this.listBox1.Size = new System.Drawing.Size(746, 415); this.listBox1.TabIndex = 0; - this.splitContainer2.Panel2.Controls.Add(this.listBox1); - this.splitContainer1.Panel2.Controls.Add(this.splitContainer2); + this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1_SelectedIndexChanged); // // Setting // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(850, 469); + this.ClientSize = new System.Drawing.Size(905, 515); + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Controls.Add(this.splitContainer1); this.Name = "Setting"; this.Text = "设置"; + this.Load += new System.EventHandler(this.Setting_Load); + this.splitContainer1.Panel1.ResumeLayout(false); + this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); + this.splitContainer1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.picBoxCamera)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.toolStrip1)).EndInit(); + this.splitContainer2.Panel1.ResumeLayout(false); + this.splitContainer2.Panel1.PerformLayout(); + this.splitContainer2.Panel2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); + this.splitContainer2.ResumeLayout(false); + this.toolStrip1.ResumeLayout(false); + this.toolStrip1.PerformLayout(); this.ResumeLayout(false); - this.PerformLayout(); } @@ -149,5 +166,4 @@ namespace Camera private System.Windows.Forms.ToolStripButton toolStripButton2; private System.Windows.Forms.ListBox listBox1; } -} - +} \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 0845680..222c234 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; @@ -13,49 +14,343 @@ namespace Camera { private Camera _camera; + private Rectangle _detectionZone; + private Rectangle _ledZone; + private int _selectedZoneIndex = -1; + private bool _isDrawing = false; + private bool _isMoving = false; + private Point _startPoint; + private Rectangle _originalZone; + + private Color[] _zoneColors = new Color[] { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Cyan, Color.Magenta }; + public Setting() { InitializeComponent(); - InitializeForm(); } - private void InitializeForm() + public void SetImage(Image image) + { + if (picBoxCamera.Image != null) + { + picBoxCamera.Image.Dispose(); + } + picBoxCamera.Image = (Image)image.Clone(); + _detectionZone = new Rectangle(0, 0, picBoxCamera.Image.Width, picBoxCamera.Image.Height); + _ledZone = new Rectangle(0, 0, 50, 50); + LoadConfig(); + picBoxCamera.Invalidate(); + } + + private void Setting_Load(object sender, EventArgs e) { - // 初始化相机对象 _camera = new Camera(); - - // 为工具栏按钮添加点击事件 - toolStripButton1.Click += ToolStripButton1_Click; - toolStripButton2.Click += ToolStripButton2_Click; - - // 初始化列表框数据 - InitializeListBox(); - - // 设置图片框的默认背景色 picBoxCamera.BackColor = Color.Gray; + splitContainer1.Panel2MinSize = 200; + splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; + splitContainer1.SplitterDistance = splitContainer1.Width - 200; + splitContainer2.Panel1MinSize = 40; } - private void InitializeListBox() + private void LoadConfig() { - // 添加一些示例数据到列表框 - listBox1.Items.Add("区域 1 - 红色"); - listBox1.Items.Add("区域 2 - 绿色"); - listBox1.Items.Add("区域 3 - 蓝色"); - listBox1.Items.Add("区域 4 - 灰色"); + string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Camera.csv"); + if (File.Exists(configPath)) + { + try + { + _detectionZone = new Rectangle(0, 0, 0, 0); + _ledZone = new Rectangle(0, 0, 0, 0); + + using (StreamReader reader = new StreamReader(configPath, Encoding.UTF8)) + { + string line; + bool isFirstLine = true; + while ((line = reader.ReadLine()) != null) + { + if (isFirstLine) + { + isFirstLine = false; + continue; + } + + string[] parts = line.Split(','); + if (parts.Length >= 5) + { + int index = int.Parse(parts[0]); + int x = int.Parse(parts[1]); + int y = int.Parse(parts[2]); + int width = int.Parse(parts[3]); + int height = int.Parse(parts[4]); + Color color = parts.Length > 5 ? ColorTranslator.FromHtml(parts[5]) : Color.Red; + + Rectangle zone = new Rectangle(x, y, width, height); + + if (index == 0) + { + _detectionZone = zone; + } + else if (index == 1) + { + _ledZone = zone; + } + } + } + } + + UpdateListBox(); + picBoxCamera.Invalidate(); + } + catch (Exception ex) + { + MessageBox.Show("加载配置失败: " + ex.Message); + } + } + else + { + if (picBoxCamera.Image != null) + { + _detectionZone = new Rectangle(10, 10, picBoxCamera.Image.Width - 20, picBoxCamera.Image.Height - 20); + _ledZone = new Rectangle(picBoxCamera.Image.Width - 60, 10, 50, 50); + } + UpdateListBox(); + } + } + + private void SaveConfig() + { + string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Camera.csv"); + try + { + using (StreamWriter writer = new StreamWriter(configPath, false, Encoding.UTF8)) + { + writer.WriteLine("索引,X坐标,Y坐标,宽度,高度,颜色"); + + if (_detectionZone.Width > 0 && _detectionZone.Height > 0) + { + writer.WriteLine(string.Format("0,{0},{1},{2},{3},#FF0000", _detectionZone.X, _detectionZone.Y, _detectionZone.Width, _detectionZone.Height)); + } + + if (_ledZone.Width > 0 && _ledZone.Height > 0) + { + writer.WriteLine(string.Format("1,{0},{1},{2},{3},#00FF00", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height)); + } + } + + MessageBox.Show("配置已保存到: " + configPath); + } + catch (Exception ex) + { + MessageBox.Show("保存配置失败: " + ex.Message); + } + } + + private void UpdateListBox() + { + listBox1.Items.Clear(); + + if (_detectionZone.Width > 0 && _detectionZone.Height > 0) + { + listBox1.Items.Add(string.Format("检测区: X={0}, Y={1}, W={2}, H={3}", _detectionZone.X, _detectionZone.Y, _detectionZone.Width, _detectionZone.Height)); + } + + if (_ledZone.Width > 0 && _ledZone.Height > 0) + { + listBox1.Items.Add(string.Format("Led区: X={0}, Y={1}, W={2}, H={3}", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height)); + } + } + + private void PicBoxCamera_Paint(object sender, PaintEventArgs e) + { + if (picBoxCamera.Image == null) return; + + Graphics g = e.Graphics; + + float scaleX = (float)picBoxCamera.ClientSize.Width / picBoxCamera.Image.Width; + float scaleY = (float)picBoxCamera.ClientSize.Height / picBoxCamera.Image.Height; + + Rectangle scaledDetectionZone = new Rectangle( + (int)(_detectionZone.X * scaleX), + (int)(_detectionZone.Y * scaleY), + (int)(_detectionZone.Width * scaleX), + (int)(_detectionZone.Height * scaleY) + ); + + Rectangle scaledLedZone = new Rectangle( + (int)(_ledZone.X * scaleX), + (int)(_ledZone.Y * scaleY), + (int)(_ledZone.Width * scaleX), + (int)(_ledZone.Height * scaleY) + ); + + using (Pen pen = new Pen(Color.Red, 2)) + { + g.DrawRectangle(pen, scaledDetectionZone); + } + + using (Pen pen = new Pen(Color.Green, 2)) + { + g.DrawRectangle(pen, scaledLedZone); + } + + using (Font font = new Font("Arial", 10)) + { + g.DrawString("检测区", font, Brushes.Red, scaledDetectionZone.X, scaledDetectionZone.Y - 15); + g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15); + } + + if (_selectedZoneIndex == 0) + { + DrawSelectionHandles(g, scaledDetectionZone); + } + else if (_selectedZoneIndex == 1) + { + DrawSelectionHandles(g, scaledLedZone); + } + } + + private void DrawSelectionHandles(Graphics g, Rectangle rect) + { + int handleSize = 8; + using (Brush brush = new SolidBrush(Color.White)) + using (Pen pen = new Pen(Color.Blue, 2)) + { + g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize); + g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize); + g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize); + g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize); + } + } + + private void PicBoxCamera_MouseDown(object sender, MouseEventArgs e) + { + if (picBoxCamera.Image == null) return; + + float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; + float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; + Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY)); + + if (_detectionZone.Contains(imagePoint)) + { + _selectedZoneIndex = 0; + _isMoving = true; + _startPoint = imagePoint; + _originalZone = _detectionZone; + } + else if (_ledZone.Contains(imagePoint)) + { + _selectedZoneIndex = 1; + _isMoving = true; + _startPoint = imagePoint; + _originalZone = _ledZone; + } + else + { + _selectedZoneIndex = -1; + _isDrawing = true; + _startPoint = imagePoint; + } + + UpdateListBox(); + picBoxCamera.Invalidate(); + } + + private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e) + { + if (picBoxCamera.Image == null) return; + + float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; + float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; + Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY)); + + if (_isMoving && _selectedZoneIndex >= 0) + { + int dx = imagePoint.X - _startPoint.X; + int dy = imagePoint.Y - _startPoint.Y; + + Rectangle newZone = new Rectangle( + _originalZone.X + dx, + _originalZone.Y + dy, + _originalZone.Width, + _originalZone.Height + ); + + if (newZone.X >= 0 && newZone.Y >= 0 && + newZone.X + newZone.Width <= picBoxCamera.Image.Width && + newZone.Y + newZone.Height <= picBoxCamera.Image.Height) + { + if (_selectedZoneIndex == 0) + { + _detectionZone = newZone; + } + else if (_selectedZoneIndex == 1) + { + _ledZone = newZone; + } + } + + UpdateListBox(); + picBoxCamera.Invalidate(); + } + } + + private void PicBoxCamera_MouseUp(object sender, MouseEventArgs e) + { + if (picBoxCamera.Image == null) return; + + float scaleX = (float)picBoxCamera.Image.Width / picBoxCamera.ClientSize.Width; + float scaleY = (float)picBoxCamera.Image.Height / picBoxCamera.ClientSize.Height; + Point imagePoint = new Point((int)(e.X * scaleX), (int)(e.Y * scaleY)); + + if (_isDrawing && !_isMoving) + { + int x = Math.Min(_startPoint.X, imagePoint.X); + int y = Math.Min(_startPoint.Y, imagePoint.Y); + int width = Math.Abs(imagePoint.X - _startPoint.X); + int height = Math.Abs(imagePoint.Y - _startPoint.Y); + + if (width > 10 && height > 10) + { + _detectionZone = new Rectangle(x, y, width, height); + _selectedZoneIndex = 0; + } + } + + _isDrawing = false; + _isMoving = false; + UpdateListBox(); + picBoxCamera.Invalidate(); + } + + private void ListBox1_SelectedIndexChanged(object sender, EventArgs e) + { + if (listBox1.SelectedIndex >= 0) + { + _selectedZoneIndex = listBox1.SelectedIndex; + picBoxCamera.Invalidate(); + } } private void ToolStripButton1_Click(object sender, EventArgs e) { - // 按钮1点击事件处理 - MessageBox.Show("按钮1被点击"); + SaveConfig(); } private void ToolStripButton2_Click(object sender, EventArgs e) { - // 按钮2点击事件处理 - MessageBox.Show("按钮2被点击"); + if (picBoxCamera.Image != null) + { + _detectionZone = new Rectangle(10, 10, picBoxCamera.Image.Width - 20, picBoxCamera.Image.Height - 20); + _ledZone = new Rectangle(picBoxCamera.Image.Width - 60, 10, 50, 50); + _selectedZoneIndex = -1; + UpdateListBox(); + picBoxCamera.Invalidate(); + } } - // 可以根据需要添加更多事件处理方法 + private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) + { + + } } -} +} \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.resx b/Windows/CS/Framework4.0/Camera/Camera/Setting.resx index 1af7de1..5da7a24 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.resx +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Test/Form1.Designer.cs b/Windows/CS/Framework4.0/Camera/Test/Form1.Designer.cs index 58216d6..1cf1ed6 100644 --- a/Windows/CS/Framework4.0/Camera/Test/Form1.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Test/Form1.Designer.cs @@ -28,17 +28,29 @@ namespace Test /// private void InitializeComponent() { - if (this.components == null) - { - this.components = new System.ComponentModel.Container(); - } - + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); - this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); + this.button1 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.tableLayoutPanel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.flowLayoutPanel1)).BeginInit(); this.SuspendLayout(); // + // 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.button1, 0, 0); + this.tableLayoutPanel1.Controls.Add(this.button2, 1, 0); + this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom; + this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 443); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 1; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel1.Size = new System.Drawing.Size(804, 33); + this.tableLayoutPanel1.TabIndex = 1; + // // pictureBox1 // this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill; @@ -49,20 +61,12 @@ namespace Test this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // - // flowLayoutPanel1 - // - this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom; - this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 443); - this.flowLayoutPanel1.Name = "flowLayoutPanel1"; - this.flowLayoutPanel1.Size = new System.Drawing.Size(804, 33); - this.flowLayoutPanel1.TabIndex = 1; - this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight; - // // button1 // - this.button1 = new System.Windows.Forms.Button(); + this.button1.Dock = System.Windows.Forms.DockStyle.Fill; + this.button1.Location = new System.Drawing.Point(3, 3); this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(400, 33); + this.button1.Size = new System.Drawing.Size(394, 27); this.button1.TabIndex = 0; this.button1.Text = "获取摄像头图像"; this.button1.UseVisualStyleBackColor = true; @@ -70,9 +74,10 @@ namespace Test // // button2 // - this.button2 = new System.Windows.Forms.Button(); + this.button2.Dock = System.Windows.Forms.DockStyle.Fill; + this.button2.Location = new System.Drawing.Point(403, 3); this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(400, 33); + this.button2.Size = new System.Drawing.Size(394, 27); this.button2.TabIndex = 1; this.button2.Text = "设置"; this.button2.UseVisualStyleBackColor = true; @@ -84,18 +89,19 @@ namespace Test this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(804, 476); this.Controls.Add(this.pictureBox1); - this.Controls.Add(this.flowLayoutPanel1); + this.Controls.Add(this.tableLayoutPanel1); this.Name = "Form1"; this.Text = "摄像头图像获取"; + this.Load += new System.EventHandler(Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.flowLayoutPanel1)).EndInit(); + this.tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); } #endregion + private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.PictureBox pictureBox1; - private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; } diff --git a/Windows/CS/Framework4.0/Camera/Test/Form1.cs b/Windows/CS/Framework4.0/Camera/Test/Form1.cs index 55d9d5e..dcc2409 100644 --- a/Windows/CS/Framework4.0/Camera/Test/Form1.cs +++ b/Windows/CS/Framework4.0/Camera/Test/Form1.cs @@ -17,7 +17,10 @@ namespace Test public Form1() { InitializeComponent(); - + } + + private void Form1_Load(object sender, EventArgs e) + { _camera = new Camera.Camera(); _camera.ImageCaptured += Camera_ImageCaptured; } @@ -59,8 +62,7 @@ namespace Test private void button2_Click(object sender, EventArgs e) { - Camera.Setting settingForm = new Camera.Setting(); - settingForm.ShowDialog(); + _camera.SetArea(); } protected override void OnFormClosing(FormClosingEventArgs e)