ZIMWebCamera.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 OnClick_Open()
  54. {
  55. if (_webCamTexture != null)
  56. {
  57. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  58. return;
  59. }
  60. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  61. {
  62. WebCamDevice[] devices = WebCamTexture.devices;
  63. for (int i = 0; i < devices.Length; i++)
  64. {
  65. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  66. this.logText.text += "devices[" + i + "].name:" + devices[i].name + "\n";
  67. }
  68. if (devices.Length == 0)
  69. {
  70. Debug.LogError("开启失败,没找到可用的摄像头!");
  71. return;
  72. }
  73. if (devices.Length < cameraIndex + 1)
  74. {
  75. Debug.Log("<color=yellow>开启失败,没有对应序号的摄像头! 尝试启动默认摄像头</color>");
  76. cameraIndex = 0;
  77. //return;
  78. }
  79. string deviceName = devices[cameraIndex].name;
  80. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  81. //try
  82. {
  83. _webCamTexture.Play();
  84. // if (rawImage) rawImage.texture = _webCamTexture;
  85. Debug.Log("ZIMWebCamera fps:" + fps + ",size:[" + width + "," + height + "]");
  86. ScreenLocate.Main.WebCamIsReady(_webCamTexture);
  87. // rawImage.texture = ScreenLocate.Main.getUVCTexture;
  88. Debug.Log("ZIMWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
  89. }
  90. //catch (System.Exception e)
  91. //{
  92. // Debug.Log("摄像头开启失败 " + deviceName + ", 可能有其他软件占用, " + e.Message);
  93. //}
  94. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  95. /*
  96. Task.Run(() => {
  97. if(_webCamTexture.didUpdateThisFrame)
  98. });/**/
  99. }
  100. else
  101. {
  102. Debug.LogError("开启失败,用户未授予摄像头权限!");
  103. }
  104. }
  105. public void newWebCamTexture(string name)
  106. {
  107. _webCamTexture = new WebCamTexture(name, width, height, fps);
  108. _webCamTexture.Play();
  109. if (rawImage) rawImage.texture = _webCamTexture;
  110. }
  111. public void OnClick_Close()
  112. {
  113. if (_webCamTexture != null)
  114. {
  115. _webCamTexture.Stop();
  116. _webCamTexture = null;
  117. if (rawImage) rawImage.texture = null;
  118. Debug.Log("成功关闭摄像头");
  119. //o0WebCam.Dispose();
  120. //o0WebCam = null;
  121. }
  122. }
  123. public void OnClick_ScreenLocateManual()
  124. {
  125. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  126. InfraredScreenPositioningView _infraredScreenPositioningView = FindObjectOfType<InfraredScreenPositioningView>();
  127. _infraredScreenPositioningView.enterFromZimWebCamera = true;
  128. _infraredScreenPositioningView.transform.SetParent(mParent);
  129. ScreenLocate.Main.ResetScreenIdentification();
  130. }
  131. void AdjustBrightnessContrast(Color32[] pixels, float brightness, float contrast)
  132. {
  133. float contrastFactor = 1.0f + contrast;
  134. for (int i = 0; i < pixels.Length; i++)
  135. {
  136. Color32 pixel = pixels[i];
  137. float r = pixel.r / 255.0f;
  138. float g = pixel.g / 255.0f;
  139. float b = pixel.b / 255.0f;
  140. r = Mathf.Clamp01(((r - 0.5f) * contrastFactor + 0.5f) + brightness);
  141. g = Mathf.Clamp01(((g - 0.5f) * contrastFactor + 0.5f) + brightness);
  142. b = Mathf.Clamp01(((b - 0.5f) * contrastFactor + 0.5f) + brightness);
  143. pixel.r = (byte)(r * 255);
  144. pixel.g = (byte)(g * 255);
  145. pixel.b = (byte)(b * 255);
  146. pixels[i] = pixel;
  147. }
  148. }
  149. private Texture2D adjustedTexture;
  150. private void AdjustTexture()
  151. {
  152. Color32[] pixels = _webCamTexture.GetPixels32();
  153. if (!ScreenLocate.Main.DebugOnZIMDemo)
  154. AdjustBrightnessContrast(pixels, ScreenLocate.Main.pcBrightness, ScreenLocate.Main.pcContrast);
  155. if (adjustedTexture == null || adjustedTexture.width != _webCamTexture.width || adjustedTexture.height != _webCamTexture.height)
  156. {
  157. adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  158. }
  159. adjustedTexture.SetPixels32(pixels);
  160. adjustedTexture.Apply();
  161. }
  162. }