1
0

2 کامیت‌ها 2986191b40 ... 527c9d7c47

نویسنده SHA1 پیام تاریخ
  向羽 孟 527c9d7c47 20250916003 完善海康相机工具类及MODEL 2 هفته پیش
  向羽 孟 2f4ed333b8 20250916002 添加Config操作工具类XMLReadWrite 2 هفته پیش

+ 174 - 2
MvvmScaffoldFrame48.DLL/CameraTools/HikCamera.cs

@@ -1,4 +1,5 @@
 using MvCameraControl;
+using MvvmScaffoldFrame48.Model.HikVisionCamera;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -12,16 +13,25 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
     /// </summary>
     public class HikCamera
     {
+        #region 实例
         /// <summary>
         /// 相机实例
         /// </summary>
         private IDevice device;
+        #endregion
 
+        #region 变量
+        /// <summary>
+        /// 最后一次帧号记录
+        /// </summary>
+        private long lastframeNum = -1;
+        #endregion
+
+        #region 构造函数
         /// <summary>
         /// 相机构造函数
         /// </summary>
         /// <param name="device">相机实例</param>
-        /// <exception cref="Exception"></exception>
         public HikCamera(IDevice device)
         {
             if (device == null)
@@ -35,7 +45,6 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
         /// 相机构造函数
         /// </summary>
         /// <param name="deviceInfo">相机信息</param>
-        /// <exception cref="Exception"></exception>
         public HikCamera(IDeviceInfo deviceInfo)
         {
             if (device != null && device.IsConnected)
@@ -83,5 +92,168 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
                 }
             }
         }
+        #endregion
+
+        #region 共有方法
+        /// <summary>
+        /// 重新加载指定相机
+        /// </summary>
+        /// <param name="deviceInfo">相机信息</param>
+        /// <returns>true为加载成功,false为加载失败</returns>
+        public bool ReLoadCameraDevice(IDeviceInfo deviceInfo)
+        {
+            bool Blresult = false;
+            if (device != null && device.IsConnected)
+            {
+                device.Close();
+                device.Dispose();
+            }
+            try
+            {
+                // 打开设备
+                device = DeviceFactory.CreateDevice(deviceInfo);
+                Blresult = true;
+            }
+            catch
+            {
+                return Blresult;
+            }
+
+            int result = device.Open();
+            if (result != MvError.MV_OK)
+            {
+                Blresult = false;
+                return Blresult;
+            }
+
+            // 判断是否为gige设备
+            if (device is IGigEDevice)
+            {
+                // 转换为gigE设备
+                IGigEDevice gigEDevice = device as IGigEDevice;
+
+                // 探测网络最佳包大小(只对GigE相机有效)
+                result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
+                if (result != MvError.MV_OK)
+                {
+                    //Log("Warning: Get Packet Size failed!", result);
+                }
+                else
+                {
+                    result = device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
+                    if (result != MvError.MV_OK)
+                    {
+                        //Log("Warning: Set Packet Size failed!", result);
+                    }
+                }
+            }
+            return Blresult;
+        }
+
+        /// <summary>
+        /// 开启采集
+        /// </summary>
+        public bool StartReceiveFuntion()
+        {
+            bool result = false;
+            try
+            {
+                if (device == null) return result;
+                device.StreamGrabber.SetImageNodeNum(30);
+                int ret = device.StreamGrabber.StartGrabbing();
+                if (ret != MvError.MV_OK)
+                {
+                    result = true;
+                }
+            }
+            catch
+            {
+                result = false;
+            }
+            return result;
+        }
+
+        /// <summary>
+        /// 关闭采集
+        /// </summary>
+        public bool StopReceiveFuntion()
+        {
+            bool result = false;
+            try
+            {
+                if (device == null) return result;
+                int ret = device.StreamGrabber.StopGrabbing();
+                if (ret != MvError.MV_OK)
+                {
+                    result = true;
+                }
+            }
+            catch
+            {
+                result = false;
+            }
+            return result;
+        }
+
+        /// <summary>
+        /// 获取一次图片,需开启采集
+        /// </summary>
+        /// <param name="IFrameData">输出的帧数据</param>
+        /// <returns>true为采集成功,false为采集失败</returns>
+        public bool GetOnceImage(out IFrameOut IFrameData)
+        {
+            bool result = false;
+            int BRet = device.StreamGrabber.GetImageBuffer(1000, out IFrameData);
+            if (BRet == 0 && IFrameData != null) result = true;
+            else return result;
+            if (lastframeNum == -1)
+            {
+                lastframeNum = IFrameData.FrameNum;
+            }
+            else if (lastframeNum == IFrameData.FrameNum - 1)
+            {
+                lastframeNum = IFrameData.FrameNum;
+            }
+            else
+            {
+                //丢帧记录,用于连续采集时的丢帧检测
+                Console.WriteLine("lost frame: Width[{0}] , Height[{1}] , FrameNum[{2}] ,Frevous[{3}]",
+                    IFrameData.Image.Width, IFrameData.Image.Height, IFrameData.FrameNum, lastframeNum);
+                lastframeNum = IFrameData.FrameNum;
+            }
+            return result;
+        }
+
+        /// <summary>
+        /// 获取相机图像尺寸信息
+        /// </summary>
+        /// <returns></returns>
+        public CameraImageSizeCModel GetCamereImageSize()
+        {
+            CameraImageSizeCModel cameraImageSize = null;
+            if (device != null)
+            {
+                try
+                {
+                    device.Parameters.GetIntValue("Width", out IIntValue PixWidth);
+                    device.Parameters.GetIntValue("Height", out IIntValue PixHeight);
+                    cameraImageSize = new CameraImageSizeCModel()
+                    {
+                        Height = (int)PixHeight.CurValue,
+                        Width = (int)PixWidth.CurValue
+                    };
+                }
+                catch
+                {
+                    return null;
+                }
+            }
+            return cameraImageSize;
+        }
+        #endregion
+
+        #region 私有方法
+
+        #endregion
     }
 }

+ 3 - 3
MvvmScaffoldFrame48.DLL/CameraTools/HikVision.cs

@@ -32,10 +32,10 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
         /// </summary>
         /// <param name="CamList"></param>
         /// <returns></returns>
-        public static void GetCameraList(out List<CameraInfoClassModel> CameraInfoList)
+        public static void GetCameraList(out List<CameraInfoModel> CameraInfoList)
         {
             CamList = new List<IDeviceInfo>();
-            CameraInfoList = new List<CameraInfoClassModel>();
+            CameraInfoList = new List<CameraInfoModel>();
             int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out CamList);
             if (nRet != MvError.MV_OK)
             {
@@ -46,7 +46,7 @@ namespace MvvmScaffoldFrame48.DLL.CameraTools
                 //输出相机名称及SN码以方便选择设备
                 foreach (var item in CamList)
                 {
-                    CameraInfoList.Add(new CameraInfoClassModel
+                    CameraInfoList.Add(new CameraInfoModel
                     {
                         DeviceName = item.ModelName,
                         DeviceSN = item.SerialNumber,

+ 59 - 0
MvvmScaffoldFrame48.DLL/ConfigTools/XMLReadWrite.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Xml.Serialization;
+
+namespace MvvmScaffoldFrame48.DLL.ConfigTools
+{
+    public static class XMLReadWrite
+    {
+        /// <summary>
+        /// 序列化对象到XML文件
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="data"></param>
+        /// <param name="filePath"></param>
+        /// <exception cref="InvalidOperationException"></exception>
+        public static void SerializeToXml<T>(T data, string filePath)
+        {
+            try
+            {
+                var serializer = new XmlSerializer(typeof(T));
+                using (var writer = new StreamWriter(filePath))
+                {
+                    serializer.Serialize(writer, data);
+                }
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidOperationException("XML serialization failed", ex);
+            }
+        }
+
+        /// <summary>
+        /// 从XML文件反序列化对象
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="filePath"></param>
+        /// <returns></returns>
+        /// <exception cref="InvalidOperationException"></exception>
+        public static T DeserializeFromXml<T>(string filePath)
+        {
+            try
+            {
+                var serializer = new XmlSerializer(typeof(T));
+                using (var reader = new StreamReader(filePath))
+                {
+                    return (T)serializer.Deserialize(reader);
+                }
+            }
+            catch (Exception ex)
+            {
+                throw new InvalidOperationException("XML deserialization failed", ex);
+            }
+        }
+    }
+}

+ 1 - 0
MvvmScaffoldFrame48.DLL/MvvmScaffoldFrame48.Dll.csproj

@@ -52,6 +52,7 @@
     <Compile Include="CameraTools\HikCamera.cs" />
     <Compile Include="CameraTools\HikVision.cs" />
     <Compile Include="CommunicationTools\ModbusTcpClient.cs" />
+    <Compile Include="ConfigTools\XMLReadWrite.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UserManager.cs" />
   </ItemGroup>

+ 14 - 0
MvvmScaffoldFrame48.MODEL/HikVisionCamera/CameraImageSizeCModel.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace MvvmScaffoldFrame48.Model.HikVisionCamera
+{
+    public class CameraImageSizeCModel
+    {
+        public int Width { get; set; }
+        public int Height { get; set; }
+    }
+}

+ 1 - 1
MvvmScaffoldFrame48.MODEL/HikVisionCamera/CameraInfoClassModel.cs → MvvmScaffoldFrame48.MODEL/HikVisionCamera/CameraInfoModel.cs

@@ -9,7 +9,7 @@ namespace MvvmScaffoldFrame48.Model.HikVisionCamera
     /// <summary>
     /// 相机初始化信息
     /// </summary>
-    public class CameraInfoClassModel
+    public class CameraInfoModel
     {
         public string DeviceName { get; set; }
 

+ 2 - 1
MvvmScaffoldFrame48.MODEL/MvvmScaffoldFrame48.Model.csproj

@@ -41,7 +41,8 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="HikVisionCamera\CameraInfoClassModel.cs" />
+    <Compile Include="HikVisionCamera\CameraImageSizeCModel.cs" />
+    <Compile Include="HikVisionCamera\CameraInfoModel.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UserModel.cs" />
   </ItemGroup>