ZIMWebCamera.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. IEnumerator Start()
  21. {
  22. yield return new WaitForEndOfFrame();
  23. OnClick_Open();
  24. }
  25. void Update()
  26. {
  27. if (_webCamTexture)
  28. {
  29. width = _webCamTexture.width;
  30. height = _webCamTexture.height;
  31. fps = (int)_webCamTexture.requestedFPS;
  32. rawImageSize.texture = rawImage.texture;
  33. //rawImageSize.SetNativeSize();
  34. maintainAspectRatio.AdjustSize();
  35. }
  36. }
  37. public Vector2Int IndexToCoord(int i)
  38. {
  39. var y = i / width;
  40. var x = i % width;
  41. return new Vector2Int(x, y);
  42. }
  43. public int CoordToIndex(int x, int y)
  44. {
  45. return y * width + x;
  46. }
  47. public void OnClick_Open()
  48. {
  49. if (_webCamTexture != null)
  50. {
  51. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  52. return;
  53. }
  54. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  55. {
  56. WebCamDevice[] devices = WebCamTexture.devices;
  57. for (int i = 0; i < devices.Length; i++)
  58. {
  59. Debug.Log("devices[" + i + "].name:" + devices[i].name);
  60. this.logText.text += "devices[" + i + "].name:" + devices[i].name + "\n";
  61. }
  62. if (devices.Length == 0)
  63. {
  64. Debug.LogError("开启失败,没找到可用的摄像头!");
  65. return;
  66. }
  67. if (devices.Length < cameraIndex + 1)
  68. {
  69. Debug.LogError("开启失败,没有对应序号的摄像头!");
  70. return;
  71. }
  72. string deviceName = devices[cameraIndex].name;
  73. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  74. //try
  75. {
  76. _webCamTexture.Play();
  77. if (rawImage) rawImage.texture = _webCamTexture;
  78. Debug.Log("成功开启摄像头 " + deviceName);
  79. ScreenLocate.Main.WebCamIsReady(rawImage.texture);
  80. }
  81. //catch (System.Exception e)
  82. //{
  83. // Debug.Log("摄像头开启失败 " + deviceName + ", 可能有其他软件占用, " + e.Message);
  84. //}
  85. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  86. /*
  87. Task.Run(() => {
  88. if(_webCamTexture.didUpdateThisFrame)
  89. });/**/
  90. }
  91. else
  92. {
  93. Debug.LogError("开启失败,用户未授予摄像头权限!");
  94. }
  95. }
  96. public void newWebCamTexture(string name)
  97. {
  98. _webCamTexture = new WebCamTexture(name, width, height, fps);
  99. _webCamTexture.Play();
  100. if (rawImage) rawImage.texture = _webCamTexture;
  101. }
  102. public void OnClick_Close()
  103. {
  104. if (_webCamTexture != null)
  105. {
  106. _webCamTexture.Stop();
  107. _webCamTexture = null;
  108. if (rawImage) rawImage.texture = null;
  109. Debug.Log("成功关闭摄像头");
  110. //o0WebCam.Dispose();
  111. //o0WebCam = null;
  112. }
  113. }
  114. public void OnClick_ScreenLocateManual()
  115. {
  116. ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
  117. InfraredScreenPositioningView _infraredScreenPositioningView = FindObjectOfType<InfraredScreenPositioningView>();
  118. _infraredScreenPositioningView.enterFromZimWebCamera = true;
  119. _infraredScreenPositioningView.transform.SetParent(mParent);
  120. }
  121. }