NativeCamera.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Runtime.InteropServices;
  13. /// <summary> Session Native API. </summary>
  14. internal partial class NativeCamera : ICameraDataProvider
  15. {
  16. /// <summary> Handle of the native camera. </summary>
  17. private UInt64 m_NativeCameraHandle;
  18. private bool _IsErrorState = false;
  19. /// <summary> Creates a new bool. </summary>
  20. /// <returns> True if it succeeds, false if it fails. </returns>
  21. public bool Create()
  22. {
  23. _IsErrorState = false;
  24. var result = NativeApi.NRRGBCameraCreate(ref m_NativeCameraHandle);
  25. NativeErrorListener.Check(result, this, "Create", true);
  26. return result == NativeResult.Success;
  27. }
  28. /// <summary> Gets raw data. </summary>
  29. /// <param name="imageHandle"> Handle of the image.</param>
  30. /// <param name="eye"> The eye.</param>
  31. /// <param name="ptr"> [in,out] The pointer.</param>
  32. /// <param name="size"> [in,out] The size.</param>
  33. /// <returns> True if it succeeds, false if it fails. </returns>
  34. public bool GetRawData(UInt64 imageHandle, int eye, ref IntPtr ptr, ref int size)
  35. {
  36. if (_IsErrorState)
  37. {
  38. return false;
  39. }
  40. uint data_size = 0;
  41. var result = NativeApi.NRRGBCameraImageGetRawData(m_NativeCameraHandle, imageHandle, ref ptr, ref data_size);
  42. size = (int)data_size;
  43. NativeErrorListener.Check(result, this, "GetRawData");
  44. return result == NativeResult.Success;
  45. }
  46. /// <summary> Gets a resolution. </summary>
  47. /// <param name="imageHandle"> Handle of the image.</param>
  48. /// <param name="eye"> The eye.</param>
  49. /// <returns> The resolution. </returns>
  50. public NativeResolution GetResolution(UInt64 imageHandle, int eye)
  51. {
  52. NativeResolution resolution = new NativeResolution(0, 0);
  53. var result = NativeApi.NRRGBCameraImageGetResolution(m_NativeCameraHandle, imageHandle, ref resolution);
  54. NativeErrorListener.Check(result, this, "GetResolution");
  55. return resolution;
  56. }
  57. /// <summary> Gets hmd time nanos. </summary>
  58. /// <param name="imageHandle"> Handle of the image.</param>
  59. /// <param name="eye"> The eye.</param>
  60. /// <returns> The hmd time nanos. </returns>
  61. public UInt64 GetHMDTimeNanos(UInt64 imageHandle, int eye)
  62. {
  63. UInt64 time = 0;
  64. NativeApi.NRRGBCameraImageGetHMDTimeNanos(m_NativeCameraHandle, imageHandle, ref time);
  65. return time;
  66. }
  67. /// <summary> Get exposure time. </summary>
  68. /// <param name="imageHandle"> Handle of the image. </param>
  69. /// <param name="eye"> The eye. </param>
  70. /// <returns> Exposure time of the image. </returns>
  71. public UInt32 GetExposureTime(UInt64 imageHandle, int eye)
  72. {
  73. UInt32 exposureTime = 0;
  74. return exposureTime;
  75. }
  76. /// <summary> Get Gain. </summary>
  77. /// <param name="imageHandle"> Handle of the image. </param>
  78. /// <param name="eye"> The eye. </param>
  79. /// <returns> Gain of the image. </returns>
  80. public UInt32 GetGain(UInt64 imageHandle, int eye)
  81. {
  82. UInt32 gain = 0;
  83. return gain;
  84. }
  85. /// <summary> Callback, called when the set capture. </summary>
  86. /// <param name="callback"> The callback.</param>
  87. /// <param name="userdata"> (Optional) The userdata.</param>
  88. /// <returns> True if it succeeds, false if it fails. </returns>
  89. public bool SetCaptureCallback(CameraImageCallback callback, UInt64 userdata = 0)
  90. {
  91. if (_IsErrorState)
  92. {
  93. return false;
  94. }
  95. var result = NativeApi.NRRGBCameraSetCaptureCallback(m_NativeCameraHandle, callback, userdata);
  96. NativeErrorListener.Check(result, this, "SetCaptureCallback");
  97. return result == NativeResult.Success;
  98. }
  99. /// <summary> Sets image format. </summary>
  100. /// <param name="format"> Describes the format to use.</param>
  101. /// <returns> True if it succeeds, false if it fails. </returns>
  102. public bool SetImageFormat(CameraImageFormat format)
  103. {
  104. if (_IsErrorState)
  105. {
  106. return false;
  107. }
  108. var result = NativeApi.NRRGBCameraSetImageFormat(m_NativeCameraHandle, format);
  109. NativeErrorListener.Check(result, this, "SetImageFormat");
  110. return result == NativeResult.Success;
  111. }
  112. /// <summary> Starts a capture. </summary>
  113. /// <returns> True if it succeeds, false if it fails. </returns>
  114. public bool StartCapture()
  115. {
  116. if (_IsErrorState)
  117. {
  118. NativeErrorListener.Check(NativeResult.RGBCameraDeviceNotFind, this, "StartCapture", true);
  119. return false;
  120. }
  121. var result = NativeApi.NRRGBCameraStartCapture(m_NativeCameraHandle);
  122. _IsErrorState = (result != NativeResult.Success);
  123. NativeErrorListener.Check(result, this, "StartCapture", true);
  124. return result == NativeResult.Success;
  125. }
  126. /// <summary> Stops a capture. </summary>
  127. /// <returns> True if it succeeds, false if it fails. </returns>
  128. public bool StopCapture()
  129. {
  130. if (_IsErrorState)
  131. {
  132. return false;
  133. }
  134. var result = NativeApi.NRRGBCameraStopCapture(m_NativeCameraHandle);
  135. NativeErrorListener.Check(result, this, "StopCapture", true);
  136. return result == NativeResult.Success;
  137. }
  138. /// <summary> Destroys the image described by imageHandle. </summary>
  139. /// <param name="imageHandle"> Handle of the image.</param>
  140. /// <returns> True if it succeeds, false if it fails. </returns>
  141. public bool DestroyImage(UInt64 imageHandle)
  142. {
  143. if (_IsErrorState)
  144. {
  145. return false;
  146. }
  147. var result = NativeApi.NRRGBCameraImageDestroy(m_NativeCameraHandle, imageHandle);
  148. NativeErrorListener.Check(result, this, "DestroyImage");
  149. return result == NativeResult.Success;
  150. }
  151. /// <summary> Releases this object. </summary>
  152. /// <returns> True if it succeeds, false if it fails. </returns>
  153. public bool Release()
  154. {
  155. _IsErrorState = false;
  156. var result = NativeApi.NRRGBCameraDestroy(m_NativeCameraHandle);
  157. NativeErrorListener.Check(result, this, "Release");
  158. return result == NativeResult.Success;
  159. }
  160. /// <summary> A native api. </summary>
  161. private struct NativeApi
  162. {
  163. /// <summary> Nrrgb camera image get raw data. </summary>
  164. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  165. /// <param name="rgb_camera_image_handle"> Handle of the RGB camera image.</param>
  166. /// <param name="out_image_raw_data"> [in,out] Information describing the out image raw.</param>
  167. /// <param name="out_image_raw_data_size"> [in,out] Size of the out image raw data.</param>
  168. /// <returns> A NativeResult. </returns>
  169. [DllImport(NativeConstants.NRNativeLibrary)]
  170. public static extern NativeResult NRRGBCameraImageGetRawData(UInt64 rgb_camera_handle,
  171. UInt64 rgb_camera_image_handle, ref IntPtr out_image_raw_data, ref UInt32 out_image_raw_data_size);
  172. /// <summary> Nrrgb camera image get resolution. </summary>
  173. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  174. /// <param name="rgb_camera_image_handle"> Handle of the RGB camera image.</param>
  175. /// <param name="out_image_resolution"> [in,out] The out image resolution.</param>
  176. /// <returns> A NativeResult. </returns>
  177. [DllImport(NativeConstants.NRNativeLibrary)]
  178. public static extern NativeResult NRRGBCameraImageGetResolution(UInt64 rgb_camera_handle,
  179. UInt64 rgb_camera_image_handle, ref NativeResolution out_image_resolution);
  180. /// <summary> Nrrgb camera image get hmd time nanos. </summary>
  181. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  182. /// <param name="rgb_camera_image_handle"> Handle of the RGB camera image.</param>
  183. /// <param name="out_image_hmd_time_nanos"> [in,out] The out image hmd time nanos.</param>
  184. /// <returns> A NativeResult. </returns>
  185. [DllImport(NativeConstants.NRNativeLibrary)]
  186. public static extern NativeResult NRRGBCameraImageGetHMDTimeNanos(
  187. UInt64 rgb_camera_handle, UInt64 rgb_camera_image_handle,
  188. ref UInt64 out_image_hmd_time_nanos);
  189. /// <summary> Nrrgb camera create. </summary>
  190. /// <param name="out_rgb_camera_handle"> [in,out] Handle of the out RGB camera.</param>
  191. /// <returns> A NativeResult. </returns>
  192. [DllImport(NativeConstants.NRNativeLibrary)]
  193. public static extern NativeResult NRRGBCameraCreate(ref UInt64 out_rgb_camera_handle);
  194. /// <summary> Nrrgb camera destroy. </summary>
  195. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  196. /// <returns> A NativeResult. </returns>
  197. [DllImport(NativeConstants.NRNativeLibrary)]
  198. public static extern NativeResult NRRGBCameraDestroy(UInt64 rgb_camera_handle);
  199. /// <summary> Callback, called when the nrrgb camera set capture. </summary>
  200. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  201. /// <param name="image_callback"> The image callback.</param>
  202. /// <param name="userdata"> The userdata.</param>
  203. /// <returns> A NativeResult. </returns>
  204. [DllImport(NativeConstants.NRNativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  205. public static extern NativeResult NRRGBCameraSetCaptureCallback(
  206. UInt64 rgb_camera_handle, CameraImageCallback image_callback, UInt64 userdata);
  207. /// <summary> Nrrgb camera set image format. </summary>
  208. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  209. /// <param name="format"> Describes the format to use.</param>
  210. /// <returns> A NativeResult. </returns>
  211. [DllImport(NativeConstants.NRNativeLibrary)]
  212. public static extern NativeResult NRRGBCameraSetImageFormat(
  213. UInt64 rgb_camera_handle, CameraImageFormat format);
  214. /// <summary> Nrrgb camera start capture. </summary>
  215. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  216. /// <returns> A NativeResult. </returns>
  217. [DllImport(NativeConstants.NRNativeLibrary)]
  218. public static extern NativeResult NRRGBCameraStartCapture(UInt64 rgb_camera_handle);
  219. /// <summary> Nrrgb camera stop capture. </summary>
  220. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  221. /// <returns> A NativeResult. </returns>
  222. [DllImport(NativeConstants.NRNativeLibrary)]
  223. public static extern NativeResult NRRGBCameraStopCapture(UInt64 rgb_camera_handle);
  224. /// <summary> Nrrgb camera image destroy. </summary>
  225. /// <param name="rgb_camera_handle"> Handle of the RGB camera.</param>
  226. /// <param name="rgb_camera_image_handle"> Handle of the RGB camera image.</param>
  227. /// <returns> A NativeResult. </returns>
  228. [DllImport(NativeConstants.NRNativeLibrary)]
  229. public static extern NativeResult NRRGBCameraImageDestroy(UInt64 rgb_camera_handle,
  230. UInt64 rgb_camera_image_handle);
  231. };
  232. }
  233. }