GestureManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using RayNeo.Native;
  2. using System;
  3. using UnityEngine;
  4. namespace RayNeo
  5. {
  6. public enum GestureType
  7. {
  8. Nothing,
  9. Five,
  10. Pointer
  11. }
  12. public class GestureManager
  13. {
  14. private static GestureManager ins = new GestureManager();
  15. public static GestureManager Ins
  16. {
  17. get
  18. {
  19. return ins;
  20. }
  21. }
  22. private AndroidJavaObject m_javaGestureCtrl;
  23. private AndroidJavaObject m_javancnnMPCtrl;
  24. public Action<GestureType> GestureTypeCallback;
  25. public Action<bool> HandStaticStateChange;
  26. public Action<bool, float, float[], float[]> OnPalmInfoChanged;
  27. private bool m_isHandStatic = false;//手是否静止了.
  28. public void Start(int w = 640, int h = 480, int f = 20)
  29. {
  30. #if UNITY_EDITOR
  31. return;
  32. #endif
  33. Debug.Log("[GestureManager.Init.MercuryX2]开启算法");
  34. if (m_javaGestureCtrl == null)
  35. {
  36. m_javaGestureCtrl = new AndroidJavaObject("com.tcl.unity.unityadapter.algorithm.gesture.GestureController").CallStatic<AndroidJavaObject>("getInstance");
  37. m_javancnnMPCtrl = m_javaGestureCtrl.Call<AndroidJavaObject>("getMediapipehand");
  38. InitPalmListener();
  39. InitMotionListener();
  40. }
  41. AndroidJavaObject uActivity = GetUnityActivity();
  42. m_javaGestureCtrl.Call("startAlgorithm", uActivity, GetCacheDir(), uActivity.Call<AndroidJavaObject>("getAssets"), w, h, f);
  43. GlobalMgrUtil.Instance.StartPoll(GetCurrentGesture, 0.3f);
  44. //GestureRecCtrl.InitAlgorithm();
  45. //StartCheckGestureType();
  46. }
  47. private void InitPalmListener()
  48. {
  49. HandsInfoListener listener = new HandsInfoListener(OnPalmInfo);
  50. m_javancnnMPCtrl.Call("setHandsInfoListener", listener);
  51. }
  52. /// <summary>
  53. /// 调用手势动静接口
  54. /// </summary>
  55. private void InitMotionListener()
  56. {
  57. SetMotionListener Listener = new SetMotionListener(OnMotionStaticStateChange);
  58. m_javancnnMPCtrl.Call("setMotionListener", Listener);
  59. }
  60. public void Stop()
  61. {
  62. if (m_javaGestureCtrl == null)
  63. {
  64. return;
  65. }
  66. GlobalMgrUtil.Instance.StopPoll();
  67. m_javaGestureCtrl.Call("stopAlgorithm");
  68. Debug.Log("[GestureManager.Stop.MercuryX2]:算法模块关闭");
  69. }
  70. public void GetCurrentGesture()
  71. {
  72. GestureType CurrentGestureType = GestureType.Nothing;
  73. string Getsture = m_javancnnMPCtrl.Call<string>("getGesture");
  74. if (string.IsNullOrEmpty(Getsture))
  75. {
  76. GestureTypeCallback?.Invoke(GestureType.Nothing);
  77. return;
  78. }
  79. if (Getsture.Equals("Five"))
  80. {
  81. CurrentGestureType = GestureType.Five;
  82. }
  83. else if (Getsture.Equals("Pointer"))
  84. {
  85. CurrentGestureType = GestureType.Pointer;
  86. }
  87. else
  88. {
  89. CurrentGestureType = GestureType.Nothing;
  90. }
  91. GestureTypeCallback?.Invoke(CurrentGestureType);
  92. }
  93. public bool IsCaptureGesture()
  94. {
  95. if (m_javancnnMPCtrl == null)
  96. {
  97. return false;
  98. }
  99. bool isGetGesture = m_javancnnMPCtrl.Call<bool>("checkState");
  100. return isGetGesture;
  101. }
  102. /// <summary>
  103. /// 获取手势骨节点坐标数组
  104. /// </summary>
  105. /// <returns></returns>
  106. public float[] GetSkeletonCoordinates()
  107. {
  108. float[] SkeletonCoordinates = m_javancnnMPCtrl.Call<float[]>("getSkeletonCoordinate");
  109. if (SkeletonCoordinates != null)
  110. {
  111. return SkeletonCoordinates;
  112. }
  113. return null;
  114. }
  115. private void OnPalmInfo(bool isRecognized, float handSize, float[] position, float[] normal)
  116. {
  117. OnPalmInfoChanged?.Invoke(isRecognized, handSize, position, normal);
  118. }
  119. private void OnMotionStaticStateChange(bool isStatic)
  120. {
  121. m_isHandStatic = isStatic;
  122. HandStaticStateChange?.Invoke(m_isHandStatic);
  123. }
  124. ///// <summary>
  125. ///// 设置手势动静识别时间判定阈值
  126. ///// </summary>
  127. ///// <param name="TimeScale"></param>
  128. //public void SetTimeThreshold(float TimeScale)
  129. //{
  130. // m_javancnnMPCtrl.Call("setTimeThreshold", TimeScale);
  131. //}
  132. /// <summary>
  133. /// 设置手势动静识别像素变化幅度阈值
  134. /// </summary>
  135. /// <param name="TimeScale"></param>
  136. public void SetPixelThreshold(float TimeScale)
  137. {
  138. m_javancnnMPCtrl.Call("setPixelThreshold", TimeScale);
  139. }
  140. public AndroidJavaObject GetJavaMP()
  141. {
  142. return m_javancnnMPCtrl;
  143. }
  144. #region 动静识别接口
  145. public sealed class SetMotionListener : AndroidJavaProxy
  146. {
  147. private Action<bool> m_Interface;
  148. public SetMotionListener(Action<bool> Interface) : base("com.ncnn.mediapipehand.NcnnMediapipeHand$handsMotionListener")
  149. {
  150. Debug.Log("[SetMotionListener.MercuryX2]:SetMotionListener");
  151. m_Interface = Interface;
  152. }
  153. public void onMotionChanged(bool IsStill)
  154. {
  155. MainThreadQueue.Instance.ExecuteQueue.Enqueue(() =>
  156. {
  157. m_Interface.Invoke(IsStill);
  158. });
  159. }
  160. }
  161. #endregion
  162. #region 手掌识别接口
  163. public sealed class HandsInfoListener : AndroidJavaProxy
  164. {
  165. private Action<bool, float, float[], float[]> m_onHandsInfoCallBack;
  166. public HandsInfoListener(Action<bool, float, float[], float[]> onHandsInfoCallBack)
  167. : base("com.ncnn.mediapipehand.NcnnMediapipeHand$handsInfoListener")
  168. {
  169. m_onHandsInfoCallBack = onHandsInfoCallBack;
  170. }
  171. public void onHandsInfo(bool isRecognized, float handSize, float[] position, float[] normal)
  172. {
  173. MainThreadQueue.Instance.ExecuteQueue.Enqueue(() =>
  174. {
  175. m_onHandsInfoCallBack?.Invoke(isRecognized, handSize, position, normal);
  176. });
  177. }
  178. }
  179. #endregion
  180. private AndroidJavaObject GetUnityActivity()
  181. {
  182. AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  183. AndroidJavaObject UnityActivity = UnityClass.GetStatic<AndroidJavaObject>("currentActivity");
  184. return UnityActivity;
  185. }
  186. public string GetCacheDir()
  187. {
  188. using (var CacheDir = GetUnityActivity().Call<AndroidJavaObject>("getCacheDir"))
  189. {
  190. if (CacheDir == null)
  191. {
  192. return string.Empty;
  193. }
  194. string path = CacheDir.Call<string>("getAbsolutePath");
  195. Debug.Log("[GestureManager.GestureManagerMercuryX2]:cache path|" + path);
  196. return path;
  197. }
  198. }
  199. }
  200. }