ZIMWebCamera.cs 4.1 KB

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