PcWebCamera.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. // using AndroidJavaClass = UnityEngine.AndroidJNI.AndroidJavaClass;
  7. // using AndroidJavaObject = UnityEngine.AndroidJNI.AndroidJavaObject;
  8. public class PcWebCamera : MonoBehaviour
  9. {
  10. public int cameraIndex = 0;
  11. public int width = 320;
  12. public int height = 240;
  13. public int fps = 30;
  14. public string deviceName;
  15. private WebCamTexture _webCamTexture;
  16. public WebCamTexture webCamTexture { get => _webCamTexture; set => _webCamTexture = value; }
  17. public Vector2 Size => new Vector2(width, height);
  18. public float brightness = 0.0f; // 亮度调整值,范围 -1.0 到 1.0
  19. public float contrast = 1.0f; // 对比度调整值,范围 0.0 到 2.0
  20. private void Awake()
  21. {
  22. SceneManager.sceneLoaded += OnSceneLoaded;
  23. // SceneManager.sceneUnloaded += OnSceneUnloaded;
  24. }
  25. private IEnumerator Start()
  26. {
  27. yield return new WaitForEndOfFrame();
  28. OnClick_Open();
  29. }
  30. private void OnDestroy()
  31. {
  32. OnClick_Close();
  33. }
  34. void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  35. {
  36. Debug.Log("Scene Loaded: " + scene.name);
  37. Debug.Log("Load Scene Mode: " + mode);
  38. StartCoroutine(RestartWebCam());
  39. // 在场景加载时执行的代码
  40. //if (BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToInfraredDevice() || BluetoothAim.ins.isMainConnectToGun()))
  41. //{
  42. // StartCoroutine(RestartWebCam());
  43. //}
  44. //else if (scene.name == "Home") {
  45. // //没连接设备时候,如果跳回home页面,设置一次
  46. // StartCoroutine(RestartWebCam());
  47. //}
  48. }
  49. void OnSceneUnloaded(Scene scene) {
  50. if (_webCamTexture != null && _webCamTexture.isPlaying)
  51. {
  52. _webCamTexture.Stop();
  53. }
  54. }
  55. IEnumerator RestartWebCam()
  56. {
  57. //yield return new WaitForSecondsRealtime(1); // 延迟 x 秒重启 WebCamTexture
  58. yield return new WaitForEndOfFrame();
  59. if (_webCamTexture != null)
  60. {
  61. //_webCamTexture.Stop();
  62. _webCamTexture.Play();
  63. }
  64. }
  65. private void Update()
  66. {
  67. //if (_webCamTexture)
  68. //{
  69. // width = _webCamTexture.width;
  70. // height = _webCamTexture.height;
  71. // fps = (int)_webCamTexture.requestedFPS;
  72. //}
  73. if (_webCamTexture && _webCamTexture.didUpdateThisFrame)
  74. {
  75. width = _webCamTexture.width;
  76. height = _webCamTexture.height;
  77. fps = (int)_webCamTexture.requestedFPS;
  78. AdjustTexture();
  79. ScreenLocate.Main.setUVCTexture = adjustedTexture;
  80. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  81. }
  82. }
  83. public Vector2Int IndexToCoord(int i)
  84. {
  85. var y = i / width;
  86. var x = i % width;
  87. return new Vector2Int(x, y);
  88. }
  89. public int CoordToIndex(int x, int y)
  90. {
  91. return y * width + x;
  92. }
  93. public void OnClick_Open()
  94. {
  95. if (_webCamTexture != null)
  96. {
  97. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  98. return;
  99. }
  100. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  101. {
  102. WebCamDevice[] devices = WebCamTexture.devices;
  103. for (int i = 0; i < devices.Length; i++)
  104. {
  105. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  106. }
  107. if (devices.Length == 0)
  108. {
  109. Debug.LogError("开启失败,没找到可用的摄像头!");
  110. return;
  111. }
  112. if (devices.Length < cameraIndex + 1)
  113. {
  114. Debug.LogError("开启失败,没有对应序号的摄像头!");
  115. return;
  116. }
  117. deviceName = devices[cameraIndex].name;
  118. //StartCoroutine(DetectResolutions());
  119. StartWebCam(width, height);
  120. //Debug.Log("PCWebCamera fps:" + fps + ",size:[" + width + "," + height + "]");
  121. //_webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  122. //_webCamTexture.Play();
  123. //Debug.Log("PCWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
  124. //// 创建一个 Texture2D 用于存储调整后的图像
  125. ////adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  126. //ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  127. }
  128. else
  129. {
  130. Debug.LogError("开启失败,用户未授予摄像头权限!");
  131. }
  132. }
  133. private int[] widths = { 320, 160 };
  134. private int[] heights = { 240, 120 };
  135. private List<Resolution> supportedResolutions = new List<Resolution>();
  136. private IEnumerator DetectResolutions()
  137. {
  138. for (int i = 0; i < 2; i++)
  139. {
  140. int _width = widths[i], _height = heights[i];
  141. _webCamTexture = new WebCamTexture(deviceName, _width, _height, fps);
  142. _webCamTexture.Play();
  143. // Wait for a short time to let the webcam initialize
  144. yield return new WaitForSeconds(2);
  145. if (_webCamTexture.width > 16 && _webCamTexture.height > 16)
  146. {
  147. Debug.Log("Supported resolution: " + _webCamTexture.width + " x " + _webCamTexture.height);
  148. supportedResolutions.Add(new Resolution { width = _webCamTexture.width, height = _webCamTexture.height });
  149. }
  150. else
  151. {
  152. Debug.Log("Resolution not supported: " + width + " x " + height);
  153. }
  154. _webCamTexture.Stop();
  155. yield return null;
  156. }
  157. // 执行下一步操作,例如选择一个支持的分辨率并启动摄像头
  158. if (supportedResolutions.Count > 0)
  159. {
  160. Resolution selectedResolution = supportedResolutions[0]; // 选择第一个支持的分辨率(你可以根据需要选择不同的分辨率)
  161. StartWebCam(selectedResolution.width, selectedResolution.height);
  162. }
  163. else
  164. {
  165. Debug.LogError("No supported resolutions found.");
  166. }
  167. }
  168. private void StartWebCam(int _width, int _height)
  169. {
  170. Debug.Log("PCWebCamera fps:" + fps + ",size:[" + _width + "," + _height + "]");
  171. _webCamTexture = new WebCamTexture(deviceName, _width, _height, fps);
  172. _webCamTexture.Play();
  173. Debug.Log("PCWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
  174. ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  175. }
  176. public void newWebCamTexture(string name)
  177. {
  178. _webCamTexture = new WebCamTexture(name, width, height, fps);
  179. _webCamTexture.Play();
  180. //Debug.Log("[newWebCamTexture]成功开启摄像头");
  181. }
  182. public void OnClick_Close()
  183. {
  184. if (_webCamTexture != null)
  185. {
  186. _webCamTexture.Stop();
  187. _webCamTexture = null;
  188. Debug.Log("[OnClick_Close]成功关闭摄像头");
  189. }
  190. }
  191. public WebCamTexture newWebCamTexture(int width, int height)
  192. {
  193. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  194. _webCamTexture.Play();
  195. Debug.Log("[newWebCamTexture]成功开启摄像头");
  196. return _webCamTexture;
  197. }
  198. void AdjustBrightnessContrast(Color32[] pixels, float brightness, float contrast)
  199. {
  200. float contrastFactor = 1.0f + contrast;
  201. for (int i = 0; i < pixels.Length; i++)
  202. {
  203. Color32 pixel = pixels[i];
  204. float r = pixel.r / 255.0f;
  205. float g = pixel.g / 255.0f;
  206. float b = pixel.b / 255.0f;
  207. r = Mathf.Clamp01(((r - 0.5f) * contrastFactor + 0.5f) + brightness);
  208. g = Mathf.Clamp01(((g - 0.5f) * contrastFactor + 0.5f) + brightness);
  209. b = Mathf.Clamp01(((b - 0.5f) * contrastFactor + 0.5f) + brightness);
  210. pixel.r = (byte)(r * 255);
  211. pixel.g = (byte)(g * 255);
  212. pixel.b = (byte)(b * 255);
  213. pixels[i] = pixel;
  214. }
  215. }
  216. private Texture2D adjustedTexture;
  217. private void AdjustTexture()
  218. {
  219. Color32[] pixels = _webCamTexture.GetPixels32();
  220. AdjustBrightnessContrast(pixels, ScreenLocate.Main.pcBrightness, ScreenLocate.Main.pcContrast);
  221. if (adjustedTexture == null || adjustedTexture.width != _webCamTexture.width || adjustedTexture.height != _webCamTexture.height)
  222. {
  223. adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  224. }
  225. adjustedTexture.SetPixels32(pixels);
  226. adjustedTexture.Apply();
  227. }
  228. }