o0WebCamera.cs 3.9 KB

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