革博士程序V1仓库
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

189 linhas
5.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using IKapC.NET;
  7. using IKapBoardClassLibrary;
  8. namespace LeatherApp.Device.CamerUtil
  9. {
  10. public class IKDevice
  11. {
  12. // 相机句柄
  13. public IntPtr m_pDev = new IntPtr(-1);
  14. // 采集卡句柄
  15. public IntPtr m_pBoard = new IntPtr(-1);
  16. // 用户缓冲区,用于图像数据转换
  17. public IntPtr m_pUserBuffer = new IntPtr(-1);
  18. // 是否正在采集
  19. public volatile bool m_bGrabingImage = false;
  20. // 是否已更新用户缓冲区
  21. public volatile bool m_bUpdateImage = false;
  22. // 相机类型,0为GV相机,1为CL相机,2为CXP相机
  23. public int m_nType = -1;
  24. // 图像宽度
  25. public int m_nWidth = -1;
  26. // 图像高度
  27. public int m_nHeight = -1;
  28. // 像素位数
  29. public int m_nDepth = 8;
  30. // 图像通道数
  31. public int m_nChannels = 1;
  32. // 相机索引
  33. public int m_nDevIndex = -1;
  34. // 采集卡索引
  35. public int m_nBoardIndex = -1;
  36. // 相机缓冲区个数
  37. public int m_nFrameCount = 2;
  38. // 当前帧索引
  39. public int m_nCurFrameIndex = 0;
  40. // 相机缓冲区大小
  41. public int m_nBufferSize = 0;
  42. // 用户缓冲区锁
  43. public object m_mutexImage = new object();
  44. /*
  45. *@brief:根据索引开启设备
  46. *@param [in] nDevIndex:相机索引
  47. *@param [in] nBoardIndex:采集卡索引
  48. *@return:是否开启成功
  49. */
  50. public virtual bool openDevice(int nDevIndex, int nBoardIndex)
  51. {
  52. return false;
  53. }
  54. /*
  55. *@brief:关闭相机
  56. *@param [in]:
  57. *@return:是否关闭成功
  58. */
  59. public virtual bool closeDevice()
  60. {
  61. return false;
  62. }
  63. /*
  64. *@brief:查询设备连接状态
  65. *@param [in]:
  66. *@return:是否连接
  67. */
  68. public virtual bool isOpen()
  69. {
  70. return false;
  71. }
  72. /*
  73. *@brief:加载采集卡配置文件
  74. *@param [in] sFilePath:配置文件路径
  75. *@return:是否加载成功
  76. */
  77. public virtual bool loadConfiguration(string sFilePath)
  78. {
  79. return false;
  80. }
  81. /*
  82. * @brief:申请缓冲区资源
  83. * @return:是否申请成功
  84. */
  85. public virtual bool createBuffer()
  86. {
  87. return false;
  88. }
  89. /*
  90. * @brief:清除已申请缓冲区资源
  91. * @return:
  92. */
  93. public virtual void clearBuffer()
  94. {
  95. }
  96. /*
  97. *@brief:开始采集
  98. *@param [in] nCount:采集帧数
  99. *@return:是否开始采集
  100. */
  101. public virtual bool startGrab(int nCount)
  102. {
  103. return false;
  104. }
  105. /*
  106. *@brief:停止采集
  107. *@param [in]:
  108. *@return:是否停止采集
  109. */
  110. public virtual bool stopGrab()
  111. {
  112. return false;
  113. }
  114. /*
  115. * @brief:设置相机特征值
  116. * @param [in] featureName:特征名
  117. * @param [in] featureValue:特征值
  118. * @return: 是否设置成功
  119. */
  120. public bool setFeatureValue(string featureName, string featureValue)
  121. {
  122. IntPtr itkFeature = new IntPtr(-1);
  123. uint nType = 0;
  124. uint res = IKapCLib.ItkDevAllocFeature(m_pDev, featureName, ref itkFeature);
  125. if (!Check(res))
  126. {
  127. System.Diagnostics.Debug.WriteLine("Camera error:Allocate feature failed");
  128. return false;
  129. }
  130. res = IKapCLib.ItkFeatureGetType(itkFeature, ref nType);
  131. if (!Check(res))
  132. {
  133. System.Diagnostics.Debug.WriteLine("Camera error:Get feature type failed");
  134. return false;
  135. }
  136. switch (nType)
  137. {
  138. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_INT32:
  139. res = IKapCLib.ItkFeatureSetInt32(itkFeature, Convert.ToInt32(featureValue));
  140. break;
  141. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_INT64:
  142. res = IKapCLib.ItkFeatureSetInt64(itkFeature, Convert.ToInt64(featureValue));
  143. break;
  144. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_FLOAT:
  145. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_DOUBLE:
  146. res = IKapCLib.ItkFeatureSetDouble(itkFeature, Convert.ToDouble(featureValue));
  147. break;
  148. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_ENUM:
  149. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_STRING:
  150. res = IKapCLib.ItkFeatureFromString(itkFeature, featureValue);
  151. break;
  152. case (uint)ItkFeatureType.ITKFEATURE_VAL_TYPE_COMMAND:
  153. res = IKapCLib.ItkFeatureExecuteCommand(itkFeature);
  154. break;
  155. }
  156. if (!Check(res))
  157. {
  158. System.Diagnostics.Debug.WriteLine("Camera error:Set feature failed");
  159. return false;
  160. }
  161. return true;
  162. }
  163. /*
  164. *@brief:检查错误码
  165. *@param [in] err:错误码
  166. *@return:是否错误
  167. */
  168. public static bool Check(uint err)
  169. {
  170. if (err != (uint)ItkStatusErrorId.ITKSTATUS_OK)
  171. {
  172. System.Diagnostics.Debug.WriteLine("Error code: {0}.\n", err.ToString("x8"));
  173. return false;
  174. }
  175. return true;
  176. }
  177. }
  178. }