|
@@ -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
|
|
|
}
|
|
|
}
|