PcWebCamera.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 = 1280;
  11. public int height = 720;
  12. public int fps = 60;
  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. }
  126. //else
  127. //{
  128. // Debug.LogError("开启失败,用户未授予摄像头权限!");
  129. //}
  130. }
  131. public void newWebCamTexture(string name)
  132. {
  133. _webCamTexture = new WebCamTexture(name, width, height, fps);
  134. _webCamTexture.Play();
  135. //Debug.Log("[newWebCamTexture]成功开启摄像头");
  136. }
  137. public void OnClick_Close()
  138. {
  139. if (_webCamTexture != null)
  140. {
  141. _webCamTexture.Stop();
  142. _webCamTexture = null;
  143. Debug.Log("[OnClick_Close]成功关闭摄像头");
  144. }
  145. }
  146. public WebCamTexture newWebCamTexture(int width, int height)
  147. {
  148. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  149. _webCamTexture.Play();
  150. Debug.Log("[newWebCamTexture]成功开启摄像头");
  151. return _webCamTexture;
  152. }
  153. //修改亮度和对比度
  154. Color32 AdjustBrightnessAndContrast(Color32 color, float brightness, float contrast)
  155. {
  156. float r = color.r / 255.0f;
  157. float g = color.g / 255.0f;
  158. float b = color.b / 255.0f;
  159. // 调整对比度
  160. r = ((r - 0.5f) * contrast + 0.5f);
  161. g = ((g - 0.5f) * contrast + 0.5f);
  162. b = ((b - 0.5f) * contrast + 0.5f);
  163. // 调整亮度
  164. r += brightness;
  165. g += brightness;
  166. b += brightness;
  167. // 限制值在 0 到 1 之间
  168. r = Mathf.Clamp01(r);
  169. g = Mathf.Clamp01(g);
  170. b = Mathf.Clamp01(b);
  171. return new Color32((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), color.a);
  172. }
  173. }