InfraredCameraHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using SLAMUVC;
  6. using UnityEngine.UI;
  7. using SmartBowSDK;
  8. using o0InfraredLocate.ZIM;
  9. namespace InfraredManager
  10. {
  11. public class InfraredCameraHelper
  12. {
  13. #region 单例
  14. private static InfraredCameraHelper _Instance;
  15. public static InfraredCameraHelper GetInstance()
  16. {
  17. if (_Instance == null) _Instance = new();
  18. return _Instance;
  19. }
  20. #endregion
  21. #region 创建销毁
  22. private GameObject _managerObject;
  23. private ScreenLocate _screenLocate;
  24. private UVCManager _uvcManager;
  25. public static List<RawImage> InfraredCameraHelperRawImageList = new();
  26. public void Create(int defWidth = 320, int defHeight = 240)
  27. {
  28. if (_managerObject) return;
  29. string prefabName = "WebCameraView";
  30. var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
  31. UnityEngine.Object.DontDestroyOnLoad(o);
  32. o.name = prefabName;
  33. _screenLocate = o.GetComponent<ScreenLocate>();
  34. _screenLocate.InfraredCameraHelper = this;
  35. _uvcManager = o.GetComponent<UVCManager>();
  36. _uvcManager.startUVCManager.AddListener(_screenLocate.UVCIsReady);
  37. _uvcManager.updateUVCManager.AddListener(_screenLocate.UVCUpdate);
  38. _uvcManager.DefaultWidth = defWidth;
  39. _uvcManager.DefaultHeight = defHeight;
  40. Debug.Log("InitUVCManagerCamera 初始化相机,size:[" + _uvcManager.DefaultWidth + "," + _uvcManager.DefaultHeight + "]");
  41. _managerObject = o;
  42. if (Application.platform == RuntimePlatform.Android) Application.targetFrameRate = 60;
  43. }
  44. /// <summary>
  45. /// 在pc里面创建此预制
  46. /// </summary>
  47. public void CreateToPc()
  48. {
  49. if (_managerObject) return;
  50. string prefabName = "WebCameraView_Pc";
  51. var o = UnityEngine.Object.Instantiate(Resources.Load<GameObject>(prefabName));
  52. UnityEngine.Object.DontDestroyOnLoad(o);
  53. o.name = "WebCameraView";
  54. _screenLocate = o.GetComponent<ScreenLocate>();
  55. _screenLocate.InfraredCameraHelper = this;
  56. _uvcManager = o.GetComponent<UVCManager>();
  57. _managerObject = o;
  58. }
  59. public void Dispose()
  60. {
  61. if (_managerObject) UnityEngine.Object.DestroyImmediate(_managerObject);
  62. if (_Instance == this) _Instance = null;
  63. }
  64. #endregion
  65. #region 画面接口
  66. public void onStartPreview()
  67. {
  68. _uvcManager.onStartPreview();
  69. }
  70. public void onStopPreview()
  71. {
  72. _uvcManager.onStopPreview();
  73. }
  74. /// <summary>
  75. /// 获取摄像机对象CameraInfo
  76. /// </summary>
  77. public UVCManager.CameraInfo GetUVCCameraInfo()
  78. {
  79. return _screenLocate.mUVCCameraInfo;
  80. }
  81. /// <summary>
  82. /// 获取相机画面贴图
  83. /// </summary>
  84. public Texture GetCameraTexture()
  85. {
  86. return _screenLocate.getUVCTexture; //_screenLocate.rawImage.texture;
  87. }
  88. /// <summary>
  89. /// 获取相机画面材质
  90. /// </summary>
  91. public Material GetCameraMaterial()
  92. {
  93. return null;//_screenLocate.rawImage.material;
  94. }
  95. /// <summary>
  96. /// 设置亮度(-1~1)
  97. /// </summary>
  98. public void SetBrightness(float value)
  99. {
  100. //_screenLocate.rawImage.material.SetFloat("_Brightness", value);
  101. _screenLocate.pcBrightness = value;
  102. }
  103. /// <summary>
  104. /// 获取亮度(-1~1)
  105. /// </summary>
  106. public float GetBrightness()
  107. {
  108. return _screenLocate.pcBrightness;// _screenLocate.rawImage.material.GetFloat("_Brightness");
  109. }
  110. /// <summary>
  111. /// 设置对比度(-1~1)
  112. /// </summary>
  113. public void SetContrast(float value)
  114. {
  115. // _screenLocate.rawImage.material.SetFloat("_Contrast", value);
  116. _screenLocate.pcContrast = value;
  117. }
  118. /// <summary>
  119. /// 获取对比度(-1~1)
  120. /// </summary>
  121. public float GetContrast()
  122. {
  123. return _screenLocate.pcContrast;//_screenLocate.rawImage.material.GetFloat("_Contrast");
  124. }
  125. /// <summary>
  126. /// 设置相机画面分辨率
  127. /// </summary>
  128. public void SetCameraResolution(int width, int height)
  129. {
  130. if (_uvcManager)
  131. {
  132. _uvcManager.DefaultWidth = width;
  133. _uvcManager.DefaultHeight = height;
  134. }
  135. Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolution,width:" + width + " ,height:" + height);
  136. }
  137. /// <summary>
  138. /// 设置相机画面分辨率
  139. /// </summary>
  140. public void SetCameraResolutionNew(int width, int height)
  141. {
  142. Debug.Log("[InfraredCameraHelper]设置分辨率 SetCameraResolutionNew,width:" + width + " ,height:" + height);
  143. _screenLocate.Resize(width, height);
  144. }
  145. /// <summary>
  146. /// 设置default分辨率
  147. /// </summary>
  148. public void SetHighCameraResolution()
  149. {
  150. Vector2 _highResolution = _screenLocate.mUVCCameraInfo.CurrentHighResolution;
  151. _screenLocate.Resize((int)_highResolution.x, (int)_highResolution.y);
  152. }
  153. /// <summary>
  154. /// 设置Low分辨率
  155. /// </summary>
  156. public void SetLowCameraResolution()
  157. {
  158. Vector2 _lowResolution = _screenLocate.mUVCCameraInfo.CurrentLowResolution;
  159. _screenLocate.Resize((int)_lowResolution.x, (int)_lowResolution.y);
  160. }
  161. #endregion
  162. #region 操作接口
  163. /// <summary>
  164. /// 单点模式开关
  165. /// </summary>
  166. public void SetSinglePoint(bool value) => _screenLocate.SetSinglePoint(value);
  167. /// <summary>
  168. /// 是否为单点模式
  169. /// </summary>
  170. public bool IsSinglePoint() => _screenLocate.IsSinglePoint();
  171. /// <summary>
  172. /// 设置抖动过滤值
  173. /// </summary>
  174. public void SetShakeFilterValue(float value)
  175. {
  176. _screenLocate.filterDis = value;
  177. }
  178. /// <summary>
  179. /// 获取抖动过滤值
  180. /// </summary>
  181. public float GetShakeFilterValue()
  182. {
  183. return _screenLocate.filterDis;
  184. }
  185. /// <summary>
  186. /// 设置亮度过滤阈值
  187. /// </summary>
  188. public void SetInfraredLocateBrightnessThreshold(float value)
  189. {
  190. _screenLocate.SetInfraredLocateBrightnessThreshold(value);
  191. }
  192. /// <summary>
  193. /// 是否已经定位
  194. /// </summary>
  195. public bool IsScreenLoateOK()
  196. {
  197. return _screenLocate.IsScreenLoateOK();
  198. }
  199. /// <summary>
  200. /// 是否正在手动定位
  201. /// </summary>
  202. public bool IsScreenLocateManualDoing()
  203. {
  204. return _screenLocate.IsScreenLocateManualDoing();
  205. }
  206. /// <summary>
  207. /// 进入手动定位
  208. /// </summary>
  209. /// <returns>相机画面截图</returns>
  210. public Texture2D EnterScreenLocateManual()
  211. {
  212. return _screenLocate.EnterScreenLocateManual();
  213. }
  214. /// <summary>
  215. /// 退出/完成手动定位
  216. /// </summary>
  217. /// <param name="points">定位点(左下、右下、右上、左上)</param>
  218. public void QuitScreenLocateManual(List<Vector2> points)
  219. {
  220. _screenLocate.QuitScreenLocateManual(points);
  221. }
  222. /// <summary>
  223. /// 获取中心点偏移量
  224. /// </summary>
  225. /// <param name="inputPoint"></param>
  226. /// <param name="type"> CameraLocation , ScreenUV</param>
  227. /// <returns></returns>
  228. public Vector2 GetCenterOffset(Vector2 inputPoint, string type = "CameraLocation")
  229. {
  230. // 获取偏移量
  231. return _screenLocate.AdjustPointsOffset(inputPoint, type);
  232. }
  233. /// <summary>
  234. /// 重置中心点的偏移量
  235. /// </summary>
  236. public void ResetCenterOffset() {
  237. _screenLocate.ResetPointsOffest();
  238. }
  239. /// <summary>
  240. /// 初始化一次本地的记录点
  241. /// </summary>
  242. public void InitScreenLocateManual()
  243. {
  244. //获取本地记录的点
  245. if (ScreenLocate.GetScreenLocateVectorList())
  246. {
  247. Debug.Log("[初始化本地坐标]:" + _screenLocate.PrintVector2List(ScreenLocate.quadUnityVectorList));
  248. //使用算法顺序点来设置屏幕四边形,定位点(左下,右下,左上,右上)
  249. _screenLocate.QuadUnityVectorListToScreenQuad(ScreenLocate.quadUnityVectorList);
  250. // _screenLocate.SyncInfraredDemo();
  251. InvokeOnUVCPosUpdate(ScreenLocate.quadUnityVectorList);
  252. }
  253. }
  254. /// <summary>
  255. /// 进入自动定位
  256. /// </summary>
  257. public void EnterScreenLocateManualAuto()
  258. {
  259. _screenLocate.EnterScreenLocateManualAuto();
  260. }
  261. /// <summary>
  262. /// 修改 Capture ,建议值30
  263. /// </summary>
  264. public void SetCapture(int value)
  265. {
  266. _screenLocate.SetCapture(value);
  267. }
  268. /// <summary>
  269. /// 修改delay,建议值30
  270. /// </summary>
  271. public void SetDelay(int value)
  272. {
  273. _screenLocate.SetDelay(value);
  274. }
  275. #endregion
  276. #region 监听事件
  277. /// <summary>
  278. /// UVC 相机准备事件
  279. /// </summary>
  280. public OnUVCIsReadyEvent OnUVCIsReady;
  281. public delegate void OnUVCIsReadyEvent(UVCManager.CameraInfo camera);
  282. public void InvokeOnUVCIsReady(UVCManager.CameraInfo camera)
  283. {
  284. try
  285. {
  286. OnUVCIsReady?.Invoke(camera);
  287. }
  288. catch (Exception e)
  289. {
  290. Debug.LogError(e);
  291. }
  292. }
  293. /// <summary>
  294. /// UVC 相机更新事件
  295. /// </summary>
  296. public OnUVCIsUpdateEvent OnUVCIsUpdate;
  297. public delegate void OnUVCIsUpdateEvent();
  298. public void InvokeOnUVCIsUpdate()
  299. {
  300. try
  301. {
  302. OnUVCIsUpdate?.Invoke();
  303. }
  304. catch (Exception e)
  305. {
  306. Debug.LogError(e);
  307. }
  308. }
  309. /// <summary>
  310. /// UVC 定位点更新事件
  311. /// </summary>
  312. public OnUVCPosUpdateEvent OnUVCPosUpdate;
  313. public delegate void OnUVCPosUpdateEvent(List<Vector2> list);
  314. public void InvokeOnUVCPosUpdate(List<Vector2> list)
  315. {
  316. try
  317. {
  318. OnUVCPosUpdate?.Invoke(list);
  319. }
  320. catch (Exception e)
  321. {
  322. Debug.LogError(e);
  323. }
  324. }
  325. /// <summary>
  326. /// 单点更新事件
  327. /// </summary>
  328. public OnPositionUpdateEvent OnPositionUpdate;
  329. public delegate void OnPositionUpdateEvent(Vector2 position);
  330. public void InvokeOnPositionUpdate(Vector2 position)
  331. {
  332. try
  333. {
  334. OnPositionUpdate?.Invoke(position);
  335. }
  336. catch (Exception e)
  337. {
  338. Debug.LogError(e);
  339. }
  340. }
  341. /// <summary>
  342. /// 双点更新事件
  343. /// </summary>
  344. public OnPositionUpdate2Event OnPositionUpdate2;
  345. public delegate void OnPositionUpdate2Event(Vector2 position, int index);
  346. public void InvokeOnPositionUpdate2(Vector2 position, int index)
  347. {
  348. try
  349. {
  350. OnPositionUpdate2?.Invoke(position, index);
  351. }
  352. catch (Exception e)
  353. {
  354. Debug.LogError(e);
  355. }
  356. }
  357. #endregion
  358. }
  359. }