123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using MvCameraControl;
- 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
- {
- /// <summary>
- /// 相机实例
- /// </summary>
- private IDevice device;
- /// <summary>
- /// 相机构造函数
- /// </summary>
- /// <param name="device">相机实例</param>
- /// <exception cref="Exception"></exception>
- public HikCamera(IDevice device)
- {
- if (device == null)
- {
- throw new Exception("相机实例不能为空");
- }
- this.device = device;
- }
- /// <summary>
- /// 相机构造函数
- /// </summary>
- /// <param name="deviceInfo">相机信息</param>
- /// <exception cref="Exception"></exception>
- 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);
- }
- }
- }
- }
- }
- }
|