InfraredCameraHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 SetCenterOffset(Vector2 inputPointOffset, string type = "CameraLocation")
  229. {
  230. // 获取偏移量
  231. return _screenLocate.SetPointsOffset(inputPointOffset, type);
  232. }
  233. /// <summary>
  234. /// 获取中心点偏移量
  235. /// </summary>
  236. /// <param name="inputPoint"></param>
  237. /// <param name="type"> CameraLocation , ScreenUV</param>
  238. /// <returns></returns>
  239. public Vector2 GetCenterOffset(Vector2 inputPoint, string type = "CameraLocation")
  240. {
  241. // 获取偏移量
  242. return _screenLocate.AdjustPointsOffset(inputPoint, type);
  243. }
  244. /// <summary>
  245. /// 重置中心点的偏移量
  246. /// </summary>
  247. public void ResetCenterOffset() {
  248. _screenLocate.ResetPointsOffest();
  249. }
  250. /// <summary>
  251. /// 撤销回退中心点的偏移量操作。
  252. /// </summary>
  253. public void RevokeCenterOffset()
  254. {
  255. _screenLocate.RevokePointsOffest();
  256. }
  257. /// <summary>
  258. /// 清除定位数据
  259. /// </summary>
  260. public void ClearInfraredPositioningData(bool bSyncLocal = false) {
  261. //重置识别点
  262. ScreenLocate.Main.ScreenIdentification.ClearQuadCache();
  263. //清除一下记录的点
  264. ScreenLocate.quadUnityVectorList.Clear();
  265. //是否需要保存清除后的数据
  266. if(bSyncLocal) ScreenLocate.SaveScreenLocateVectorList();
  267. }
  268. /// <summary>
  269. /// 初始化一次本地的记录点
  270. /// </summary>
  271. public void InitScreenLocateManual()
  272. {
  273. //获取本地记录的点
  274. if (ScreenLocate.GetScreenLocateVectorList())
  275. {
  276. Debug.Log("[初始化本地坐标]:" + _screenLocate.PrintVector2List(ScreenLocate.quadUnityVectorList));
  277. //使用算法顺序点来设置屏幕四边形,定位点(左下,右下,左上,右上)
  278. _screenLocate.QuadUnityVectorListToScreenQuad(ScreenLocate.quadUnityVectorList);
  279. // _screenLocate.SyncInfraredDemo();
  280. InvokeOnUVCPosUpdate(ScreenLocate.quadUnityVectorList);
  281. }
  282. }
  283. /// <summary>
  284. /// 进入自动定位
  285. /// </summary>
  286. public void EnterScreenLocateManualAuto()
  287. {
  288. _screenLocate.EnterScreenLocateManualAuto();
  289. }
  290. /// <summary>
  291. /// 修改 Capture ,建议值30
  292. /// </summary>
  293. public void SetCapture(int value)
  294. {
  295. _screenLocate.SetCapture(value);
  296. }
  297. /// <summary>
  298. /// 修改delay,建议值30
  299. /// </summary>
  300. public void SetDelay(int value)
  301. {
  302. _screenLocate.SetDelay(value);
  303. }
  304. #endregion
  305. #region 监听事件
  306. /// <summary>
  307. /// UVC 相机准备事件
  308. /// </summary>
  309. public OnUVCIsReadyEvent OnUVCIsReady;
  310. public delegate void OnUVCIsReadyEvent(UVCManager.CameraInfo camera);
  311. public void InvokeOnUVCIsReady(UVCManager.CameraInfo camera)
  312. {
  313. try
  314. {
  315. OnUVCIsReady?.Invoke(camera);
  316. }
  317. catch (Exception e)
  318. {
  319. Debug.LogError(e);
  320. }
  321. }
  322. /// <summary>
  323. /// UVC 相机更新事件
  324. /// </summary>
  325. public OnUVCIsUpdateEvent OnUVCIsUpdate;
  326. public delegate void OnUVCIsUpdateEvent();
  327. public void InvokeOnUVCIsUpdate()
  328. {
  329. try
  330. {
  331. OnUVCIsUpdate?.Invoke();
  332. }
  333. catch (Exception e)
  334. {
  335. Debug.LogError(e);
  336. }
  337. }
  338. /// <summary>
  339. /// UVC 定位点更新事件
  340. /// </summary>
  341. public OnUVCPosUpdateEvent OnUVCPosUpdate;
  342. public delegate void OnUVCPosUpdateEvent(List<Vector2> list);
  343. public void InvokeOnUVCPosUpdate(List<Vector2> list)
  344. {
  345. try
  346. {
  347. OnUVCPosUpdate?.Invoke(list);
  348. }
  349. catch (Exception e)
  350. {
  351. Debug.LogError(e);
  352. }
  353. }
  354. /// <summary>
  355. /// 单点更新事件
  356. /// </summary>
  357. public OnPositionUpdateEvent OnPositionUpdate;
  358. public delegate void OnPositionUpdateEvent(Vector2 position);
  359. public void InvokeOnPositionUpdate(Vector2 position)
  360. {
  361. try
  362. {
  363. OnPositionUpdate?.Invoke(position);
  364. }
  365. catch (Exception e)
  366. {
  367. Debug.LogError(e);
  368. }
  369. }
  370. /// <summary>
  371. /// 双点更新事件
  372. /// </summary>
  373. public OnPositionUpdate2Event OnPositionUpdate2;
  374. public delegate void OnPositionUpdate2Event(Vector2 position, int index);
  375. public void InvokeOnPositionUpdate2(Vector2 position, int index)
  376. {
  377. try
  378. {
  379. OnPositionUpdate2?.Invoke(position, index);
  380. }
  381. catch (Exception e)
  382. {
  383. Debug.LogError(e);
  384. }
  385. }
  386. #endregion
  387. }
  388. }