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 { /// /// 海康相机操作类 /// public class HikCamera { #region 实例 /// /// 相机实例 /// private IDevice device; #endregion #region 变量 /// /// 最后一次帧号记录 /// private long lastframeNum = -1; #endregion #region 构造函数 /// /// 相机构造函数 /// /// 相机实例 public HikCamera(IDevice device) { if (device == null) { throw new Exception("相机实例不能为空"); } this.device = device; } /// /// 相机构造函数 /// /// 相机信息 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 共有方法 /// /// 重新加载指定相机 /// /// 相机信息 /// true为加载成功,false为加载失败 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; } /// /// 开启采集 /// 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; } /// /// 关闭采集 /// 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; } /// /// 获取一次图片,需开启采集 /// /// 输出的帧数据 /// true为采集成功,false为采集失败 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; } /// /// 获取相机图像尺寸信息 /// /// 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 } }