MyUVCManager.cs 9.7 KB

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