捕获图像

This commit is contained in:
zqm
2026-03-17 14:28:07 +08:00
parent ce49e4c736
commit 7cd923b91b
11 changed files with 651 additions and 130 deletions

View File

@@ -43,6 +43,9 @@ namespace Test
this.lblStatus = new System.Windows.Forms.ToolStripStatusLabel();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtLog = new System.Windows.Forms.TextBox();
this.lblIpAddress = new System.Windows.Forms.Label();
this.txtIpAddress = new System.Windows.Forms.TextBox();
this.btnSetIP = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit();
this.statusStrip1.SuspendLayout();
this.groupBox1.SuspendLayout();
@@ -79,82 +82,109 @@ namespace Test
this.btnSetConfigFile.UseVisualStyleBackColor = true;
this.btnSetConfigFile.Click += new System.EventHandler(this.btnSetConfigFile_Click);
//
// lblIpAddress
//
this.lblIpAddress.AutoSize = true;
this.lblIpAddress.Location = new System.Drawing.Point(608, 125);
this.lblIpAddress.Name = "lblIpAddress";
this.lblIpAddress.Size = new System.Drawing.Size(65, 12);
this.lblIpAddress.TabIndex = 3;
this.lblIpAddress.Text = "IP地址:";
//
// txtIpAddress
//
this.txtIpAddress.Location = new System.Drawing.Point(675, 122);
this.txtIpAddress.Name = "txtIpAddress";
this.txtIpAddress.Size = new System.Drawing.Size(100, 21);
this.txtIpAddress.TabIndex = 4;
this.txtIpAddress.Text = "192.168.100.10";
//
// btnSetIP
//
this.btnSetIP.Location = new System.Drawing.Point(608, 140);
this.btnSetIP.Name = "btnSetIP";
this.btnSetIP.Size = new System.Drawing.Size(111, 31);
this.btnSetIP.TabIndex = 5;
this.btnSetIP.Text = "3. 设置IP地址";
this.btnSetIP.UseVisualStyleBackColor = true;
this.btnSetIP.Click += new System.EventHandler(this.btnSetIP_Click);
//
// btnStartCamera
//
this.btnStartCamera.Location = new System.Drawing.Point(608, 140);
this.btnStartCamera.Location = new System.Drawing.Point(608, 177);
this.btnStartCamera.Name = "btnStartCamera";
this.btnStartCamera.Size = new System.Drawing.Size(111, 31);
this.btnStartCamera.TabIndex = 3;
this.btnStartCamera.Text = "3. 启动摄像头";
this.btnStartCamera.TabIndex = 6;
this.btnStartCamera.Text = "4. 启动摄像头";
this.btnStartCamera.UseVisualStyleBackColor = true;
this.btnStartCamera.Click += new System.EventHandler(this.btnStartCamera_Click);
//
// btnStartCapture
//
this.btnStartCapture.Location = new System.Drawing.Point(608, 191);
this.btnStartCapture.Location = new System.Drawing.Point(608, 228);
this.btnStartCapture.Name = "btnStartCapture";
this.btnStartCapture.Size = new System.Drawing.Size(111, 31);
this.btnStartCapture.TabIndex = 4;
this.btnStartCapture.Text = "4. 开始捕获";
this.btnStartCapture.TabIndex = 7;
this.btnStartCapture.Text = "5. 开始捕获";
this.btnStartCapture.UseVisualStyleBackColor = true;
this.btnStartCapture.Click += new System.EventHandler(this.btnStartCapture_Click);
//
// btnDetectLeds
//
this.btnDetectLeds.Location = new System.Drawing.Point(608, 242);
this.btnDetectLeds.Location = new System.Drawing.Point(608, 279);
this.btnDetectLeds.Name = "btnDetectLeds";
this.btnDetectLeds.Size = new System.Drawing.Size(111, 31);
this.btnDetectLeds.TabIndex = 5;
this.btnDetectLeds.Text = "5. LED识别";
this.btnDetectLeds.TabIndex = 8;
this.btnDetectLeds.Text = "6. LED识别";
this.btnDetectLeds.UseVisualStyleBackColor = true;
this.btnDetectLeds.Click += new System.EventHandler(this.btnDetectLeds_Click);
//
// btnGetLedStatuses
//
this.btnGetLedStatuses.Location = new System.Drawing.Point(608, 293);
this.btnGetLedStatuses.Location = new System.Drawing.Point(608, 330);
this.btnGetLedStatuses.Name = "btnGetLedStatuses";
this.btnGetLedStatuses.Size = new System.Drawing.Size(111, 31);
this.btnGetLedStatuses.TabIndex = 6;
this.btnGetLedStatuses.Text = "6. 状态查询";
this.btnGetLedStatuses.TabIndex = 9;
this.btnGetLedStatuses.Text = "7. 状态查询";
this.btnGetLedStatuses.UseVisualStyleBackColor = true;
this.btnGetLedStatuses.Click += new System.EventHandler(this.btnGetLedStatuses_Click);
//
// btnShowRegionEditor
//
this.btnShowRegionEditor.Location = new System.Drawing.Point(608, 344);
this.btnShowRegionEditor.Location = new System.Drawing.Point(608, 381);
this.btnShowRegionEditor.Name = "btnShowRegionEditor";
this.btnShowRegionEditor.Size = new System.Drawing.Size(111, 31);
this.btnShowRegionEditor.TabIndex = 7;
this.btnShowRegionEditor.Text = "7. 区域编辑器";
this.btnShowRegionEditor.TabIndex = 10;
this.btnShowRegionEditor.Text = "8. 区域编辑器";
this.btnShowRegionEditor.UseVisualStyleBackColor = true;
this.btnShowRegionEditor.Click += new System.EventHandler(this.btnShowRegionEditor_Click);
//
// btnShutdown
//
this.btnShutdown.Location = new System.Drawing.Point(608, 395);
this.btnShutdown.Location = new System.Drawing.Point(608, 432);
this.btnShutdown.Name = "btnShutdown";
this.btnShutdown.Size = new System.Drawing.Size(111, 31);
this.btnShutdown.TabIndex = 8;
this.btnShutdown.Text = "8. 关闭";
this.btnShutdown.TabIndex = 11;
this.btnShutdown.Text = "9. 关闭";
this.btnShutdown.UseVisualStyleBackColor = true;
this.btnShutdown.Click += new System.EventHandler(this.btnShutdown_Click);
//
// btnStopCapture
//
this.btnStopCapture.Location = new System.Drawing.Point(725, 191);
this.btnStopCapture.Location = new System.Drawing.Point(725, 228);
this.btnStopCapture.Name = "btnStopCapture";
this.btnStopCapture.Size = new System.Drawing.Size(63, 31);
this.btnStopCapture.TabIndex = 9;
this.btnStopCapture.TabIndex = 12;
this.btnStopCapture.Text = "停止";
this.btnStopCapture.UseVisualStyleBackColor = true;
this.btnStopCapture.Click += new System.EventHandler(this.btnStopCapture_Click);
//
// btnStopCamera
//
this.btnStopCamera.Location = new System.Drawing.Point(725, 140);
this.btnStopCamera.Location = new System.Drawing.Point(725, 177);
this.btnStopCamera.Name = "btnStopCamera";
this.btnStopCamera.Size = new System.Drawing.Size(63, 31);
this.btnStopCamera.TabIndex = 10;
this.btnStopCamera.TabIndex = 13;
this.btnStopCamera.Text = "停止";
this.btnStopCamera.UseVisualStyleBackColor = true;
this.btnStopCamera.Click += new System.EventHandler(this.btnStopCamera_Click);
@@ -210,6 +240,9 @@ namespace Test
this.Controls.Add(this.btnDetectLeds);
this.Controls.Add(this.btnStartCapture);
this.Controls.Add(this.btnStartCamera);
this.Controls.Add(this.btnSetIP);
this.Controls.Add(this.txtIpAddress);
this.Controls.Add(this.lblIpAddress);
this.Controls.Add(this.btnSetConfigFile);
this.Controls.Add(this.btnInitialize);
this.Controls.Add(this.LedImage);
@@ -243,5 +276,8 @@ namespace Test
private System.Windows.Forms.ToolStripStatusLabel lblStatus;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtLog;
private System.Windows.Forms.Label lblIpAddress;
private System.Windows.Forms.TextBox txtIpAddress;
private System.Windows.Forms.Button btnSetIP;
}
}

View File

@@ -3,10 +3,13 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ErrorEventArgs = XCamera.Models.ErrorEventArgs;
using InfoEventArgs = XCamera.Models.InfoEventArgs;
using XCamera.Models;
namespace Test
@@ -16,6 +19,7 @@ namespace Test
private bool _isInitialized = false;
private bool _isConnected = false;
private bool _isCapturing = false;
private string _cameraIpAddress = "192.168.100.10"; // 默认IP地址
public Form1()
{
@@ -44,6 +48,8 @@ namespace Test
global::XCamera.XCamera.ConnectionStateChanged += OnConnectionStateChanged;
global::XCamera.XCamera.CaptureStateChanged += OnCaptureStateChanged;
global::XCamera.XCamera.ErrorOccurred += OnErrorOccurred;
global::XCamera.XCamera.InfoOccurred += OnInfoOccurred;
global::XCamera.XCamera.ImageCaptured += OnImageCaptured;
}
/// <summary>
@@ -53,28 +59,18 @@ namespace Test
{
try
{
LogMessage("正在初始化...");
LogMessage("正在初始化SDK...");
// 使用默认配置文件路径
string configPath = System.IO.Path.Combine(Application.StartupPath, "config", "sample_config.json");
if (global::XCamera.XCamera.Initialize(configPath))
// 初始化SDK不加载配置文件
if (global::XCamera.XCamera.Initialize())
{
_isInitialized = true;
LogMessage("初始化成功!");
// 显示配置信息
var config = global::XCamera.XCamera.GetCurrentConfig();
if (config != null)
{
LogMessage($"摄像头IP: {config.IpAddress}");
LogMessage($"TCP端口: {config.TcpPort}");
LogMessage($"LED区域数: {config.LedRegions.Count}");
}
LogMessage("SDK初始化成功!");
LogMessage("现在可以设置配置文件或启动摄像头");
}
else
{
LogMessage("初始化失败!");
LogMessage("SDK初始化失败!");
}
}
catch (Exception ex)
@@ -92,42 +88,91 @@ namespace Test
{
try
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
{
openFileDialog.Filter = "JSON文件|*.json|所有文件|*.*";
openFileDialog.Title = "选择配置文件";
if (openFileDialog.ShowDialog() == DialogResult.OK)
// 设置默认路径为程序目录下的config文件夹
string defaultConfigPath = Path.Combine(Application.StartupPath, "config");
if (Directory.Exists(defaultConfigPath))
{
LogMessage($"正在加载配置文件: {openFileDialog.FileName}");
folderDialog.SelectedPath = defaultConfigPath;
}
else
{
folderDialog.SelectedPath = Application.StartupPath;
}
folderDialog.Description = "选择配置目录";
folderDialog.ShowNewFolderButton = false;
if (folderDialog.ShowDialog() == DialogResult.OK)
{
LogMessage($"正在设置配置目录: {folderDialog.SelectedPath}");
if (global::XCamera.XCamera.SetConfigFile(openFileDialog.FileName))
if (global::XCamera.XCamera.SetConfigPath(folderDialog.SelectedPath))
{
LogMessage("配置文件加载成功!");
LogMessage("配置目录设置成功!");
// 显示配置信息
var config = global::XCamera.XCamera.GetCurrentConfig();
if (config != null)
{
LogMessage($"摄像头IP: {config.IpAddress}");
LogMessage($"LED区域数: {config.LedRegions.Count}");
LogMessage($"TCP端口: {config.TcpPort}");
LogMessage($"用户名: {config.Username}");
LogMessage($"通道: {config.Channel}");
}
}
else
{
LogMessage("配置文件加载失败!");
LogMessage("配置目录设置失败!");
}
}
}
}
catch (Exception ex)
{
LogMessage($"配置文件加载错误: {ex.Message}");
LogMessage($"配置目录设置错误: {ex.Message}");
}
}
/// <summary>
/// 3. 启动摄像头
/// 3. 设置IP地址
/// </summary>
private void btnSetIP_Click(object sender, EventArgs e)
{
try
{
if (!_isInitialized)
{
LogMessage("请先初始化!");
return;
}
string ipAddress = txtIpAddress.Text.Trim();
if (string.IsNullOrEmpty(ipAddress))
{
LogMessage("IP地址不能为空");
return;
}
if (global::XCamera.XCamera.SetIP(ipAddress))
{
_cameraIpAddress = ipAddress;
LogMessage($"IP地址设置成功: {ipAddress}");
}
else
{
LogMessage($"IP地址设置失败: {ipAddress}");
}
}
catch (Exception ex)
{
LogMessage($"设置IP地址错误: {ex.Message}");
}
}
/// <summary>
/// 5. 启动摄像头
/// </summary>
private void btnStartCamera_Click(object sender, EventArgs e)
{
@@ -159,7 +204,7 @@ namespace Test
}
/// <summary>
/// 4. 开始捕获图像
/// 6. 开始捕获图像
/// </summary>
private void btnStartCapture_Click(object sender, EventArgs e)
{
@@ -197,7 +242,7 @@ namespace Test
}
/// <summary>
/// 5. LED灯识别
/// 7. LED灯识别
/// </summary>
private void btnDetectLeds_Click(object sender, EventArgs e)
{
@@ -229,7 +274,7 @@ namespace Test
}
/// <summary>
/// 6. 状态查询
/// 8. 状态查询
/// </summary>
private void btnGetLedStatuses_Click(object sender, EventArgs e)
{
@@ -250,6 +295,9 @@ namespace Test
{
LogMessage($" {status.GetStatusDescription()}");
}
// 显示图像
DisplayLedImage(statuses);
}
catch (Exception ex)
{
@@ -258,7 +306,7 @@ namespace Test
}
/// <summary>
/// 7. 区域设置窗口
/// 9. 区域编辑器
/// </summary>
private void btnShowRegionEditor_Click(object sender, EventArgs e)
{
@@ -275,37 +323,47 @@ namespace Test
}
/// <summary>
/// 8. 资源释放
/// 9. 关闭
/// </summary>
private void btnShutdown_Click(object sender, EventArgs e)
{
try
{
LogMessage("正在释放资源...");
LogMessage("正在关闭...");
// 停止捕获
if (_isCapturing)
{
global::XCamera.XCamera.StopCapture();
LogMessage("捕获已停止");
}
// 停止摄像头
if (_isConnected)
{
global::XCamera.XCamera.StopCamera();
LogMessage("摄像头已停止");
}
global::XCamera.XCamera.Shutdown();
// 关闭SDK
if (_isInitialized)
{
global::XCamera.XCamera.Shutdown();
LogMessage("SDK已关闭");
}
_isInitialized = false;
_isConnected = false;
_isCapturing = false;
LogMessage("资源已释放");
LogMessage("关闭完成!");
}
catch (Exception ex)
{
LogMessage($"资源释放错误: {ex.Message}");
LogMessage($"关闭错误: {ex.Message}");
}
// 重置状态
_isInitialized = false;
_isConnected = false;
_isCapturing = false;
UpdateUI();
}
@@ -318,13 +376,14 @@ namespace Test
{
if (!_isCapturing)
{
LogMessage("当前未在捕获");
LogMessage("当前未在捕获状态");
return;
}
LogMessage("正在停止捕获...");
global::XCamera.XCamera.StopCapture();
LogMessage("捕获已停止");
LogMessage("停止捕获成功!");
}
catch (Exception ex)
{
@@ -343,19 +402,14 @@ namespace Test
{
if (!_isConnected)
{
LogMessage("摄像头未连接");
LogMessage("当前未连接摄像头!");
return;
}
LogMessage("正在停止摄像头...");
if (_isCapturing)
{
global::XCamera.XCamera.StopCapture();
}
global::XCamera.XCamera.StopCamera();
LogMessage("摄像头已停止");
LogMessage("停止摄像头成功!");
}
catch (Exception ex)
{
@@ -376,10 +430,23 @@ namespace Test
return;
}
LogMessage($"[{DateTime.Now:HH:mm:ss.fff}] LED状态更新:");
foreach (var status in e.Statuses)
// 更新状态
_isConnected = global::XCamera.XCamera.IsConnected;
_isCapturing = global::XCamera.XCamera.IsCapturing;
UpdateUI();
// 显示LED状态
if (e.Statuses.Count > 0)
{
LogMessage($" {status.GetStatusDescription()}");
LogMessage($"LED状态更新 ({e.Statuses.Count} 个):");
foreach (var status in e.Statuses)
{
LogMessage($" {status.GetStatusDescription()}");
}
// 显示图像
DisplayLedImage(e.Statuses);
}
}
@@ -395,8 +462,16 @@ namespace Test
}
_isConnected = e.IsConnected;
LogMessage($"[{DateTime.Now:HH:mm:ss.fff}] 摄像头连接状态: {(e.IsConnected ? "" : "")}");
UpdateUI();
if (e.IsConnected)
{
LogMessage("摄像头已连接");
}
else
{
LogMessage("摄像头已断开");
}
}
/// <summary>
@@ -411,8 +486,16 @@ namespace Test
}
_isCapturing = e.IsCapturing;
LogMessage($"[{DateTime.Now:HH:mm:ss.fff}] 图像捕获状态: {(e.IsCapturing ? "" : "")}");
UpdateUI();
if (e.IsCapturing)
{
LogMessage("开始捕获图像");
}
else
{
LogMessage("停止捕获图像");
}
}
/// <summary>
@@ -430,19 +513,123 @@ namespace Test
}
/// <summary>
/// 日志消息
/// 信息事件处理
/// </summary>
private void LogMessage(string message)
private void OnInfoOccurred(object sender, InfoEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(LogMessage), message);
BeginInvoke(new Action<object, InfoEventArgs>(OnInfoOccurred), sender, e);
return;
}
string timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
txtLog.AppendText($"[{timestamp}] {message}" + Environment.NewLine);
txtLog.ScrollToCaret();
LogMessage($"[{DateTime.Now:HH:mm:ss.fff}] {e.InfoMessage}");
}
/// <summary>
/// 图像捕获事件处理
/// </summary>
private void OnImageCaptured(object sender, ImageCapturedEventArgs e)
{
if (InvokeRequired)
{
BeginInvoke(new Action<object, ImageCapturedEventArgs>(OnImageCaptured), sender, e);
return;
}
try
{
// 显示捕获的图像
if (e.Image != null)
{
// 清理之前的图像
if (LedImage.Image != null)
{
var oldImage = LedImage.Image;
LedImage.Image = null;
oldImage.Dispose();
}
// 如果LED状态为空直接显示原图
if (e.LedStatuses == null || e.LedStatuses.Count == 0)
{
LedImage.Image = (System.Drawing.Image)e.Image.Clone();
}
else
{
// 创建带LED区域标注的图像
var annotatedImage = CreateAnnotatedImage(e.Image, e.LedStatuses);
LedImage.Image = annotatedImage;
}
// 调整显示模式
LedImage.SizeMode = PictureBoxSizeMode.Zoom;
}
}
catch (Exception ex)
{
LogMessage($"显示图像失败: {ex.Message}");
}
}
/// <summary>
/// 创建带LED区域标注的图像
/// </summary>
private System.Drawing.Image CreateAnnotatedImage(System.Drawing.Image originalImage, List<LedStatus> ledStatuses)
{
// 创建位图副本
var bitmap = new Bitmap(originalImage.Width, originalImage.Height);
using (var g = Graphics.FromImage(bitmap))
{
// 绘制原始图像
g.DrawImage(originalImage, 0, 0, originalImage.Width, originalImage.Height);
// 绘制LED状态信息
for (int i = 0; i < ledStatuses.Count; i++)
{
var status = ledStatuses[i];
// 根据状态设置颜色
Color ledColor = GetLedColor(status);
// 计算LED显示位置
int ledSize = 40;
int spacing = 60;
int startX = 20;
int startY = 20;
int x = startX + (i % 5) * spacing;
int y = startY + (i / 5) * spacing;
// 绘制LED圆形
Rectangle ledRect = new Rectangle(x, y, ledSize, ledSize);
using (var ledBrush = new SolidBrush(ledColor))
{
g.FillEllipse(ledBrush, ledRect);
}
// 绘制LED边框
using (var borderPen = new Pen(Color.Black, 2))
{
g.DrawEllipse(borderPen, ledRect);
}
// 绘制标签
using (var font = new Font("Arial", 8))
using (var textBrush = new SolidBrush(Color.White))
{
string label = status.RegionName;
g.DrawString(label, font, textBrush, x, y + ledSize + 2);
string stateText = status.State.ToString();
if (status.IsBlinking)
stateText = "Blinking";
g.DrawString(stateText, font, textBrush, x, y + ledSize + 15);
}
}
}
return bitmap;
}
/// <summary>
@@ -552,11 +739,7 @@ namespace Test
{
try
{
// 避免在设计模式下执行外部代码
if (DesignMode)
return;
// 确保释放资源
// 确保在关闭时清理资源
if (_isCapturing)
{
global::XCamera.XCamera.StopCapture();
@@ -572,10 +755,27 @@ namespace Test
global::XCamera.XCamera.Shutdown();
}
}
catch
catch (Exception ex)
{
// 忽略关闭时的错误
System.Diagnostics.Debug.WriteLine($"关闭错误: {ex.Message}");
}
}
/// <summary>
/// 日志消息
/// </summary>
private void LogMessage(string message)
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(LogMessage), message);
return;
}
string timestamp = DateTime.Now.ToString("HH:mm:ss.fff");
txtLog.AppendText($"[{timestamp}] {message}" + Environment.NewLine);
txtLog.ScrollToCaret();
}
}
}

View File

@@ -42,6 +42,7 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.VisualBasic" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XCamera\XCamera.csproj">

View File

@@ -0,0 +1,14 @@
{
"Server": {
"SID": "98FE3E132FD026031709254777EA-XCloudSDK"
},
"Log": {
"UIEnbale": 0,
"FileEnbale": 1,
"NetEnbale": 0,
"ServerIP": "",
"Port": 0,
"LogLevel": 8,
"BackupDays": 7
}
}