ZIMWebCamera.cs 6.4 KB

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