136 lines
4.9 KiB
C#
136 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading;
|
||
using XCamera;
|
||
using XCamera.Models;
|
||
|
||
namespace XCamera.TestApp
|
||
{
|
||
/// <summary>
|
||
/// XCamera库简单测试程序
|
||
/// </summary>
|
||
class Program
|
||
{
|
||
static void Main(string[] args)
|
||
{
|
||
Console.WriteLine("XCamera LED灯识别库测试程序");
|
||
Console.WriteLine("==========================================");
|
||
|
||
try
|
||
{
|
||
// 订阅事件
|
||
XCamera.LedStatusUpdated += OnLedStatusUpdated;
|
||
XCamera.ConnectionStateChanged += OnConnectionStateChanged;
|
||
XCamera.ErrorOccurred += OnErrorOccurred;
|
||
|
||
// 1. 初始化
|
||
Console.WriteLine("正在初始化...");
|
||
if (!XCamera.Initialize("config\\sample_config.json"))
|
||
{
|
||
Console.WriteLine("初始化失败!");
|
||
return;
|
||
}
|
||
Console.WriteLine("初始化成功!");
|
||
|
||
// 显示当前配置
|
||
var config = XCamera.GetCurrentConfig();
|
||
if (config != null)
|
||
{
|
||
Console.WriteLine($"摄像头IP: {config.IpAddress}");
|
||
Console.WriteLine($"TCP端口: {config.TcpPort}");
|
||
Console.WriteLine($"LED区域数: {config.LedRegions.Count}");
|
||
}
|
||
|
||
// 2. 启动摄像头
|
||
Console.WriteLine("正在启动摄像头...");
|
||
if (!XCamera.StartCamera())
|
||
{
|
||
Console.WriteLine("启动摄像头失败!");
|
||
return;
|
||
}
|
||
Console.WriteLine("摄像头启动成功!");
|
||
|
||
// 3. 手动检测一次LED状态
|
||
Console.WriteLine("正在检测LED状态...");
|
||
var statuses = XCamera.DetectLeds();
|
||
Console.WriteLine($"检测到 {statuses.Count} 个LED灯:");
|
||
foreach (var status in statuses)
|
||
{
|
||
Console.WriteLine($" {status.GetStatusDescription()}");
|
||
}
|
||
|
||
// 4. 开始循环捕获(可选)
|
||
Console.WriteLine("\n是否开始循环捕获? (y/n)");
|
||
string response = Console.ReadLine();
|
||
|
||
if (response.ToLower() == "y")
|
||
{
|
||
Console.WriteLine("开始循环捕获,按任意键停止...");
|
||
XCamera.StartCapture(2000); // 2秒间隔
|
||
|
||
// 等待用户输入
|
||
Console.ReadKey();
|
||
|
||
XCamera.StopCapture();
|
||
Console.WriteLine("停止捕获");
|
||
}
|
||
|
||
// 5. 显示区域编辑器(可选)
|
||
Console.WriteLine("\n是否显示区域编辑器? (y/n)");
|
||
response = Console.ReadLine();
|
||
|
||
if (response.ToLower() == "y")
|
||
{
|
||
Console.WriteLine("显示区域编辑器...");
|
||
XCamera.ShowRegionEditor();
|
||
}
|
||
|
||
// 6. 关闭资源
|
||
Console.WriteLine("正在关闭资源...");
|
||
XCamera.Shutdown();
|
||
Console.WriteLine("资源已释放");
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"程序异常: {ex.Message}");
|
||
Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
|
||
}
|
||
finally
|
||
{
|
||
XCamera.Shutdown();
|
||
}
|
||
|
||
Console.WriteLine("\n测试程序结束,按任意键退出...");
|
||
Console.ReadKey();
|
||
}
|
||
|
||
/// <summary>
|
||
/// LED状态更新事件处理
|
||
/// </summary>
|
||
private static void OnLedStatusUpdated(object sender, LedStatusEventArgs e)
|
||
{
|
||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] LED状态更新:");
|
||
foreach (var status in e.Statuses)
|
||
{
|
||
Console.WriteLine($" {status.GetStatusDescription()}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 连接状态变更事件处理
|
||
/// </summary>
|
||
private static void OnConnectionStateChanged(object sender, ConnectionStateEventArgs e)
|
||
{
|
||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 摄像头连接状态: {(e.IsConnected ? "已连接" : "已断开")}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 错误事件处理
|
||
/// </summary>
|
||
private static void OnErrorOccurred(object sender, ErrorEventArgs e)
|
||
{
|
||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 错误: {e.ErrorMessage}");
|
||
}
|
||
}
|
||
} |