捕获图像

This commit is contained in:
zqm
2026-03-18 08:58:39 +08:00
parent 7cd923b91b
commit 47733146ac
8 changed files with 208 additions and 81 deletions

View File

@@ -46,27 +46,31 @@ namespace Test
this.lblIpAddress = new System.Windows.Forms.Label(); this.lblIpAddress = new System.Windows.Forms.Label();
this.txtIpAddress = new System.Windows.Forms.TextBox(); this.txtIpAddress = new System.Windows.Forms.TextBox();
this.btnSetIP = new System.Windows.Forms.Button(); this.btnSetIP = new System.Windows.Forms.Button();
this.camera = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.LedImage)).BeginInit();
this.statusStrip1.SuspendLayout(); this.statusStrip1.SuspendLayout();
this.groupBox1.SuspendLayout(); this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.camera)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// LedImage // LedImage
// //
this.LedImage.BackColor = System.Drawing.SystemColors.ControlDark; this.LedImage.BackColor = System.Drawing.SystemColors.ControlDark;
this.LedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.LedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.LedImage.Location = new System.Drawing.Point(12, 12); this.LedImage.Location = new System.Drawing.Point(16, 15);
this.LedImage.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.LedImage.Name = "LedImage"; this.LedImage.Name = "LedImage";
this.LedImage.Size = new System.Drawing.Size(557, 426); this.LedImage.Size = new System.Drawing.Size(742, 532);
this.LedImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.LedImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.LedImage.TabIndex = 0; this.LedImage.TabIndex = 0;
this.LedImage.TabStop = false; this.LedImage.TabStop = false;
// //
// btnInitialize // btnInitialize
// //
this.btnInitialize.Location = new System.Drawing.Point(608, 38); this.btnInitialize.Location = new System.Drawing.Point(811, 48);
this.btnInitialize.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnInitialize.Name = "btnInitialize"; this.btnInitialize.Name = "btnInitialize";
this.btnInitialize.Size = new System.Drawing.Size(111, 31); this.btnInitialize.Size = new System.Drawing.Size(148, 39);
this.btnInitialize.TabIndex = 1; this.btnInitialize.TabIndex = 1;
this.btnInitialize.Text = "1. 初始化"; this.btnInitialize.Text = "1. 初始化";
this.btnInitialize.UseVisualStyleBackColor = true; this.btnInitialize.UseVisualStyleBackColor = true;
@@ -74,46 +78,21 @@ namespace Test
// //
// btnSetConfigFile // btnSetConfigFile
// //
this.btnSetConfigFile.Location = new System.Drawing.Point(608, 89); this.btnSetConfigFile.Location = new System.Drawing.Point(811, 111);
this.btnSetConfigFile.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnSetConfigFile.Name = "btnSetConfigFile"; this.btnSetConfigFile.Name = "btnSetConfigFile";
this.btnSetConfigFile.Size = new System.Drawing.Size(111, 31); this.btnSetConfigFile.Size = new System.Drawing.Size(148, 39);
this.btnSetConfigFile.TabIndex = 2; this.btnSetConfigFile.TabIndex = 2;
this.btnSetConfigFile.Text = "2. 设置配置文件"; this.btnSetConfigFile.Text = "2. 设置配置文件";
this.btnSetConfigFile.UseVisualStyleBackColor = true; this.btnSetConfigFile.UseVisualStyleBackColor = true;
this.btnSetConfigFile.Click += new System.EventHandler(this.btnSetConfigFile_Click); 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 // btnStartCamera
// //
this.btnStartCamera.Location = new System.Drawing.Point(608, 177); this.btnStartCamera.Location = new System.Drawing.Point(811, 221);
this.btnStartCamera.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnStartCamera.Name = "btnStartCamera"; this.btnStartCamera.Name = "btnStartCamera";
this.btnStartCamera.Size = new System.Drawing.Size(111, 31); this.btnStartCamera.Size = new System.Drawing.Size(148, 39);
this.btnStartCamera.TabIndex = 6; this.btnStartCamera.TabIndex = 6;
this.btnStartCamera.Text = "4. 启动摄像头"; this.btnStartCamera.Text = "4. 启动摄像头";
this.btnStartCamera.UseVisualStyleBackColor = true; this.btnStartCamera.UseVisualStyleBackColor = true;
@@ -121,9 +100,10 @@ namespace Test
// //
// btnStartCapture // btnStartCapture
// //
this.btnStartCapture.Location = new System.Drawing.Point(608, 228); this.btnStartCapture.Location = new System.Drawing.Point(811, 285);
this.btnStartCapture.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnStartCapture.Name = "btnStartCapture"; this.btnStartCapture.Name = "btnStartCapture";
this.btnStartCapture.Size = new System.Drawing.Size(111, 31); this.btnStartCapture.Size = new System.Drawing.Size(148, 39);
this.btnStartCapture.TabIndex = 7; this.btnStartCapture.TabIndex = 7;
this.btnStartCapture.Text = "5. 开始捕获"; this.btnStartCapture.Text = "5. 开始捕获";
this.btnStartCapture.UseVisualStyleBackColor = true; this.btnStartCapture.UseVisualStyleBackColor = true;
@@ -131,9 +111,10 @@ namespace Test
// //
// btnDetectLeds // btnDetectLeds
// //
this.btnDetectLeds.Location = new System.Drawing.Point(608, 279); this.btnDetectLeds.Location = new System.Drawing.Point(811, 349);
this.btnDetectLeds.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnDetectLeds.Name = "btnDetectLeds"; this.btnDetectLeds.Name = "btnDetectLeds";
this.btnDetectLeds.Size = new System.Drawing.Size(111, 31); this.btnDetectLeds.Size = new System.Drawing.Size(148, 39);
this.btnDetectLeds.TabIndex = 8; this.btnDetectLeds.TabIndex = 8;
this.btnDetectLeds.Text = "6. LED识别"; this.btnDetectLeds.Text = "6. LED识别";
this.btnDetectLeds.UseVisualStyleBackColor = true; this.btnDetectLeds.UseVisualStyleBackColor = true;
@@ -141,9 +122,10 @@ namespace Test
// //
// btnGetLedStatuses // btnGetLedStatuses
// //
this.btnGetLedStatuses.Location = new System.Drawing.Point(608, 330); this.btnGetLedStatuses.Location = new System.Drawing.Point(811, 412);
this.btnGetLedStatuses.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnGetLedStatuses.Name = "btnGetLedStatuses"; this.btnGetLedStatuses.Name = "btnGetLedStatuses";
this.btnGetLedStatuses.Size = new System.Drawing.Size(111, 31); this.btnGetLedStatuses.Size = new System.Drawing.Size(148, 39);
this.btnGetLedStatuses.TabIndex = 9; this.btnGetLedStatuses.TabIndex = 9;
this.btnGetLedStatuses.Text = "7. 状态查询"; this.btnGetLedStatuses.Text = "7. 状态查询";
this.btnGetLedStatuses.UseVisualStyleBackColor = true; this.btnGetLedStatuses.UseVisualStyleBackColor = true;
@@ -151,9 +133,10 @@ namespace Test
// //
// btnShowRegionEditor // btnShowRegionEditor
// //
this.btnShowRegionEditor.Location = new System.Drawing.Point(608, 381); this.btnShowRegionEditor.Location = new System.Drawing.Point(811, 476);
this.btnShowRegionEditor.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnShowRegionEditor.Name = "btnShowRegionEditor"; this.btnShowRegionEditor.Name = "btnShowRegionEditor";
this.btnShowRegionEditor.Size = new System.Drawing.Size(111, 31); this.btnShowRegionEditor.Size = new System.Drawing.Size(148, 39);
this.btnShowRegionEditor.TabIndex = 10; this.btnShowRegionEditor.TabIndex = 10;
this.btnShowRegionEditor.Text = "8. 区域编辑器"; this.btnShowRegionEditor.Text = "8. 区域编辑器";
this.btnShowRegionEditor.UseVisualStyleBackColor = true; this.btnShowRegionEditor.UseVisualStyleBackColor = true;
@@ -161,9 +144,10 @@ namespace Test
// //
// btnShutdown // btnShutdown
// //
this.btnShutdown.Location = new System.Drawing.Point(608, 432); this.btnShutdown.Location = new System.Drawing.Point(811, 540);
this.btnShutdown.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnShutdown.Name = "btnShutdown"; this.btnShutdown.Name = "btnShutdown";
this.btnShutdown.Size = new System.Drawing.Size(111, 31); this.btnShutdown.Size = new System.Drawing.Size(148, 39);
this.btnShutdown.TabIndex = 11; this.btnShutdown.TabIndex = 11;
this.btnShutdown.Text = "9. 关闭"; this.btnShutdown.Text = "9. 关闭";
this.btnShutdown.UseVisualStyleBackColor = true; this.btnShutdown.UseVisualStyleBackColor = true;
@@ -171,9 +155,10 @@ namespace Test
// //
// btnStopCapture // btnStopCapture
// //
this.btnStopCapture.Location = new System.Drawing.Point(725, 228); this.btnStopCapture.Location = new System.Drawing.Point(967, 285);
this.btnStopCapture.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnStopCapture.Name = "btnStopCapture"; this.btnStopCapture.Name = "btnStopCapture";
this.btnStopCapture.Size = new System.Drawing.Size(63, 31); this.btnStopCapture.Size = new System.Drawing.Size(84, 39);
this.btnStopCapture.TabIndex = 12; this.btnStopCapture.TabIndex = 12;
this.btnStopCapture.Text = "停止"; this.btnStopCapture.Text = "停止";
this.btnStopCapture.UseVisualStyleBackColor = true; this.btnStopCapture.UseVisualStyleBackColor = true;
@@ -181,9 +166,10 @@ namespace Test
// //
// btnStopCamera // btnStopCamera
// //
this.btnStopCamera.Location = new System.Drawing.Point(725, 177); this.btnStopCamera.Location = new System.Drawing.Point(967, 221);
this.btnStopCamera.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnStopCamera.Name = "btnStopCamera"; this.btnStopCamera.Name = "btnStopCamera";
this.btnStopCamera.Size = new System.Drawing.Size(63, 31); this.btnStopCamera.Size = new System.Drawing.Size(84, 39);
this.btnStopCamera.TabIndex = 13; this.btnStopCamera.TabIndex = 13;
this.btnStopCamera.Text = "停止"; this.btnStopCamera.Text = "停止";
this.btnStopCamera.UseVisualStyleBackColor = true; this.btnStopCamera.UseVisualStyleBackColor = true;
@@ -191,26 +177,30 @@ namespace Test
// //
// statusStrip1 // statusStrip1
// //
this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.lblStatus}); this.lblStatus});
this.statusStrip1.Location = new System.Drawing.Point(0, 451); this.statusStrip1.Location = new System.Drawing.Point(0, 693);
this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(800, 22); this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 19, 0);
this.statusStrip1.Size = new System.Drawing.Size(1067, 25);
this.statusStrip1.TabIndex = 11; this.statusStrip1.TabIndex = 11;
this.statusStrip1.Text = "statusStrip1"; this.statusStrip1.Text = "statusStrip1";
// //
// lblStatus // lblStatus
// //
this.lblStatus.Name = "lblStatus"; this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(68, 17); this.lblStatus.Size = new System.Drawing.Size(107, 20);
this.lblStatus.Text = "状态: 未初始化"; this.lblStatus.Text = "状态: 未初始化";
// //
// groupBox1 // groupBox1
// //
this.groupBox1.Controls.Add(this.txtLog); this.groupBox1.Controls.Add(this.txtLog);
this.groupBox1.Location = new System.Drawing.Point(12, 444); this.groupBox1.Location = new System.Drawing.Point(16, 555);
this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.groupBox1.Name = "groupBox1"; this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(557, 100); this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.groupBox1.Size = new System.Drawing.Size(743, 125);
this.groupBox1.TabIndex = 12; this.groupBox1.TabIndex = 12;
this.groupBox1.TabStop = false; this.groupBox1.TabStop = false;
this.groupBox1.Text = "日志"; this.groupBox1.Text = "日志";
@@ -218,18 +208,58 @@ namespace Test
// txtLog // txtLog
// //
this.txtLog.Dock = System.Windows.Forms.DockStyle.Fill; this.txtLog.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtLog.Location = new System.Drawing.Point(3, 17); this.txtLog.Location = new System.Drawing.Point(4, 22);
this.txtLog.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtLog.Multiline = true; this.txtLog.Multiline = true;
this.txtLog.Name = "txtLog"; this.txtLog.Name = "txtLog";
this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtLog.Size = new System.Drawing.Size(551, 80); this.txtLog.Size = new System.Drawing.Size(735, 99);
this.txtLog.TabIndex = 0; this.txtLog.TabIndex = 0;
// //
// lblIpAddress
//
this.lblIpAddress.AutoSize = true;
this.lblIpAddress.Location = new System.Drawing.Point(811, 156);
this.lblIpAddress.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.lblIpAddress.Name = "lblIpAddress";
this.lblIpAddress.Size = new System.Drawing.Size(61, 15);
this.lblIpAddress.TabIndex = 3;
this.lblIpAddress.Text = "IP地址:";
//
// txtIpAddress
//
this.txtIpAddress.Location = new System.Drawing.Point(900, 152);
this.txtIpAddress.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.txtIpAddress.Name = "txtIpAddress";
this.txtIpAddress.Size = new System.Drawing.Size(132, 25);
this.txtIpAddress.TabIndex = 4;
this.txtIpAddress.Text = "192.168.100.10";
//
// btnSetIP
//
this.btnSetIP.Location = new System.Drawing.Point(811, 175);
this.btnSetIP.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnSetIP.Name = "btnSetIP";
this.btnSetIP.Size = new System.Drawing.Size(148, 39);
this.btnSetIP.TabIndex = 5;
this.btnSetIP.Text = "3. 设置IP地址";
this.btnSetIP.UseVisualStyleBackColor = true;
this.btnSetIP.Click += new System.EventHandler(this.btnSetIP_Click);
//
// camera
//
this.camera.Location = new System.Drawing.Point(980, 20);
this.camera.Name = "camera";
this.camera.Size = new System.Drawing.Size(52, 50);
this.camera.TabIndex = 14;
this.camera.TabStop = false;
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
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(1067, 718);
this.Controls.Add(this.camera);
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);
@@ -246,6 +276,7 @@ namespace Test
this.Controls.Add(this.btnSetConfigFile); this.Controls.Add(this.btnSetConfigFile);
this.Controls.Add(this.btnInitialize); this.Controls.Add(this.btnInitialize);
this.Controls.Add(this.LedImage); this.Controls.Add(this.LedImage);
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "Form1"; this.Name = "Form1";
this.Text = "XCamera LED灯识别测试程序"; this.Text = "XCamera LED灯识别测试程序";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
@@ -254,6 +285,7 @@ namespace Test
this.statusStrip1.PerformLayout(); this.statusStrip1.PerformLayout();
this.groupBox1.ResumeLayout(false); this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout(); this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.camera)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@@ -279,5 +311,6 @@ namespace Test
private System.Windows.Forms.Label lblIpAddress; private System.Windows.Forms.Label lblIpAddress;
private System.Windows.Forms.TextBox txtIpAddress; private System.Windows.Forms.TextBox txtIpAddress;
private System.Windows.Forms.Button btnSetIP; private System.Windows.Forms.Button btnSetIP;
private System.Windows.Forms.PictureBox camera;
} }
} }

View File

@@ -186,7 +186,8 @@ namespace Test
LogMessage("正在启动摄像头..."); LogMessage("正在启动摄像头...");
if (global::XCamera.XCamera.StartCamera()) // 使用camera控件的窗口句柄进行预览
if (global::XCamera.XCamera.StartCamera(camera.Handle))
{ {
LogMessage("摄像头启动成功!"); LogMessage("摄像头启动成功!");
} }

View File

@@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root> </root>

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using XCamera.Models; using XCamera.Models;
namespace XCamera.Core namespace XCamera.Core
@@ -82,8 +83,9 @@ namespace XCamera.Core
/// 初始化连接管理器 /// 初始化连接管理器
/// </summary> /// </summary>
/// <param name="config">摄像头配置</param> /// <param name="config">摄像头配置</param>
/// <param name="userHandle">用户句柄</param>
/// <returns>是否成功初始化</returns> /// <returns>是否成功初始化</returns>
public bool Initialize(CameraConfig config) public bool Initialize(CameraConfig config, int userHandle = -1)
{ {
if (config == null) if (config == null)
{ {
@@ -99,11 +101,36 @@ namespace XCamera.Core
lock (_lockObject) lock (_lockObject)
{ {
_config = config; _config = config;
_userHandle = userHandle;
} }
return true; return true;
} }
/// <summary>
/// 获取设备信息JSON格式
/// </summary>
private string GetDeviceInfoJson()
{
// 手动构建JSON字符串
StringBuilder json = new StringBuilder();
json.Append("{");
json.Append($"\"DevId\":\"{DeviceId}\",");
json.Append($"\"DevName\":\"Camera_{DeviceId}\",");
json.Append("\"DevType\":\"IPCamera\",");
json.Append($"\"UserName\":\"{_config.Username}\",");
json.Append($"\"Password\":\"{_config.Password}\",");
json.Append($"\"Address\":\"{_config.IpAddress ?? "192.168.100.10"}\",");
json.Append($"\"Port\":{_config.TcpPort},");
json.Append($"\"Channel\":{_config.Channel},");
json.Append("\"StreamType\":0,");
json.Append("\"Protocol\":\"TCP\",");
json.Append("\"Enable\":true");
json.Append("}");
return json.ToString();
}
/// <summary> /// <summary>
/// 获取配置验证错误信息 /// 获取配置验证错误信息
/// </summary> /// </summary>
@@ -162,14 +189,6 @@ namespace XCamera.Core
throw new Exception($"设置设备用户名密码失败,错误码: {result}"); throw new Exception($"设置设备用户名密码失败,错误码: {result}");
} }
// 设备登录
result = XCloudSdkWrapper.XCloudSDK_Device_DevLogin(_userHandle, DeviceId, 1);
if (result < 0)
{
throw new Exception($"设备登录失败,错误码: {result}");
}
lock (_lockObject) lock (_lockObject)
{ {
_isConnected = true; _isConnected = true;
@@ -206,9 +225,6 @@ namespace XCamera.Core
StopRealPlay(); StopRealPlay();
} }
// 设备登出
XCloudSdkWrapper.XCloudSDK_Device_DevLogout(DeviceId);
// 删除设备信息 // 删除设备信息
XCloudSdkWrapper.XCloudSDK_Device_DeleteDevsInfo(DeviceId); XCloudSdkWrapper.XCloudSDK_Device_DeleteDevsInfo(DeviceId);
@@ -249,7 +265,7 @@ namespace XCamera.Core
{ {
// 开始实时预览 // 开始实时预览
int playHandle = XCloudSdkWrapper.XCloudSDK_Device_MediaRealPlay( int playHandle = XCloudSdkWrapper.XCloudSDK_Device_MediaRealPlay(
_userHandle, DeviceId, _config.Channel, 0, hWnd, 1, ""); _userHandle, DeviceId, 0, 0, hWnd, 0, "");
if (playHandle < 0) if (playHandle < 0)
{ {
@@ -267,6 +283,11 @@ namespace XCamera.Core
} }
catch (Exception ex) catch (Exception ex)
{ {
lock (_lockObject)
{
_isPlaying = false;
_playHandle = -1;
}
OnPlayStateChanged(); OnPlayStateChanged();
throw new Exception($"开始实时预览失败: {ex.Message}", ex); throw new Exception($"开始实时预览失败: {ex.Message}", ex);
} }

View File

@@ -149,6 +149,14 @@ namespace XCamera.Core
[DllImport("XCloudSDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] [DllImport("XCloudSDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int XCloudSDK_Device_DeleteDevsInfo(string sDevId); public static extern int XCloudSDK_Device_DeleteDevsInfo(string sDevId);
/// <summary>
/// 添加设备信息到数据中心
/// </summary>
/// <param name="sDevInfo">设备信息JSON格式</param>
/// <returns>>=0:成功; <0:失败;</returns>
[DllImport("XCloudSDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int XCloudSDK_Device_AddDevInfoToDC(string sDevInfo);
#endregion #endregion
#region #region

View File

@@ -8,6 +8,11 @@ namespace XCamera.Models
/// </summary> /// </summary>
public class CameraConfig public class CameraConfig
{ {
/// <summary>
/// IP地址
/// </summary>
public string IpAddress { get; set; }
/// <summary> /// <summary>
/// TCP端口固定34567 /// TCP端口固定34567
/// </summary> /// </summary>
@@ -99,7 +104,11 @@ namespace XCamera.Models
/// </summary> /// </summary>
public string GetDeviceId() public string GetDeviceId()
{ {
return $"DEV_{TcpPort}_{Channel}"; if (!string.IsNullOrEmpty(IpAddress))
{
return $"{IpAddress}:{TcpPort}";
}
return "192.168.100.10:34567";
} }
/// <summary> /// <summary>

View File

@@ -144,10 +144,11 @@ namespace XCamera
/// <summary> /// <summary>
/// 3. 根据配置文件内容启动摄像头 /// 3. 根据配置文件内容启动摄像头
/// </summary> /// </summary>
/// <param name="previewHandle">预览窗口句柄可为IntPtr.Zero</param>
/// <returns>是否成功启动</returns> /// <returns>是否成功启动</returns>
public static bool StartCamera() public static bool StartCamera(IntPtr previewHandle = default(IntPtr))
{ {
return Manager.StartCamera(); return Manager.StartCamera(previewHandle);
} }
/// <summary> /// <summary>

View File

@@ -26,6 +26,7 @@ namespace XCamera
private readonly object _lockObject = new object(); private readonly object _lockObject = new object();
private int _captureInterval = 1000; private int _captureInterval = 1000;
private string _cameraIpAddress = "192.168.100.10"; // 摄像头IP地址 private string _cameraIpAddress = "192.168.100.10"; // 摄像头IP地址
private int _userHandle = -1; // 用户句柄
/// <summary> /// <summary>
/// LED状态更新事件 /// LED状态更新事件
@@ -148,6 +149,9 @@ namespace XCamera
throw new Exception($"注册回调函数失败,错误码: {userHandle}"); throw new Exception($"注册回调函数失败,错误码: {userHandle}");
} }
// 保存用户句柄
_userHandle = userHandle;
// 仅在提供了配置文件路径时加载配置 // 仅在提供了配置文件路径时加载配置
if (!string.IsNullOrEmpty(configFilePath)) if (!string.IsNullOrEmpty(configFilePath))
{ {
@@ -223,8 +227,9 @@ namespace XCamera
/// <summary> /// <summary>
/// 3. 根据配置文件内容启动摄像头 /// 3. 根据配置文件内容启动摄像头
/// </summary> /// </summary>
/// <param name="previewHandle">预览窗口句柄可为IntPtr.Zero</param>
/// <returns>是否成功启动</returns> /// <returns>是否成功启动</returns>
public bool StartCamera() public bool StartCamera(IntPtr previewHandle = default(IntPtr))
{ {
if (!_isInitialized) if (!_isInitialized)
{ {
@@ -241,7 +246,7 @@ namespace XCamera
try try
{ {
// 初始化连接管理器 // 初始化连接管理器
_connectionManager.Initialize(CurrentConfig); _connectionManager.Initialize(CurrentConfig, _userHandle);
// 连接到摄像头 // 连接到摄像头
if (!_connectionManager.Connect()) if (!_connectionManager.Connect())
@@ -249,8 +254,8 @@ namespace XCamera
return false; return false;
} }
// 开始实时预览(不显示窗口 // 开始实时预览(使用指定窗口句柄
if (!_connectionManager.StartRealPlay(IntPtr.Zero)) if (!_connectionManager.StartRealPlay(previewHandle))
{ {
_connectionManager.Disconnect(); _connectionManager.Disconnect();
return false; return false;
@@ -536,6 +541,12 @@ namespace XCamera
lock (_lockObject) lock (_lockObject)
{ {
_cameraIpAddress = ipAddress; _cameraIpAddress = ipAddress;
// 更新当前配置的IP地址
if (CurrentConfig != null)
{
CurrentConfig.IpAddress = ipAddress;
}
} }
// 使用信息事件而不是错误事件 // 使用信息事件而不是错误事件
@@ -643,8 +654,44 @@ namespace XCamera
{ {
try try
{ {
var ledStatuses = DetectLeds(); // 无论是否有LED区域都捕获图像
OnLedStatusUpdated(ledStatuses); string tempFile = System.IO.Path.GetTempFileName() + ".jpg";
try
{
if (_connectionManager.CaptureImage(tempFile))
{
// 加载图像并触发图像捕获事件
try
{
using (var image = System.Drawing.Image.FromFile(tempFile))
{
// 触发图像捕获事件无LED状态
OnImageCaptured((System.Drawing.Image)image.Clone(), tempFile);
}
}
catch (Exception imgEx)
{
OnErrorOccurred($"图像加载失败: {imgEx.Message}");
}
// 如果有LED区域再进行LED检测
if (CurrentConfig?.LedRegions != null && CurrentConfig.LedRegions.Count > 0)
{
var ledStatuses = DetectLeds();
OnLedStatusUpdated(ledStatuses);
}
else
{
// 没有LED区域只更新空状态
OnLedStatusUpdated(new List<LedStatus>());
}
}
}
finally
{
// 清理临时文件
try { System.IO.File.Delete(tempFile); } catch { }
}
// 等待指定间隔 // 等待指定间隔
int waited = 0; int waited = 0;
@@ -681,6 +728,8 @@ namespace XCamera
if (nParam1 < 0) if (nParam1 < 0)
{ {
OnErrorOccurred($"设备登录失败,错误码: {nParam1}"); OnErrorOccurred($"设备登录失败,错误码: {nParam1}");
// 设备登录失败,断开连接
_connectionManager?.Disconnect();
} }
} }
else if (nMsgId == (int)XCloudSdkMessage.MediaStartRealPlay) else if (nMsgId == (int)XCloudSdkMessage.MediaStartRealPlay)
@@ -688,6 +737,8 @@ namespace XCamera
if (nParam1 < 0) if (nParam1 < 0)
{ {
OnErrorOccurred($"开始实时预览失败,错误码: {nParam1}"); OnErrorOccurred($"开始实时预览失败,错误码: {nParam1}");
// 实时预览失败,更新连接管理器状态
_connectionManager?.StopRealPlay();
} }
} }