基础测试代码

This commit is contained in:
zqm
2026-03-17 09:21:09 +08:00
parent e4502c338a
commit ce49e4c736
3 changed files with 55 additions and 49 deletions

View File

@@ -40,10 +40,9 @@ namespace Test
this.btnStopCapture = new System.Windows.Forms.Button(); this.btnStopCapture = new System.Windows.Forms.Button();
this.btnStopCamera = new System.Windows.Forms.Button(); this.btnStopCamera = new System.Windows.Forms.Button();
this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); this.lblStatus = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtLog = new System.Windows.Forms.TextBox(); this.txtLog = new System.Windows.Forms.TextBox();
this.lblStatus = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
@@ -163,18 +162,18 @@ namespace Test
// statusStrip1 // statusStrip1
// //
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1}); this.lblStatus});
this.statusStrip1.Location = new System.Drawing.Point(0, 451); this.statusStrip1.Location = new System.Drawing.Point(0, 451);
this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22); this.statusStrip1.Size = new System.Drawing.Size(800, 22);
this.statusStrip1.TabIndex = 11; this.statusStrip1.TabIndex = 11;
this.statusStrip1.Text = "statusStrip1"; this.statusStrip1.Text = "statusStrip1";
// //
// toolStripStatusLabel1 // lblStatus
// //
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; this.lblStatus.Name = "lblStatus";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(44, 17); this.lblStatus.Size = new System.Drawing.Size(68, 17);
this.toolStripStatusLabel1.Text = "状态"; this.lblStatus.Text = "状态: 未初始化";
// //
// groupBox1 // groupBox1
// //
@@ -196,23 +195,11 @@ namespace Test
this.txtLog.Size = new System.Drawing.Size(551, 80); this.txtLog.Size = new System.Drawing.Size(551, 80);
this.txtLog.TabIndex = 0; this.txtLog.TabIndex = 0;
// //
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblStatus.ForeColor = System.Drawing.Color.Red;
this.lblStatus.Location = new System.Drawing.Point(12, 550);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(96, 12);
this.lblStatus.TabIndex = 13;
this.lblStatus.Text = "状态: 未初始化";
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 574); this.ClientSize = new System.Drawing.Size(800, 574);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.groupBox1); this.Controls.Add(this.groupBox1);
this.Controls.Add(this.statusStrip1); this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.btnStopCamera); this.Controls.Add(this.btnStopCamera);
@@ -253,9 +240,8 @@ namespace Test
private System.Windows.Forms.Button btnStopCapture; private System.Windows.Forms.Button btnStopCapture;
private System.Windows.Forms.Button btnStopCamera; private System.Windows.Forms.Button btnStopCamera;
private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1; private System.Windows.Forms.ToolStripStatusLabel lblStatus;
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtLog; private System.Windows.Forms.TextBox txtLog;
private System.Windows.Forms.Label lblStatus;
} }
} }

View File

@@ -5,8 +5,8 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using XCamera;
using XCamera.Models; using XCamera.Models;
namespace Test namespace Test
@@ -16,13 +16,17 @@ namespace Test
private bool _isInitialized = false; private bool _isInitialized = false;
private bool _isConnected = false; private bool _isConnected = false;
private bool _isCapturing = false; private bool _isCapturing = false;
private Thread _captureThread;
private bool _shouldStopCapture = false;
public Form1() public Form1()
{ {
InitializeComponent(); InitializeComponent();
// 检查是否处于设计模式
if (!DesignMode)
{
InitializeEvents(); InitializeEvents();
}
UpdateUI(); UpdateUI();
} }
@@ -31,11 +35,15 @@ namespace Test
/// </summary> /// </summary>
private void InitializeEvents() private void InitializeEvents()
{ {
// 避免在设计模式下订阅事件
if (DesignMode)
return;
// 订阅XCamera事件 // 订阅XCamera事件
XCamera.LedStatusUpdated += OnLedStatusUpdated; global::XCamera.XCamera.LedStatusUpdated += OnLedStatusUpdated;
XCamera.ConnectionStateChanged += OnConnectionStateChanged; global::XCamera.XCamera.ConnectionStateChanged += OnConnectionStateChanged;
XCamera.CaptureStateChanged += OnCaptureStateChanged; global::XCamera.XCamera.CaptureStateChanged += OnCaptureStateChanged;
XCamera.ErrorOccurred += OnErrorOccurred; global::XCamera.XCamera.ErrorOccurred += OnErrorOccurred;
} }
/// <summary> /// <summary>
@@ -50,13 +58,13 @@ namespace Test
// 使用默认配置文件路径 // 使用默认配置文件路径
string configPath = System.IO.Path.Combine(Application.StartupPath, "config", "sample_config.json"); string configPath = System.IO.Path.Combine(Application.StartupPath, "config", "sample_config.json");
if (XCamera.Initialize(configPath)) if (global::XCamera.XCamera.Initialize(configPath))
{ {
_isInitialized = true; _isInitialized = true;
LogMessage("初始化成功!"); LogMessage("初始化成功!");
// 显示配置信息 // 显示配置信息
var config = XCamera.GetCurrentConfig(); var config = global::XCamera.XCamera.GetCurrentConfig();
if (config != null) if (config != null)
{ {
LogMessage($"摄像头IP: {config.IpAddress}"); LogMessage($"摄像头IP: {config.IpAddress}");
@@ -93,12 +101,12 @@ namespace Test
{ {
LogMessage($"正在加载配置文件: {openFileDialog.FileName}"); LogMessage($"正在加载配置文件: {openFileDialog.FileName}");
if (XCamera.SetConfigFile(openFileDialog.FileName)) if (global::XCamera.XCamera.SetConfigFile(openFileDialog.FileName))
{ {
LogMessage("配置文件加载成功!"); LogMessage("配置文件加载成功!");
// 显示配置信息 // 显示配置信息
var config = XCamera.GetCurrentConfig(); var config = global::XCamera.XCamera.GetCurrentConfig();
if (config != null) if (config != null)
{ {
LogMessage($"摄像头IP: {config.IpAddress}"); LogMessage($"摄像头IP: {config.IpAddress}");
@@ -133,7 +141,7 @@ namespace Test
LogMessage("正在启动摄像头..."); LogMessage("正在启动摄像头...");
if (XCamera.StartCamera()) if (global::XCamera.XCamera.StartCamera())
{ {
LogMessage("摄像头启动成功!"); LogMessage("摄像头启动成功!");
} }
@@ -171,7 +179,7 @@ namespace Test
LogMessage("正在开始捕获图像..."); LogMessage("正在开始捕获图像...");
if (XCamera.StartCapture(1000)) // 1秒间隔 if (global::XCamera.XCamera.StartCapture(1000)) // 1秒间隔
{ {
LogMessage("开始捕获图像成功!"); LogMessage("开始捕获图像成功!");
} }
@@ -203,7 +211,7 @@ namespace Test
LogMessage("正在识别LED灯..."); LogMessage("正在识别LED灯...");
var statuses = XCamera.DetectLeds(); var statuses = global::XCamera.XCamera.DetectLeds();
LogMessage($"检测到 {statuses.Count} 个LED灯:"); LogMessage($"检测到 {statuses.Count} 个LED灯:");
foreach (var status in statuses) foreach (var status in statuses)
@@ -235,7 +243,7 @@ namespace Test
LogMessage("正在查询LED状态..."); LogMessage("正在查询LED状态...");
var statuses = XCamera.GetLedStatuses(); var statuses = global::XCamera.XCamera.GetLedStatuses();
LogMessage($"当前LED状态 ({statuses.Count} 个):"); LogMessage($"当前LED状态 ({statuses.Count} 个):");
foreach (var status in statuses) foreach (var status in statuses)
@@ -257,7 +265,7 @@ namespace Test
try try
{ {
LogMessage("正在打开区域编辑器..."); LogMessage("正在打开区域编辑器...");
XCamera.ShowRegionEditor(); global::XCamera.XCamera.ShowRegionEditor();
LogMessage("区域编辑器已关闭"); LogMessage("区域编辑器已关闭");
} }
catch (Exception ex) catch (Exception ex)
@@ -277,15 +285,15 @@ namespace Test
if (_isCapturing) if (_isCapturing)
{ {
XCamera.StopCapture(); global::XCamera.XCamera.StopCapture();
} }
if (_isConnected) if (_isConnected)
{ {
XCamera.StopCamera(); global::XCamera.XCamera.StopCamera();
} }
XCamera.Shutdown(); global::XCamera.XCamera.Shutdown();
_isInitialized = false; _isInitialized = false;
_isConnected = false; _isConnected = false;
@@ -315,7 +323,7 @@ namespace Test
} }
LogMessage("正在停止捕获..."); LogMessage("正在停止捕获...");
XCamera.StopCapture(); global::XCamera.XCamera.StopCapture();
LogMessage("捕获已停止"); LogMessage("捕获已停止");
} }
catch (Exception ex) catch (Exception ex)
@@ -343,10 +351,10 @@ namespace Test
if (_isCapturing) if (_isCapturing)
{ {
XCamera.StopCapture(); global::XCamera.XCamera.StopCapture();
} }
XCamera.StopCamera(); global::XCamera.XCamera.StopCamera();
LogMessage("摄像头已停止"); LogMessage("摄像头已停止");
} }
catch (Exception ex) catch (Exception ex)
@@ -532,7 +540,9 @@ namespace Test
else else
lblStatus.Text = "状态: 未初始化"; lblStatus.Text = "状态: 未初始化";
lblStatus.ForeColor = _isConnected ? Color.Green : Color.Red; // ToolStripStatusLabel没有ForeColor属性我们可以使用BackColor或者保持默认样式
// 如果需要颜色变化可以考虑使用BackColor
// lblStatus.BackColor = _isConnected ? Color.LightGreen : Color.LightPink;
} }
/// <summary> /// <summary>
@@ -542,20 +552,24 @@ namespace Test
{ {
try try
{ {
// 避免在设计模式下执行外部代码
if (DesignMode)
return;
// 确保释放资源 // 确保释放资源
if (_isCapturing) if (_isCapturing)
{ {
XCamera.StopCapture(); global::XCamera.XCamera.StopCapture();
} }
if (_isConnected) if (_isConnected)
{ {
XCamera.StopCamera(); global::XCamera.XCamera.StopCamera();
} }
if (_isInitialized) if (_isInitialized)
{ {
XCamera.Shutdown(); global::XCamera.XCamera.Shutdown();
} }
} }
catch catch

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup> <PropertyGroup>
@@ -43,6 +43,12 @@
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XCamera\XCamera.csproj">
<Project>{B8E762E5-4A3B-4F7D-9C2E-8F3A7B9C5D1E}</Project>
<Name>XCamera</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Form1.cs"> <Compile Include="Form1.cs">
<SubType>Form</SubType> <SubType>Form</SubType>