MyUVCManager.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Rect = UnityEngine.Rect;
  6. using UnityEngine.UI;
  7. namespace SLAMUVC
  8. {
  9. [RequireComponent(typeof(UVCInterface))]
  10. public class MyUVCManager : MonoBehaviour
  11. {
  12. //public UVCCameraPixelFormat PixelFormat = UVCCameraPixelFormat.PIXEL_FORMAT_RGBX;
  13. public int width = 1280;
  14. public int height = 720;
  15. public CameraFormat mCameraFormat = CameraFormat.MJPEG;
  16. public RawImage rawImage;
  17. protected UVCInterface _interface;
  18. //protected List<string> _devices = new List<string>();
  19. private string[] resolutions;
  20. private bool isGetResolutions = false;
  21. // 滑块的当前值
  22. public float sliderValue;
  23. // 用于选择当前显示的 UVCCtrl
  24. private int selectedIndex = 0; // 用于选择当前显示的 UVCCtrl
  25. private int sliderStartPos = 100; // 滑块起始 X 坐标
  26. private int sliderWidth = 200; // 滑块的宽度
  27. private int sliderHeight = 20; // 滑块的高度
  28. private int controlHeight = 30; // 控件的高度
  29. private int verticalSpacing = 10; // 控件垂直间距
  30. private int brightnessValue;
  31. private int contrastValue;
  32. private void Awake()
  33. {
  34. _interface = GetComponent<UVCInterface>();
  35. _interface.systemCameraPermissionHandle += () =>
  36. {
  37. //授权系统相机之后,初始化红外相机
  38. _interface.InitCamera(width, height);
  39. };
  40. }
  41. // Use this for initialization
  42. void Start()
  43. {
  44. //_interface.InitCamera(width, height);
  45. // 初始化滑块的值
  46. sliderValue = 5.0f;
  47. }
  48. void Update()
  49. {
  50. if (rawImage != null || rawImage.texture.GetNativeTexturePtr() != _interface.CameraTexture.GetNativeTexturePtr())
  51. {
  52. rawImage.texture = _interface.CameraTexture;
  53. // GameObject cube = GameObject.Find("Cube");
  54. // cube.GetComponent<Renderer>().material.mainTexture = _interface.CameraTexture;
  55. }
  56. }
  57. void OnGUI()
  58. {
  59. int buttonWidth = 200;
  60. int buttonHeight = 80;
  61. int spacing = 10; // 间距
  62. int startX = 10; // 按钮的起始X坐标
  63. int startY = 10; // 按钮的起始Y坐标
  64. if (GUI.Button(new Rect(startX, startY, buttonWidth, buttonHeight), "OpenCamera"))
  65. {
  66. _interface.OpenCamera();
  67. }
  68. if (GUI.Button(new Rect(startX + buttonWidth + spacing, startY, buttonWidth, buttonHeight), "CloseCamera"))
  69. {
  70. _interface.CloseCamera();
  71. }
  72. if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 2, startY, buttonWidth, buttonHeight), "GetUvcCtrlList"))
  73. {
  74. _interface.GetUvcCtrlList();
  75. }
  76. if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 2, startY + buttonHeight + spacing, buttonWidth, buttonHeight), "重置参数"))
  77. {
  78. _interface.resetControlParams();
  79. }
  80. if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 3, startY, buttonWidth, buttonHeight), "水平翻转(H)"))
  81. {
  82. _interface.FlipHorizontally();
  83. }
  84. if (GUI.Button(new Rect(startX + (buttonWidth + spacing) * 3, startY + buttonHeight + spacing, buttonWidth, buttonHeight), "垂直翻转(V)"))
  85. {
  86. _interface.FlipVertically();
  87. }
  88. bool isPreviewing = _interface.GetIsPreviewing();
  89. if (isPreviewing)
  90. {
  91. int offsetY = startY + buttonHeight + spacing;
  92. if (resolutions == null && !isGetResolutions)
  93. {
  94. isGetResolutions = true;
  95. resolutions = _interface.GetSupportedResolutions();
  96. }
  97. if (resolutions != null)
  98. {
  99. for (int i = 0; i < resolutions.Length; i++)
  100. {
  101. string resolution = resolutions[i];
  102. if (GUI.Button(new Rect(startX, offsetY, buttonWidth * 2 + spacing, buttonHeight), resolution))
  103. {
  104. string[] res = resolution.ToString().Split('x');
  105. width = int.Parse(res[0]);
  106. height = int.Parse(res[1]);
  107. _interface.ChangeCameraInfo(width, height);
  108. }
  109. offsetY += buttonHeight + spacing;
  110. }
  111. }
  112. int sliderStartPos = startX + (buttonWidth + spacing) * 4 + 50;
  113. var uvcCtrls = _interface.UVCCtrls;
  114. if (uvcCtrls == null)
  115. {
  116. GUI.Label(new Rect(sliderStartPos, 10, 200, 30), "No data available.");
  117. return;
  118. }
  119. int yPos = 50;
  120. foreach (UVCCtrl uvcCtrl in uvcCtrls)
  121. {
  122. GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), $"Name: {uvcCtrl.name}");
  123. yPos += controlHeight + verticalSpacing;
  124. // 检查并显示亮度滑块
  125. if (uvcCtrl.name.Equals("PU_BRIGHTNESS", System.StringComparison.OrdinalIgnoreCase))
  126. {
  127. GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), "Brightness");
  128. yPos += controlHeight + verticalSpacing;
  129. // 临时变量用于控制亮度滑块
  130. uvcCtrl.value = (int)GUI.HorizontalSlider(new Rect(sliderStartPos, yPos, sliderWidth, sliderHeight), uvcCtrl.value, uvcCtrl.limit[0], uvcCtrl.limit[1]);
  131. yPos += sliderHeight + verticalSpacing;
  132. if (brightnessValue != uvcCtrl.value)
  133. {
  134. brightnessValue = uvcCtrl.value;
  135. SetBrightness(uvcCtrl.value);
  136. }
  137. // 按钮设置亮度
  138. //if (GUI.Button(new Rect(sliderStartPos, yPos, sliderWidth, controlHeight), "Set Brightness"))
  139. //{
  140. // // uvcCtrl.value = Mathf.RoundToInt(brightnessValue);
  141. // SetBrightness(uvcCtrl.value);
  142. //}
  143. yPos += controlHeight + verticalSpacing;
  144. }
  145. else if (uvcCtrl.name.Equals("PU_CONTRAST", System.StringComparison.OrdinalIgnoreCase))
  146. {
  147. GUI.Label(new Rect(sliderStartPos, yPos, 200, 30), "Contrast");
  148. yPos += controlHeight + verticalSpacing;
  149. // 临时变量用于控制对比度滑块
  150. uvcCtrl.value = (int)GUI.HorizontalSlider(new Rect(sliderStartPos, yPos, sliderWidth, sliderHeight), uvcCtrl.value, uvcCtrl.limit[0], uvcCtrl.limit[1]);
  151. yPos += sliderHeight + verticalSpacing;
  152. if (contrastValue != uvcCtrl.value)
  153. {
  154. contrastValue = uvcCtrl.value;
  155. SetContrast(uvcCtrl.value);
  156. }
  157. // 按钮设置对比度
  158. //if (GUI.Button(new Rect(sliderStartPos, yPos, sliderWidth, controlHeight), "Set Contrast"))
  159. //{
  160. // //uvcCtrl.value = Mathf.RoundToInt(contrastValue);
  161. // SetContrast(uvcCtrl.value);
  162. //}
  163. yPos += controlHeight + verticalSpacing;
  164. }
  165. }
  166. }
  167. }
  168. public void SetBrightness(int value)
  169. {
  170. _interface.SetBrightness(value);
  171. }
  172. public void SetContrast(int value)
  173. {
  174. _interface.SetContrast(value);
  175. }
  176. // Update is called once per frame
  177. //void OnGUI()
  178. //{
  179. // int offset = 10;
  180. // if (GUI.Button(new Rect(10, offset, 400, 100), "OpenCamera"))
  181. // {
  182. // _interface.OpenCamera();
  183. // }
  184. // if (GUI.Button(new Rect(420, offset, 400, 100), "CloseCamera"))
  185. // {
  186. // _interface.CloseCamera();
  187. // }
  188. // bool isPreviewing = _interface.GetIsPreviewing();
  189. // if (isPreviewing)
  190. // {
  191. // int offset2 = 10;
  192. // if (GUI.Button(new Rect(220, offset2, 400, 100), "MJPEG"))
  193. // {
  194. // mCameraFormat = CameraFormat.MJPEG;
  195. // }
  196. // offset2 += 110;
  197. // if (GUI.Button(new Rect(220, offset2, 400, 100), "YUV"))
  198. // {
  199. // mCameraFormat = CameraFormat.YUV;
  200. // }
  201. // // resolutions
  202. // if(resolutions == null) resolutions = _interface.GetSupportedResolutions();
  203. // int offset3 = 10;
  204. // if (resolutions != null)
  205. // {
  206. // foreach (var resolution in resolutions)
  207. // {
  208. // if (GUI.Button(new Rect(430, offset3, 400, 100), resolution))
  209. // {
  210. // string[] res = resolution.ToString().Split('x');
  211. // width = int.Parse(res[0]);
  212. // height = int.Parse(res[1]);
  213. // _interface.ChangeCameraInfo(width, height, mCameraFormat.ToString());
  214. // }
  215. // offset3 += 110;
  216. // }
  217. // }
  218. // }
  219. //}
  220. }
  221. }