PcWebCamera.cs 7.9 KB

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