o0WebCamera.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if (index >= DebugImage.Count)
  29. return;
  30. Destroy(DebugImage[index].texture);
  31. DebugImage[index].texture = texture;
  32. }
  33. static public void SetScreen(UnityEngine.Color? color = null)
  34. {
  35. var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
  36. var background = canvas.Find("Background").GetComponent<RectTransform>();
  37. background.gameObject.SetActive(color != null);
  38. background.GetComponent<RawImage>().color = color ?? Color.black;
  39. }
  40. void Start()
  41. {
  42. DebugImage.Add(rawImage);
  43. DebugImage.Add(rawImage2);
  44. DebugImage.Add(rawImage3);
  45. DebugImage.Add(rawImage4);
  46. DebugImage.Add(rawImage5);
  47. DebugImage.Add(rawImage6);
  48. o0InfraredIdentification = new o0.Project.InfraredIdentification(new o0.Geometry2D.Vector<int>(width, height));
  49. OnClick_Open();
  50. }
  51. void Update()
  52. {
  53. if (_webCamTexture) {
  54. width = _webCamTexture.width;
  55. height = _webCamTexture.height;
  56. fps = (int)_webCamTexture.requestedFPS;
  57. if (_webCamTexture.didUpdateThisFrame)
  58. {
  59. //Debug.Log(o0InfraredIdentification);
  60. o0InfraredIdentification.Update(_webCamTexture);
  61. }
  62. }
  63. if (Input.GetKeyDown(KeyCode.Alpha1))
  64. o0InfraredIdentification.LocateScreen();
  65. if (Input.GetKeyDown(KeyCode.Alpha2))
  66. SetScreen(Color.white);
  67. if (Input.GetKeyDown(KeyCode.Alpha3))
  68. SetScreen(Color.black);
  69. if (Input.GetKeyDown(KeyCode.Alpha4))
  70. SetScreen(null);
  71. }
  72. public void OnClick_Open()
  73. {
  74. if (_webCamTexture != null)
  75. {
  76. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  77. return;
  78. }
  79. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  80. {
  81. WebCamDevice[] devices = WebCamTexture.devices;
  82. if (devices.Length == 0)
  83. {
  84. Debug.LogError("开启失败,没找到可用的摄像头!");
  85. return;
  86. }
  87. string deviceName = devices[0].name;
  88. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  89. _webCamTexture.Play();
  90. if (rawImage) rawImage.texture = _webCamTexture;
  91. Debug.Log("成功开启摄像头 " + deviceName);
  92. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  93. /*
  94. Task.Run(() => {
  95. if(_webCamTexture.didUpdateThisFrame)
  96. });/**/
  97. }
  98. else
  99. {
  100. Debug.LogError("开启失败,用户未授予摄像头权限!");
  101. }
  102. }
  103. public void OnClick_Close()
  104. {
  105. if (_webCamTexture != null)
  106. {
  107. _webCamTexture.Stop();
  108. _webCamTexture = null;
  109. if (rawImage) rawImage.texture = null;
  110. Debug.Log("成功关闭摄像头");
  111. //o0WebCam.Dispose();
  112. //o0WebCam = null;
  113. }
  114. }
  115. }