NativeInterface_Camera.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. using Rokid.UXR.Module;
  5. using UnityEngine;
  6. using Rokid.UXR.Utility;
  7. namespace Rokid.UXR.Native
  8. {
  9. public partial class NativeInterface
  10. {
  11. /// <summary>
  12. /// The update of camera preview data.
  13. /// width, height, result, timestamp
  14. /// </summary>
  15. [Obsolete("Use NativeInterface.NativeAPI.OnCameraDataUpdate instead")]
  16. public static event Action<int, int, byte[], long> OnCameraDataUpdate;
  17. public static partial class NativeAPI
  18. {
  19. /// <summary>
  20. /// The update of camera preview data.
  21. /// width, height, result, timestamp
  22. /// </summary>
  23. public static event Action<int, int, byte[], long> OnCameraDataUpdate;
  24. /// <summary>
  25. /// Start camera preview
  26. /// </summary>
  27. public static void StartCameraPreview()
  28. {
  29. if (Utils.IsAndroidPlatfrom())
  30. startCameraPreview();
  31. }
  32. /// <summary>
  33. /// Sets the type of camera preview data type
  34. /// </summary>
  35. /// <param name="dataType">1-ARGB,2-NV21</param>
  36. public static void SetCameraPreviewDataType(int dataType)
  37. {
  38. if (Utils.IsAndroidPlatfrom())
  39. setOnCameraDataUpdate(OnCameraDataUpdateCallByC, dataType);
  40. }
  41. /// <summary>
  42. /// Stop camera preview
  43. /// </summary>
  44. public static void StopCameraPreview()
  45. {
  46. if (Utils.IsAndroidPlatfrom())
  47. stopCameraPreview();
  48. }
  49. /// <summary>
  50. /// Clean up camera data updates
  51. /// </summary>
  52. public static void ClearCameraDataUpdate()
  53. {
  54. if (Utils.IsAndroidPlatfrom())
  55. clearOnCameraDataUpdate();
  56. }
  57. /// <summary>
  58. /// Whether the glasses support the camera
  59. /// </summary>
  60. /// <returns></returns>
  61. public static bool IsSupportCamera()
  62. {
  63. return FuncDeviceCheck.CheckCameraFunc();
  64. }
  65. /// <summary>
  66. /// Preview or not
  67. /// </summary>
  68. /// <returns></returns>
  69. public static bool IsPreviewing()
  70. {
  71. if (Application.platform == RuntimePlatform.Android)
  72. return Api.isPreviewing();
  73. return false;
  74. }
  75. /// <summary>
  76. /// Get preview width
  77. /// </summary>
  78. /// <returns></returns>
  79. public static int GetPreviewWidth()
  80. {
  81. if (Utils.IsAndroidPlatfrom())
  82. {
  83. int[] dimen = new int[2];
  84. Api.getPreviewDimen(dimen);
  85. return dimen[0];
  86. }
  87. return 0;
  88. }
  89. /// <summary>
  90. /// Get preview height
  91. /// </summary>
  92. /// <returns></returns>
  93. public static int GetPreivewHeight()
  94. {
  95. if (Utils.IsAndroidPlatfrom())
  96. {
  97. int[] dimen = new int[2];
  98. Api.getPreviewDimen(dimen);
  99. return dimen[1];
  100. }
  101. return 0;
  102. }
  103. /// <summary>
  104. /// Get fx,fy
  105. /// </summary>
  106. /// <param name="data"></param>
  107. public static void GetFocalLength(float[] data)
  108. {
  109. if (Utils.IsAndroidPlatfrom())
  110. getFocalLength(data);
  111. }
  112. /// <summary>
  113. /// Get cx,cy
  114. /// </summary>
  115. /// <param name="data"></param>
  116. public static void GetPrincipalPoint(float[] data)
  117. {
  118. if (Utils.IsAndroidPlatfrom())
  119. getPrincipalPoint(data);
  120. }
  121. /// <summary>
  122. /// Get width,height
  123. /// </summary>
  124. /// <param name="data"></param>
  125. public static void GetImageDimensions(int[] data)
  126. {
  127. if (Utils.IsAndroidPlatfrom())
  128. getImageDimensions(data);
  129. }
  130. /// <summary>
  131. /// pinhole:k1,k2,k3,p1,p2
  132. /// fisheye:alpha,k1,k2,k3,k4;
  133. /// </summary>
  134. /// <param name="data"></param>
  135. public static void GetDistortion(float[] data)
  136. {
  137. if (Utils.IsAndroidPlatfrom())
  138. getDistortion(data);
  139. }
  140. /// <summary>
  141. /// Get the YPR angles of the camera,
  142. /// represented as an array of length 3 in the order [yaw, pitch, roll].
  143. /// </summary>
  144. /// <param name="data"></param>
  145. public static void GetCameraYPR(float[] data)
  146. {
  147. if (Utils.IsAndroidPlatfrom())
  148. getCameraYPR(data);
  149. }
  150. #region NativeInterface
  151. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  152. static extern void startCameraPreview();
  153. delegate void OnCameraDataUpdateC(IntPtr ptr, int size, ushort width, ushort height, long timestamp);
  154. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  155. static extern void setOnCameraDataUpdate(OnCameraDataUpdateC cb, int type);
  156. [MonoPInvokeCallback(typeof(OnCameraDataUpdateC))]
  157. static void OnCameraDataUpdateCallByC(IntPtr ptr, int size, ushort width, ushort height, long timestamp)
  158. {
  159. byte[] result = new byte[size];
  160. Marshal.Copy(ptr, result, 0, size);
  161. NativeAPI.OnCameraDataUpdate?.Invoke(width, height, result, timestamp);
  162. OnCameraDataUpdate?.Invoke(width, height, result, timestamp);
  163. }
  164. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  165. static extern void stopCameraPreview();
  166. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  167. static extern void clearOnCameraDataUpdate();
  168. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  169. static extern void getFocalLength(float[] data);
  170. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  171. static extern void getPrincipalPoint(float[] data);
  172. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  173. static extern void getImageDimensions(int[] data);
  174. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  175. static extern void getDistortion(float[] data);
  176. [DllImport(ApiConstants.UXR_GFX_PLUGIN)]
  177. static extern void getCameraYPR(float[] data);
  178. #endregion
  179. }
  180. }
  181. }