PcWebCamera.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.SceneManagement;
  4. using UnityEngine.UI;
  5. // using AndroidJavaClass = UnityEngine.AndroidJNI.AndroidJavaClass;
  6. // using AndroidJavaObject = UnityEngine.AndroidJNI.AndroidJavaObject;
  7. public class PcWebCamera : MonoBehaviour
  8. {
  9. public int cameraIndex = 0;
  10. public int width = 320;
  11. public int height = 240;
  12. public int fps = 30;
  13. public string deviceName;
  14. private WebCamTexture _webCamTexture;
  15. public WebCamTexture webCamTexture { get => _webCamTexture; set => _webCamTexture = value; }
  16. public Vector2 Size => new Vector2(width, height);
  17. public float brightness = 0.0f; // 亮度调整值,范围 -1.0 到 1.0
  18. public float contrast = 1.0f; // 对比度调整值,范围 0.0 到 2.0
  19. private void Awake()
  20. {
  21. SceneManager.sceneLoaded += OnSceneLoaded;
  22. }
  23. private IEnumerator Start()
  24. {
  25. yield return new WaitForEndOfFrame();
  26. OnClick_Open();
  27. }
  28. void OnSceneLoaded(Scene scene, LoadSceneMode mode)
  29. {
  30. Debug.Log("Scene Loaded: " + scene.name);
  31. Debug.Log("Load Scene Mode: " + mode);
  32. // 在场景加载时执行的代码
  33. if (BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToHOUYIPRO() || BluetoothAim.ins.isMainConnectToGun()))
  34. {
  35. StartCoroutine(RestartWebCam());
  36. }
  37. else if (scene.name == "Home") {
  38. //没连接设备时候,如果跳回home页面,设置一次
  39. StartCoroutine(RestartWebCam());
  40. }
  41. }
  42. IEnumerator RestartWebCam()
  43. {
  44. yield return new WaitForSecondsRealtime(1); // 延迟 x 秒重启 WebCamTexture
  45. if (_webCamTexture != null)
  46. {
  47. _webCamTexture.Stop();
  48. _webCamTexture.Play();
  49. }
  50. }
  51. private Color32[] pixels;
  52. private Texture2D adjustedTexture;
  53. private int frameCounter = 0;
  54. public int updateInterval = 5; // 每隔多少帧更新一次图像
  55. private void Update()
  56. {
  57. if (_webCamTexture)
  58. {
  59. width = _webCamTexture.width;
  60. height = _webCamTexture.height;
  61. fps = (int)_webCamTexture.requestedFPS;
  62. frameCounter++;
  63. //&& frameCounter % updateInterval == 0
  64. //if (_webCamTexture.didUpdateThisFrame )
  65. //{
  66. // // 获取 WebCamTexture 的像素数据
  67. // pixels = _webCamTexture.GetPixels32();
  68. // // 调整亮度和对比度
  69. // for (int i = 0; i < pixels.Length; i++)
  70. // {
  71. // pixels[i] = AdjustBrightnessAndContrast(pixels[i], brightness, contrast);
  72. // }
  73. // // 将调整后的像素数据应用到 Texture2D
  74. // adjustedTexture.SetPixels32(pixels);
  75. // adjustedTexture.Apply();
  76. // // 重置 frameCounter 以防溢出
  77. // if (frameCounter >= int.MaxValue - 1)
  78. // {
  79. // frameCounter = 0;
  80. // }
  81. //}
  82. }
  83. }
  84. public Vector2Int IndexToCoord(int i)
  85. {
  86. var y = i / width;
  87. var x = i % width;
  88. return new Vector2Int(x, y);
  89. }
  90. public int CoordToIndex(int x, int y)
  91. {
  92. return y * width + x;
  93. }
  94. public void OnClick_Open()
  95. {
  96. if (_webCamTexture != null)
  97. {
  98. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  99. return;
  100. }
  101. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  102. {
  103. WebCamDevice[] devices = WebCamTexture.devices;
  104. for (int i = 0; i < devices.Length; i++)
  105. {
  106. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  107. }
  108. if (devices.Length == 0)
  109. {
  110. Debug.LogError("开启失败,没找到可用的摄像头!");
  111. return;
  112. }
  113. if (devices.Length < cameraIndex + 1)
  114. {
  115. Debug.LogError("开启失败,没有对应序号的摄像头!");
  116. return;
  117. }
  118. deviceName = devices[cameraIndex].name;
  119. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  120. _webCamTexture.Play();
  121. // 创建一个 Texture2D 用于存储调整后的图像
  122. //adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  123. Debug.Log("成功开启摄像头 " + deviceName);
  124. ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  125. // StartCoroutine(getResolutions(devices[cameraIndex]));
  126. }
  127. else
  128. {
  129. Debug.LogError("开启失败,用户未授予摄像头权限!");
  130. }
  131. }
  132. IEnumerator getResolutions(WebCamDevice device) {
  133. yield return new WaitForSeconds(1.0f);
  134. // 打印支持的分辨率
  135. Resolution[] resolutions = device.availableResolutions;
  136. if (resolutions != null && resolutions.Length > 0)
  137. {
  138. foreach (Resolution res in resolutions)
  139. {
  140. Debug.Log("支持的分辨率: " + res.width + "x" + res.height);
  141. }
  142. }
  143. else
  144. {
  145. Debug.Log("没有可用的分辨率信息。");
  146. }
  147. }
  148. public void newWebCamTexture(string name)
  149. {
  150. _webCamTexture = new WebCamTexture(name, width, height, fps);
  151. _webCamTexture.Play();
  152. //Debug.Log("[newWebCamTexture]成功开启摄像头");
  153. }
  154. public void OnClick_Close()
  155. {
  156. if (_webCamTexture != null)
  157. {
  158. _webCamTexture.Stop();
  159. _webCamTexture = null;
  160. Debug.Log("[OnClick_Close]成功关闭摄像头");
  161. }
  162. }
  163. public WebCamTexture newWebCamTexture(int width, int height)
  164. {
  165. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  166. _webCamTexture.Play();
  167. Debug.Log("[newWebCamTexture]成功开启摄像头");
  168. return _webCamTexture;
  169. }
  170. //修改亮度和对比度
  171. Color32 AdjustBrightnessAndContrast(Color32 color, float brightness, float contrast)
  172. {
  173. float r = color.r / 255.0f;
  174. float g = color.g / 255.0f;
  175. float b = color.b / 255.0f;
  176. // 调整对比度
  177. r = ((r - 0.5f) * contrast + 0.5f);
  178. g = ((g - 0.5f) * contrast + 0.5f);
  179. b = ((b - 0.5f) * contrast + 0.5f);
  180. // 调整亮度
  181. r += brightness;
  182. g += brightness;
  183. b += brightness;
  184. // 限制值在 0 到 1 之间
  185. r = Mathf.Clamp01(r);
  186. g = Mathf.Clamp01(g);
  187. b = Mathf.Clamp01(b);
  188. return new Color32((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), color.a);
  189. }
  190. }