diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.Designer.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.Designer.cs
index c39eabf..225b6ee 100644
--- a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.Designer.cs
+++ b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.Designer.cs
@@ -7,19 +7,6 @@
///
private System.ComponentModel.IContainer components = null;
- ///
- /// 清理所有正在使用的资源。
- ///
- /// 如果应释放托管资源,为 true;否则为 false。
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
-
#region 组件设计器生成的代码
///
@@ -28,10 +15,34 @@
///
private void InitializeComponent()
{
- components = new System.ComponentModel.Container();
+ this.imageBox = new System.Windows.Forms.PictureBox();
+ ((System.ComponentModel.ISupportInitialize)(this.imageBox)).BeginInit();
+ this.SuspendLayout();
+ //
+ // imageBox
+ //
+ this.imageBox.Dock = System.Windows.Forms.DockStyle.Fill;
+ this.imageBox.Location = new System.Drawing.Point(0, 0);
+ this.imageBox.Name = "imageBox";
+ this.imageBox.Size = new System.Drawing.Size(150, 150);
+ this.imageBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
+ this.imageBox.TabIndex = 0;
+ this.imageBox.TabStop = false;
+ //
+ // Camera
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.Controls.Add(this.imageBox);
+ this.Name = "Camera";
+ this.Load += new System.EventHandler(this.Camera_Load);
+ ((System.ComponentModel.ISupportInitialize)(this.imageBox)).EndInit();
+ this.ResumeLayout(false);
+
}
#endregion
+
+ private System.Windows.Forms.PictureBox imageBox;
}
}
diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs
index 96efa0b..de50a30 100644
--- a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs
+++ b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@@ -11,9 +11,354 @@ namespace JoyD.Windows.CS.Toprie
{
public partial class Camera : UserControl
{
+ // 设备管理器实例
+ private DeviceManager _deviceManager;
+
+ // 是否正在接收图像
+ private bool _isReceivingImage = false;
+
+ // 显示错误的定时器
+ private System.Windows.Forms.Timer _errorDisplayTimer;
+
public Camera()
{
InitializeComponent();
+
+ // 只有在非设计模式下才初始化设备管理器和错误定时器
+ if (!DesignMode)
+ {
+ InitializeDeviceManager();
+ InitializeErrorTimer();
+ }
+ }
+
+ ///
+ /// Camera控件加载事件
+ /// 在控件加载时自动启动相机显示热图
+ ///
+ private void Camera_Load(object sender, EventArgs e)
+ {
+ // 只有在非设计模式下才启动相机
+ if (!DesignMode)
+ {
+ try
+ {
+ StartCamera();
+ }
+ catch (Exception ex)
+ {
+ ShowError($"自动启动相机失败: {ex.Message}");
+ }
+ }
+ }
+
+ // 注意:UserControl不支持FormClosing事件,资源清理已在Dispose方法中处理
+
+ ///
+ /// 初始化设备管理器
+ ///
+ private void InitializeDeviceManager()
+ {
+ _deviceManager = new DeviceManager();
+
+ // 注册图像接收事件
+ _deviceManager.ImageReceived += DeviceManager_ImageReceived;
+
+ // 注册连接状态变更事件
+ _deviceManager.ConnectionStatusChanged += DeviceManager_ConnectionStatusChanged;
+
+ // 注册连接异常事件
+ _deviceManager.ConnectionException += DeviceManager_ConnectionException;
+ }
+
+ ///
+ /// 初始化错误显示定时器
+ ///
+ private void InitializeErrorTimer()
+ {
+ _errorDisplayTimer = new System.Windows.Forms.Timer { Interval = 3000 }; // 显示3秒后清除
+ _errorDisplayTimer.Tick += ErrorDisplayTimer_Tick;
+ }
+
+ ///
+ /// 启动并连接设备
+ ///
+ public void StartCamera()
+ {
+ try
+ {
+ // 设置为热图模式
+ _deviceManager.CurrentImageMode = ImageMode.Thermal;
+
+ // 启用自动重连
+ _deviceManager.AutoReconnectEnabled = true;
+
+ // 连接设备
+ bool connected = _deviceManager.ConnectDevice();
+
+ if (connected)
+ {
+ // 开始接收图像
+ StartReceiveImage();
+ }
+ else
+ {
+ ShowError("无法连接到设备,请检查设备是否在线");
+ }
+ }
+ catch (Exception ex)
+ {
+ ShowError($"启动相机失败: {ex.Message}");
+ }
+ }
+
+ ///
+ /// 开始接收图像
+ ///
+ private void StartReceiveImage()
+ {
+ try
+ {
+ if (!_isReceivingImage && _deviceManager.ConnectionStatus == ConnectionStatus.Connected)
+ {
+ _deviceManager.StartReceiveImage();
+ _isReceivingImage = true;
+ }
+ }
+ catch (Exception ex)
+ {
+ ShowError($"开始接收图像失败: {ex.Message}");
+ }
+ }
+
+ ///
+ /// 停止接收图像
+ ///
+ public void StopCamera()
+ {
+ try
+ {
+ if (_isReceivingImage)
+ {
+ _deviceManager.StopReceiveImage();
+ _isReceivingImage = false;
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"停止相机失败: {ex.Message}");
+ }
+ }
+
+ ///
+ /// 设备管理器图像接收事件处理
+ ///
+ private void DeviceManager_ImageReceived(object sender, ImageReceivedEventArgs e)
+ {
+ if (this.InvokeRequired)
+ {
+ // 在UI线程上更新图像
+ this.Invoke(new Action(() => UpdateImage(e.ImageData)));
+ }
+ else
+ {
+ UpdateImage(e.ImageData);
+ }
+ }
+
+ ///
+ /// 更新图像显示
+ ///
+ private void UpdateImage(Image image)
+ {
+ try
+ {
+ // 释放之前的图像资源
+ if (imageBox.Image != null)
+ {
+ imageBox.Image.Dispose();
+ }
+
+ // 设置新图像
+ imageBox.Image = (Image)image.Clone();
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"更新图像失败: {ex.Message}");
+ }
+ }
+
+ ///
+ /// 设备管理器连接状态变更事件处理
+ ///
+ private void DeviceManager_ConnectionStatusChanged(object sender, ConnectionStatusChangedEventArgs e)
+ {
+ if (this.InvokeRequired)
+ {
+ this.Invoke(new Action(() => HandleConnectionStatusChanged(e)));
+ }
+ else
+ {
+ HandleConnectionStatusChanged(e);
+ }
+ }
+
+ ///
+ /// 处理连接状态变更
+ ///
+ private void HandleConnectionStatusChanged(ConnectionStatusChangedEventArgs e)
+ {
+ switch (e.NewStatus)
+ {
+ case ConnectionStatus.Connected:
+ Console.WriteLine("设备已连接");
+ if (!_isReceivingImage)
+ {
+ StartReceiveImage();
+ }
+ break;
+ case ConnectionStatus.Disconnected:
+ Console.WriteLine("设备已断开连接");
+ _isReceivingImage = false;
+ if (!string.IsNullOrEmpty(e.Message))
+ {
+ ShowError(e.Message);
+ }
+ break;
+ case ConnectionStatus.Connecting:
+ case ConnectionStatus.Reconnecting:
+ Console.WriteLine("正在连接设备...");
+ break;
+ }
+ }
+
+ ///
+ /// 设备管理器连接异常事件处理
+ ///
+ private void DeviceManager_ConnectionException(object sender, ConnectionExceptionEventArgs e)
+ {
+ if (this.InvokeRequired)
+ {
+ this.Invoke(new Action(() => ShowError($"连接异常: {e.ContextMessage}")));
+ }
+ else
+ {
+ ShowError($"连接异常: {e.ContextMessage}");
+ }
+ }
+
+ ///
+ /// 显示错误信息
+ ///
+ private void ShowError(string message)
+ {
+ Console.WriteLine(message);
+
+ // 可以根据需要添加UI上的错误显示
+ // 这里简化处理,只在控制台输出
+
+ // 如果需要在图像区域显示错误文字,可以使用以下代码
+ using (Bitmap errorBitmap = new Bitmap(imageBox.Width, imageBox.Height))
+ using (Graphics g = Graphics.FromImage(errorBitmap))
+ {
+ g.Clear(Color.Black);
+ using (Font font = new Font("Arial", 10))
+ using (Brush brush = new SolidBrush(Color.Red))
+ {
+ SizeF textSize = g.MeasureString(message, font);
+ PointF textLocation = new PointF(
+ (errorBitmap.Width - textSize.Width) / 2,
+ (errorBitmap.Height - textSize.Height) / 2);
+ g.DrawString(message, font, brush, textLocation);
+ }
+
+ UpdateImage(errorBitmap);
+ }
+
+ // 启动定时器,3秒后清除错误显示
+ _errorDisplayTimer.Stop();
+ _errorDisplayTimer.Start();
+ }
+
+ ///
+ /// 错误显示定时器事件
+ ///
+ private void ErrorDisplayTimer_Tick(object sender, EventArgs e)
+ {
+ _errorDisplayTimer.Stop();
+ // 清除错误显示,恢复到等待图像状态
+ using (Bitmap waitingBitmap = new Bitmap(imageBox.Width, imageBox.Height))
+ using (Graphics g = Graphics.FromImage(waitingBitmap))
+ {
+ g.Clear(Color.Black);
+ using (Font font = new Font("Arial", 10))
+ using (Brush brush = new SolidBrush(Color.White))
+ {
+ string waitingText = "正在等待热图数据...";
+ SizeF textSize = g.MeasureString(waitingText, font);
+ PointF textLocation = new PointF(
+ (waitingBitmap.Width - textSize.Width) / 2,
+ (waitingBitmap.Height - textSize.Height) / 2);
+ g.DrawString(waitingText, font, brush, textLocation);
+ }
+
+ UpdateImage(waitingBitmap);
+ }
+ }
+
+ ///
+ /// 清理资源
+ ///
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ // 检查是否处于设计模式
+ if (!DesignMode)
+ {
+ // 停止相机并释放相关资源
+ try
+ {
+ StopCamera();
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine("关闭相机时出错: " + ex.Message);
+ }
+ }
+
+ // 取消注册事件并释放设备管理器
+ if (_deviceManager != null)
+ {
+ _deviceManager.ImageReceived -= DeviceManager_ImageReceived;
+ _deviceManager.ConnectionStatusChanged -= DeviceManager_ConnectionStatusChanged;
+ _deviceManager.ConnectionException -= DeviceManager_ConnectionException;
+ _deviceManager.Dispose();
+ _deviceManager = null;
+ }
+
+ // 无论是否在设计模式下,都需要释放定时器
+ if (_errorDisplayTimer != null)
+ {
+ _errorDisplayTimer.Stop();
+ _errorDisplayTimer.Dispose();
+ _errorDisplayTimer = null;
+ }
+
+ // 无论是否在设计模式下,都需要释放图像资源
+ if (imageBox.Image != null)
+ {
+ imageBox.Image.Dispose();
+ imageBox.Image = null;
+ }
+
+ // 释放组件资源
+ if (components != null)
+ {
+ components.Dispose();
+ }
+ }
+ base.Dispose(disposing);
}
}
}
diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.resx b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.resx
new file mode 100644
index 0000000..1af7de1
--- /dev/null
+++ b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.resx
@@ -0,0 +1,120 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
\ No newline at end of file
diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs b/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs
index 990c155..6e56918 100644
--- a/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs
+++ b/Windows/CS/Framework4.0/Toprie/Toprie/DeviceManager.cs
@@ -106,9 +106,6 @@ namespace JoyD.Windows.CS.Toprie
// 设备ID列表
private List _deviceIds;
- // 当前连接的设备ID
- private int _currentDeviceId = -1;
-
// 是否已初始化
private bool _isInitialized;
@@ -874,8 +871,6 @@ namespace JoyD.Windows.CS.Toprie
// 启动心跳检测
StartHeartbeat();
- // 设置连接状态
- _currentDeviceId = 1; // 简化处理,实际应该根据设备返回的数据设置
UpdateConnectionStatus(ConnectionStatus.Connected, "设备连接成功");
// 重置重连尝试计数
@@ -980,10 +975,7 @@ namespace JoyD.Windows.CS.Toprie
StopAllImageReceivers();
// 设置断开状态
- UpdateConnectionStatus(ConnectionStatus.Disconnected, "设备已断开连接");
-
- // 重置设备ID
- _currentDeviceId = -1;
+ UpdateConnectionStatus(ConnectionStatus.Disconnected, "设备已断开连接");
}
catch (Exception ex)
{
@@ -1163,7 +1155,6 @@ namespace JoyD.Windows.CS.Toprie
// 释放非托管资源
_isInitialized = false;
- _currentDeviceId = -1;
_disposed = true;
}
diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj
index 173d850..cde2b8c 100644
--- a/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj
+++ b/Windows/CS/Framework4.0/Toprie/Toprie/Toprie.csproj
@@ -69,5 +69,10 @@
+
+
+ Camera.cs
+
+
\ No newline at end of file