123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using MvCameraControl;
- using MvvmScaffoldFrame48.Model.HikVisionCamera;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvvmScaffoldFrame48.DLL.CameraTools
- {
- /// <summary>
- /// 海康相机操作类
- /// </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>
- public HikCamera(IDevice device)
- {
- if (device == null)
- {
- throw new Exception("相机实例不能为空");
- }
- this.device = device;
- }
- /// <summary>
- /// 相机构造函数
- /// </summary>
- /// <param name="deviceInfo">相机信息</param>
- public HikCamera(IDeviceInfo deviceInfo)
- {
- if (device != null && device.IsConnected)
- {
- device.Close();
- device.Dispose();
- }
- try
- {
- // 打开设备
- device = DeviceFactory.CreateDevice(deviceInfo);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- int result = device.Open();
- if (result != MvError.MV_OK)
- {
- throw new Exception("打开相机失败");
- }
- // 判断是否为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);
- }
- }
- }
- }
- #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
- }
- }
|