PcWebCamera.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. StartCoroutine(RestartWebCam());
  34. }
  35. IEnumerator RestartWebCam()
  36. {
  37. yield return new WaitForSecondsRealtime(1); // 延迟 x 秒重启 WebCamTexture
  38. if (_webCamTexture != null)
  39. {
  40. _webCamTexture.Stop();
  41. _webCamTexture.Play();
  42. }
  43. }
  44. private Color32[] pixels;
  45. private Texture2D adjustedTexture;
  46. private int frameCounter = 0;
  47. public int updateInterval = 5; // 每隔多少帧更新一次图像
  48. private void Update()
  49. {
  50. if (_webCamTexture)
  51. {
  52. width = _webCamTexture.width;
  53. height = _webCamTexture.height;
  54. fps = (int)_webCamTexture.requestedFPS;
  55. frameCounter++;
  56. //&& frameCounter % updateInterval == 0
  57. //if (_webCamTexture.didUpdateThisFrame )
  58. //{
  59. // // 获取 WebCamTexture 的像素数据
  60. // pixels = _webCamTexture.GetPixels32();
  61. // // 调整亮度和对比度
  62. // for (int i = 0; i < pixels.Length; i++)
  63. // {
  64. // pixels[i] = AdjustBrightnessAndContrast(pixels[i], brightness, contrast);
  65. // }
  66. // // 将调整后的像素数据应用到 Texture2D
  67. // adjustedTexture.SetPixels32(pixels);
  68. // adjustedTexture.Apply();
  69. // // 重置 frameCounter 以防溢出
  70. // if (frameCounter >= int.MaxValue - 1)
  71. // {
  72. // frameCounter = 0;
  73. // }
  74. //}
  75. }
  76. }
  77. public Vector2Int IndexToCoord(int i)
  78. {
  79. var y = i / width;
  80. var x = i % width;
  81. return new Vector2Int(x, y);
  82. }
  83. public int CoordToIndex(int x, int y)
  84. {
  85. return y * width + x;
  86. }
  87. public void OnClick_Open()
  88. {
  89. if (_webCamTexture != null)
  90. {
  91. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  92. return;
  93. }
  94. //if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  95. {
  96. WebCamDevice[] devices = WebCamTexture.devices;
  97. for (int i = 0; i < devices.Length; i++)
  98. {
  99. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  100. }
  101. if (devices.Length == 0)
  102. {
  103. Debug.LogError("开启失败,没找到可用的摄像头!");
  104. return;
  105. }
  106. if (devices.Length < cameraIndex + 1)
  107. {
  108. Debug.LogError("开启失败,没有对应序号的摄像头!");
  109. return;
  110. }
  111. deviceName = devices[cameraIndex].name;
  112. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  113. _webCamTexture.Play();
  114. // 创建一个 Texture2D 用于存储调整后的图像
  115. //adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  116. Debug.Log("成功开启摄像头 " + deviceName);
  117. ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  118. }
  119. //else
  120. //{
  121. // Debug.LogError("开启失败,用户未授予摄像头权限!");
  122. //}
  123. }
  124. public void newWebCamTexture(string name)
  125. {
  126. _webCamTexture = new WebCamTexture(name, width, height, fps);
  127. _webCamTexture.Play();
  128. //Debug.Log("[newWebCamTexture]成功开启摄像头");
  129. }
  130. public void OnClick_Close()
  131. {
  132. if (_webCamTexture != null)
  133. {
  134. _webCamTexture.Stop();
  135. _webCamTexture = null;
  136. Debug.Log("[OnClick_Close]成功关闭摄像头");
  137. }
  138. }
  139. public WebCamTexture newWebCamTexture(int width, int height)
  140. {
  141. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  142. _webCamTexture.Play();
  143. Debug.Log("[newWebCamTexture]成功开启摄像头");
  144. return _webCamTexture;
  145. }
  146. //修改亮度和对比度
  147. Color32 AdjustBrightnessAndContrast(Color32 color, float brightness, float contrast)
  148. {
  149. float r = color.r / 255.0f;
  150. float g = color.g / 255.0f;
  151. float b = color.b / 255.0f;
  152. // 调整对比度
  153. r = ((r - 0.5f) * contrast + 0.5f);
  154. g = ((g - 0.5f) * contrast + 0.5f);
  155. b = ((b - 0.5f) * contrast + 0.5f);
  156. // 调整亮度
  157. r += brightness;
  158. g += brightness;
  159. b += brightness;
  160. // 限制值在 0 到 1 之间
  161. r = Mathf.Clamp01(r);
  162. g = Mathf.Clamp01(g);
  163. b = Mathf.Clamp01(b);
  164. return new Color32((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), color.a);
  165. }
  166. }