o0WebCamera.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using Color = UnityEngine.Color;
  5. public class o0WebCamera : MonoBehaviour {
  6. public int width = 1280;
  7. public int height = 720;
  8. public int fps = 60;
  9. public RawImage rawImage;
  10. public RawImage rawImage2;
  11. public RawImage rawImage3;
  12. public RawImage rawImage4;
  13. public RawImage rawImage5;
  14. public RawImage rawImage6;
  15. private WebCamTexture _webCamTexture;
  16. public WebCamTexture webCamTexture { get => _webCamTexture; }
  17. //o0.Project.WebCam o0WebCam = null;
  18. o0.Project.InfraredIdentification o0InfraredIdentification;
  19. static public List<RawImage> DebugImage = new List<RawImage>();
  20. static public void DebugTexture(int index, Texture texture)
  21. {
  22. if (index >= DebugImage.Count)
  23. return;
  24. Destroy(DebugImage[index].texture);
  25. DebugImage[index].texture = texture;
  26. }
  27. static public void SetScreen(UnityEngine.Color? color = null)
  28. {
  29. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  30. var background = canvas.Find("Background").GetComponent<RectTransform>();
  31. background.gameObject.SetActive(color != null);
  32. background.GetComponent<RawImage>().color = color ?? Color.black;
  33. }
  34. void Start()
  35. {
  36. DebugImage.Add(rawImage);
  37. DebugImage.Add(rawImage2);
  38. DebugImage.Add(rawImage3);
  39. DebugImage.Add(rawImage4);
  40. DebugImage.Add(rawImage5);
  41. DebugImage.Add(rawImage6);
  42. o0InfraredIdentification = new o0.Project.InfraredIdentification(new o0.Geometry2D.Vector<int>(width, height));
  43. OnClick_Open();
  44. }
  45. void Update()
  46. {
  47. if (_webCamTexture) {
  48. width = _webCamTexture.width;
  49. height = _webCamTexture.height;
  50. fps = (int)_webCamTexture.requestedFPS;
  51. if (_webCamTexture.didUpdateThisFrame)
  52. {
  53. //Debug.Log(o0InfraredIdentification);
  54. o0InfraredIdentification.Update(_webCamTexture);
  55. }
  56. }
  57. if (Input.GetKeyDown(KeyCode.Alpha1))
  58. o0InfraredIdentification.LocateScreen();
  59. if (Input.GetKeyDown(KeyCode.Alpha2))
  60. SetScreen(Color.white);
  61. if (Input.GetKeyDown(KeyCode.Alpha3))
  62. SetScreen(Color.black);
  63. if (Input.GetKeyDown(KeyCode.Alpha4))
  64. SetScreen(null);
  65. }
  66. public void OnClick_Open()
  67. {
  68. if (_webCamTexture != null)
  69. {
  70. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  71. return;
  72. }
  73. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  74. {
  75. WebCamDevice[] devices = WebCamTexture.devices;
  76. if (devices.Length == 0)
  77. {
  78. Debug.LogError("开启失败,没找到可用的摄像头!");
  79. return;
  80. }
  81. string deviceName = devices[0].name;
  82. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  83. _webCamTexture.Play();
  84. if (rawImage) rawImage.texture = _webCamTexture;
  85. Debug.Log("成功开启摄像头 " + deviceName);
  86. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  87. /*
  88. Task.Run(() => {
  89. if(_webCamTexture.didUpdateThisFrame)
  90. });/**/
  91. }
  92. else
  93. {
  94. Debug.LogError("开启失败,用户未授予摄像头权限!");
  95. }
  96. }
  97. public void OnClick_Close()
  98. {
  99. if (_webCamTexture != null)
  100. {
  101. _webCamTexture.Stop();
  102. _webCamTexture = null;
  103. if (rawImage) rawImage.texture = null;
  104. Debug.Log("成功关闭摄像头");
  105. //o0WebCam.Dispose();
  106. //o0WebCam = null;
  107. }
  108. }
  109. }