ZIMWebCamera.cs 3.9 KB

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