using MvCameraControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvvmScaffoldFrame48.DLL.CameraTools
{
///
/// 海康相机操作类
///
public class HikCamera
{
///
/// 相机实例
///
private IDevice device;
///
/// 相机构造函数
///
/// 相机实例
///
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);
}
}
}
}
}
}