捕获图像
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using XCamera.Models;
|
||||
|
||||
namespace XCamera.Core
|
||||
@@ -82,8 +83,9 @@ namespace XCamera.Core
|
||||
/// 初始化连接管理器
|
||||
/// </summary>
|
||||
/// <param name="config">摄像头配置</param>
|
||||
/// <param name="userHandle">用户句柄</param>
|
||||
/// <returns>是否成功初始化</returns>
|
||||
public bool Initialize(CameraConfig config)
|
||||
public bool Initialize(CameraConfig config, int userHandle = -1)
|
||||
{
|
||||
if (config == null)
|
||||
{
|
||||
@@ -99,11 +101,36 @@ namespace XCamera.Core
|
||||
lock (_lockObject)
|
||||
{
|
||||
_config = config;
|
||||
_userHandle = userHandle;
|
||||
}
|
||||
|
||||
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>
|
||||
@@ -162,14 +189,6 @@ namespace XCamera.Core
|
||||
throw new Exception($"设置设备用户名密码失败,错误码: {result}");
|
||||
}
|
||||
|
||||
// 设备登录
|
||||
result = XCloudSdkWrapper.XCloudSDK_Device_DevLogin(_userHandle, DeviceId, 1);
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
throw new Exception($"设备登录失败,错误码: {result}");
|
||||
}
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
_isConnected = true;
|
||||
@@ -206,9 +225,6 @@ namespace XCamera.Core
|
||||
StopRealPlay();
|
||||
}
|
||||
|
||||
// 设备登出
|
||||
XCloudSdkWrapper.XCloudSDK_Device_DevLogout(DeviceId);
|
||||
|
||||
// 删除设备信息
|
||||
XCloudSdkWrapper.XCloudSDK_Device_DeleteDevsInfo(DeviceId);
|
||||
|
||||
@@ -249,7 +265,7 @@ namespace XCamera.Core
|
||||
{
|
||||
// 开始实时预览
|
||||
int playHandle = XCloudSdkWrapper.XCloudSDK_Device_MediaRealPlay(
|
||||
_userHandle, DeviceId, _config.Channel, 0, hWnd, 1, "");
|
||||
_userHandle, DeviceId, 0, 0, hWnd, 0, "");
|
||||
|
||||
if (playHandle < 0)
|
||||
{
|
||||
@@ -267,6 +283,11 @@ namespace XCamera.Core
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
lock (_lockObject)
|
||||
{
|
||||
_isPlaying = false;
|
||||
_playHandle = -1;
|
||||
}
|
||||
OnPlayStateChanged();
|
||||
throw new Exception($"开始实时预览失败: {ex.Message}", ex);
|
||||
}
|
||||
|
||||
@@ -149,6 +149,14 @@ namespace XCamera.Core
|
||||
[DllImport("XCloudSDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
|
||||
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
|
||||
|
||||
#region 实时预览
|
||||
|
||||
@@ -8,6 +8,11 @@ namespace XCamera.Models
|
||||
/// </summary>
|
||||
public class CameraConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// IP地址
|
||||
/// </summary>
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// TCP端口(固定34567)
|
||||
/// </summary>
|
||||
@@ -99,7 +104,11 @@ namespace XCamera.Models
|
||||
/// </summary>
|
||||
public string GetDeviceId()
|
||||
{
|
||||
return $"DEV_{TcpPort}_{Channel}";
|
||||
if (!string.IsNullOrEmpty(IpAddress))
|
||||
{
|
||||
return $"{IpAddress}:{TcpPort}";
|
||||
}
|
||||
return "192.168.100.10:34567";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -144,10 +144,11 @@ namespace XCamera
|
||||
/// <summary>
|
||||
/// 3. 根据配置文件内容启动摄像头
|
||||
/// </summary>
|
||||
/// <param name="previewHandle">预览窗口句柄(可为IntPtr.Zero)</param>
|
||||
/// <returns>是否成功启动</returns>
|
||||
public static bool StartCamera()
|
||||
public static bool StartCamera(IntPtr previewHandle = default(IntPtr))
|
||||
{
|
||||
return Manager.StartCamera();
|
||||
return Manager.StartCamera(previewHandle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace XCamera
|
||||
private readonly object _lockObject = new object();
|
||||
private int _captureInterval = 1000;
|
||||
private string _cameraIpAddress = "192.168.100.10"; // 摄像头IP地址
|
||||
private int _userHandle = -1; // 用户句柄
|
||||
|
||||
/// <summary>
|
||||
/// LED状态更新事件
|
||||
@@ -148,6 +149,9 @@ namespace XCamera
|
||||
throw new Exception($"注册回调函数失败,错误码: {userHandle}");
|
||||
}
|
||||
|
||||
// 保存用户句柄
|
||||
_userHandle = userHandle;
|
||||
|
||||
// 仅在提供了配置文件路径时加载配置
|
||||
if (!string.IsNullOrEmpty(configFilePath))
|
||||
{
|
||||
@@ -223,8 +227,9 @@ namespace XCamera
|
||||
/// <summary>
|
||||
/// 3. 根据配置文件内容启动摄像头
|
||||
/// </summary>
|
||||
/// <param name="previewHandle">预览窗口句柄(可为IntPtr.Zero)</param>
|
||||
/// <returns>是否成功启动</returns>
|
||||
public bool StartCamera()
|
||||
public bool StartCamera(IntPtr previewHandle = default(IntPtr))
|
||||
{
|
||||
if (!_isInitialized)
|
||||
{
|
||||
@@ -241,7 +246,7 @@ namespace XCamera
|
||||
try
|
||||
{
|
||||
// 初始化连接管理器
|
||||
_connectionManager.Initialize(CurrentConfig);
|
||||
_connectionManager.Initialize(CurrentConfig, _userHandle);
|
||||
|
||||
// 连接到摄像头
|
||||
if (!_connectionManager.Connect())
|
||||
@@ -249,8 +254,8 @@ namespace XCamera
|
||||
return false;
|
||||
}
|
||||
|
||||
// 开始实时预览(不显示窗口)
|
||||
if (!_connectionManager.StartRealPlay(IntPtr.Zero))
|
||||
// 开始实时预览(使用指定窗口句柄)
|
||||
if (!_connectionManager.StartRealPlay(previewHandle))
|
||||
{
|
||||
_connectionManager.Disconnect();
|
||||
return false;
|
||||
@@ -536,6 +541,12 @@ namespace XCamera
|
||||
lock (_lockObject)
|
||||
{
|
||||
_cameraIpAddress = ipAddress;
|
||||
|
||||
// 更新当前配置的IP地址
|
||||
if (CurrentConfig != null)
|
||||
{
|
||||
CurrentConfig.IpAddress = ipAddress;
|
||||
}
|
||||
}
|
||||
|
||||
// 使用信息事件而不是错误事件
|
||||
@@ -643,8 +654,44 @@ namespace XCamera
|
||||
{
|
||||
try
|
||||
{
|
||||
var ledStatuses = DetectLeds();
|
||||
OnLedStatusUpdated(ledStatuses);
|
||||
// 无论是否有LED区域,都捕获图像
|
||||
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;
|
||||
@@ -681,6 +728,8 @@ namespace XCamera
|
||||
if (nParam1 < 0)
|
||||
{
|
||||
OnErrorOccurred($"设备登录失败,错误码: {nParam1}");
|
||||
// 设备登录失败,断开连接
|
||||
_connectionManager?.Disconnect();
|
||||
}
|
||||
}
|
||||
else if (nMsgId == (int)XCloudSdkMessage.MediaStartRealPlay)
|
||||
@@ -688,6 +737,8 @@ namespace XCamera
|
||||
if (nParam1 < 0)
|
||||
{
|
||||
OnErrorOccurred($"开始实时预览失败,错误码: {nParam1}");
|
||||
// 实时预览失败,更新连接管理器状态
|
||||
_connectionManager?.StopRealPlay();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user