HikCamera.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using MvCameraControl;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MvvmScaffoldFrame48.DLL.CameraTools
  8. {
  9. /// <summary>
  10. /// 海康相机操作类
  11. /// </summary>
  12. public class HikCamera
  13. {
  14. /// <summary>
  15. /// 相机实例
  16. /// </summary>
  17. private IDevice device;
  18. /// <summary>
  19. /// 相机构造函数
  20. /// </summary>
  21. /// <param name="device">相机实例</param>
  22. /// <exception cref="Exception"></exception>
  23. public HikCamera(IDevice device)
  24. {
  25. if (device == null)
  26. {
  27. throw new Exception("相机实例不能为空");
  28. }
  29. this.device = device;
  30. }
  31. /// <summary>
  32. /// 相机构造函数
  33. /// </summary>
  34. /// <param name="deviceInfo">相机信息</param>
  35. /// <exception cref="Exception"></exception>
  36. public HikCamera(IDeviceInfo deviceInfo)
  37. {
  38. if (device != null && device.IsConnected)
  39. {
  40. device.Close();
  41. device.Dispose();
  42. }
  43. try
  44. {
  45. // 打开设备
  46. device = DeviceFactory.CreateDevice(deviceInfo);
  47. }
  48. catch (Exception ex)
  49. {
  50. throw new Exception(ex.Message);
  51. }
  52. int result = device.Open();
  53. if (result != MvError.MV_OK)
  54. {
  55. throw new Exception("打开相机失败");
  56. }
  57. // 判断是否为gige设备
  58. if (device is IGigEDevice)
  59. {
  60. // 转换为gigE设备
  61. IGigEDevice gigEDevice = device as IGigEDevice;
  62. // 探测网络最佳包大小(只对GigE相机有效)
  63. result = gigEDevice.GetOptimalPacketSize(out int optionPacketSize);
  64. if (result != MvError.MV_OK)
  65. {
  66. //Log("Warning: Get Packet Size failed!", result);
  67. }
  68. else
  69. {
  70. result = device.Parameters.SetIntValue("GevSCPSPacketSize", (long)optionPacketSize);
  71. if (result != MvError.MV_OK)
  72. {
  73. //Log("Warning: Set Packet Size failed!", result);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }