ZIMWebCamera.cs 6.0 KB

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