| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Color = UnityEngine.Color;
- public class o0WebCamera : MonoBehaviour {
- public int width = 1280;
- public int height = 720;
- public int fps = 60;
- public RawImage rawImage;
- public RawImage rawImage2;
- public RawImage rawImage3;
- public RawImage rawImage4;
- public RawImage rawImage5;
- public RawImage rawImage6;
- private WebCamTexture _webCamTexture;
- public WebCamTexture webCamTexture { get => _webCamTexture; }
- //o0.Project.WebCam o0WebCam = null;
- o0.Project.InfraredIdentification o0InfraredIdentification;
- static public List<RawImage> DebugImage = new List<RawImage>();
- static public void DebugTexture(int index, Texture texture)
- {
- if (index >= DebugImage.Count)
- return;
- Destroy(DebugImage[index].texture);
- DebugImage[index].texture = texture;
- }
- static public void SetScreen(UnityEngine.Color? color = null)
- {
- var canvas = GameObject.Find("WebCameraView").GetComponent<RectTransform>();
- var background = canvas.Find("Background").GetComponent<RectTransform>();
- background.gameObject.SetActive(color != null);
- background.GetComponent<RawImage>().color = color ?? Color.black;
- }
- void Start()
- {
- DebugImage.Add(rawImage);
- DebugImage.Add(rawImage2);
- DebugImage.Add(rawImage3);
- DebugImage.Add(rawImage4);
- DebugImage.Add(rawImage5);
- DebugImage.Add(rawImage6);
- o0InfraredIdentification = new o0.Project.InfraredIdentification(new o0.Geometry2D.Vector<int>(width, height));
- OnClick_Open();
- }
- void Update()
- {
- if (_webCamTexture) {
- width = _webCamTexture.width;
- height = _webCamTexture.height;
- fps = (int)_webCamTexture.requestedFPS;
- if (_webCamTexture.didUpdateThisFrame)
- {
- //Debug.Log(o0InfraredIdentification);
- o0InfraredIdentification.Update(_webCamTexture);
- }
- }
- if (Input.GetKeyDown(KeyCode.Alpha1))
- o0InfraredIdentification.LocateScreen();
- if (Input.GetKeyDown(KeyCode.Alpha2))
- SetScreen(Color.white);
- if (Input.GetKeyDown(KeyCode.Alpha3))
- SetScreen(Color.black);
- if (Input.GetKeyDown(KeyCode.Alpha4))
- SetScreen(null);
- }
- public void OnClick_Open()
- {
- if (_webCamTexture != null)
- {
- Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
- return;
- }
- if (Application.HasUserAuthorization(UserAuthorization.WebCam))
- {
- WebCamDevice[] devices = WebCamTexture.devices;
- if (devices.Length == 0)
- {
- Debug.LogError("开启失败,没找到可用的摄像头!");
- return;
- }
- string deviceName = devices[0].name;
- _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
- _webCamTexture.Play();
- if (rawImage) rawImage.texture = _webCamTexture;
- Debug.Log("成功开启摄像头 " + deviceName);
- //o0WebCam = new o0.Project.WebCam(_webCamTexture);
- /*
- Task.Run(() => {
- if(_webCamTexture.didUpdateThisFrame)
- });/**/
- }
- else
- {
- Debug.LogError("开启失败,用户未授予摄像头权限!");
- }
- }
- public void OnClick_Close()
- {
- if (_webCamTexture != null)
- {
- _webCamTexture.Stop();
- _webCamTexture = null;
- if (rawImage) rawImage.texture = null;
- Debug.Log("成功关闭摄像头");
- //o0WebCam.Dispose();
- //o0WebCam = null;
- }
- }
- }
|