ZIMWebCamera.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. // using AndroidJavaClass = UnityEngine.AndroidJNI.AndroidJavaClass;
  5. // using AndroidJavaObject = UnityEngine.AndroidJNI.AndroidJavaObject;
  6. public class ZIMWebCamera : MonoBehaviour
  7. {
  8. int cameraIndex = 0;
  9. [SerializeField] public InputField inputIndex;
  10. public int width = 1280;
  11. public int height = 720;
  12. public int fps = 60;
  13. public RawImage rawImage;
  14. public RawImage rawImageSize;
  15. public MaintainAspectRatio maintainAspectRatio;
  16. private WebCamTexture _webCamTexture;
  17. public WebCamTexture webCamTexture { get => _webCamTexture; }
  18. public Vector2 Size => new Vector2(width, height);
  19. public Text logText;
  20. public Transform mParent;
  21. private IEnumerator Start()
  22. {
  23. yield return new WaitForEndOfFrame();
  24. OnClick_Open();
  25. }
  26. private void Awake()
  27. {
  28. }
  29. private void Update()
  30. {
  31. if (_webCamTexture && _webCamTexture.didUpdateThisFrame)
  32. {
  33. width = _webCamTexture.width;
  34. height = _webCamTexture.height;
  35. fps = (int)_webCamTexture.requestedFPS;
  36. rawImageSize.texture = rawImage.texture;
  37. //rawImageSize.SetNativeSize();
  38. maintainAspectRatio.AdjustSize();
  39. AdjustTexture();
  40. ScreenLocate.Main.setUVCTexture = adjustedTexture;
  41. rawImage.texture = ScreenLocate.Main.getUVCTexture;
  42. }
  43. }
  44. public Vector2Int IndexToCoord(int i)
  45. {
  46. var y = i / width;
  47. var x = i % width;
  48. return new Vector2Int(x, y);
  49. }
  50. public int CoordToIndex(int x, int y)
  51. {
  52. return y * width + x;
  53. }
  54. public void AdjustResolution(int width, int height)
  55. {
  56. _webCamTexture.Stop();
  57. _webCamTexture = new WebCamTexture(_webCamTexture.deviceName, width, height);
  58. _webCamTexture.Play();
  59. Debug.Log("[ZIMWebCamera] 分辨率调整: " + _webCamTexture.width + "×" + _webCamTexture.height);
  60. }
  61. public void OnClick_Open()
  62. {
  63. if (!int.TryParse(inputIndex.text, out cameraIndex))
  64. {
  65. cameraIndex = 0;
  66. inputIndex.text = "0";
  67. }
  68. if (_webCamTexture != null)
  69. {
  70. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  71. return;
  72. }
  73. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  74. {
  75. WebCamDevice[] devices = WebCamTexture.devices;
  76. for (int i = 0; i < devices.Length; i++)
  77. {
  78. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  79. this.logText.text += "devices[" + i + "].name:" + devices[i].name + "\n";
  80. }
  81. if (devices.Length == 0)
  82. {
  83. Debug.LogError("开启失败,没找到可用的摄像头!");
  84. return;
  85. }
  86. if (devices.Length < cameraIndex + 1)
  87. {
  88. Debug.Log($"<color=yellow>开启失败,没有对应序号的摄像头{cameraIndex}! 尝试启动默认摄像头0</color>");
  89. cameraIndex = 0;
  90. inputIndex.text = "0";
  91. //return;
  92. }
  93. string deviceName = devices[cameraIndex].name;
  94. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  95. //try
  96. {
  97. _webCamTexture.Play();
  98. // if (rawImage) rawImage.texture = _webCamTexture;
  99. Debug.Log("ZIMWebCamera fps:" + fps + ",size:[" + width + "," + height + "]");
  100. ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  101. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  102. Debug.Log("ZIMWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
  103. }
  104. //catch (System.Exception e)
  105. //{
  106. // Debug.Log("摄像头开启失败 " + deviceName + ", 可能有其他软件占用, " + e.Message);
  107. //}
  108. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  109. /*
  110. Task.Run(() => {
  111. if(_webCamTexture.didUpdateThisFrame)
  112. });/**/
  113. }
  114. else
  115. {
  116. Debug.LogError("开启失败,用户未授予摄像头权限!");
  117. }
  118. }
  119. public void newWebCamTexture(string name)
  120. {
  121. _webCamTexture = new WebCamTexture(name, width, height, fps);
  122. _webCamTexture.Play();
  123. if (rawImage) rawImage.texture = _webCamTexture;
  124. }
  125. public void OnClick_Close()
  126. {
  127. if (_webCamTexture != null)
  128. {
  129. _webCamTexture.Stop();
  130. _webCamTexture = null;
  131. if (rawImage) rawImage.texture = null;
  132. Debug.Log("成功关闭摄像头");
  133. //o0WebCam.Dispose();
  134. //o0WebCam = null;
  135. }
  136. }
  137. public void OnClick_ScreenLocateManual()
  138. {
  139. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  140. InfraredScreenPositioningView _infraredScreenPositioningView = FindObjectOfType<InfraredScreenPositioningView>();
  141. _infraredScreenPositioningView.enterFromZimWebCamera = true;
  142. _infraredScreenPositioningView.transform.SetParent(mParent);
  143. ScreenLocate.Main.ResetScreenIdentification();
  144. }
  145. void AdjustBrightnessContrast(Color32[] pixels, float brightness, float contrast)
  146. {
  147. float contrastFactor = 1.0f + contrast;
  148. for (int i = 0; i < pixels.Length; i++)
  149. {
  150. Color32 pixel = pixels[i];
  151. float r = pixel.r / 255.0f;
  152. float g = pixel.g / 255.0f;
  153. float b = pixel.b / 255.0f;
  154. r = Mathf.Clamp01(((r - 0.5f) * contrastFactor + 0.5f) + brightness);
  155. g = Mathf.Clamp01(((g - 0.5f) * contrastFactor + 0.5f) + brightness);
  156. b = Mathf.Clamp01(((b - 0.5f) * contrastFactor + 0.5f) + brightness);
  157. pixel.r = (byte)(r * 255);
  158. pixel.g = (byte)(g * 255);
  159. pixel.b = (byte)(b * 255);
  160. pixels[i] = pixel;
  161. }
  162. }
  163. private Texture2D adjustedTexture;
  164. private void AdjustTexture()
  165. {
  166. Color32[] pixels = _webCamTexture.GetPixels32();
  167. if (!ScreenLocate.Main.DebugOnZIMDemo)
  168. AdjustBrightnessContrast(pixels, ScreenLocate.Main.pcBrightness, ScreenLocate.Main.pcContrast);
  169. if (adjustedTexture == null || adjustedTexture.width != _webCamTexture.width || adjustedTexture.height != _webCamTexture.height)
  170. {
  171. adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  172. }
  173. adjustedTexture.SetPixels32(pixels);
  174. adjustedTexture.Apply();
  175. }
  176. }