InfraredCameraHelper.cs 12 KB

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