ZIMWebCamera.cs 4.2 KB

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