ZIMWebCamera.cs 7.2 KB

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