| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- // using AndroidJavaClass = UnityEngine.AndroidJNI.AndroidJavaClass;
- // using AndroidJavaObject = UnityEngine.AndroidJNI.AndroidJavaObject;
- public class PcWebCamera : MonoBehaviour
- {
- public int cameraIndex = 0;
- public int width = 320;
- public int height = 240;
- public int fps = 30;
- public string deviceName;
- private WebCamTexture _webCamTexture;
- public WebCamTexture webCamTexture { get => _webCamTexture; set => _webCamTexture = value; }
- public Vector2 Size => new Vector2(width, height);
- public float brightness = 0.0f; // 亮度调整值,范围 -1.0 到 1.0
- public float contrast = 1.0f; // 对比度调整值,范围 0.0 到 2.0
- private void Awake()
- {
- SceneManager.sceneLoaded += OnSceneLoaded;
- }
- private IEnumerator Start()
- {
- yield return new WaitForEndOfFrame();
- OnClick_Open();
- }
- void OnSceneLoaded(Scene scene, LoadSceneMode mode)
- {
- Debug.Log("Scene Loaded: " + scene.name);
- Debug.Log("Load Scene Mode: " + mode);
- // 在场景加载时执行的代码
- if (BluetoothAim.ins && (BluetoothAim.ins.isMainConnectToHOUYIPRO() || BluetoothAim.ins.isMainConnectToGun()))
- {
- StartCoroutine(RestartWebCam());
- }
- else if (scene.name == "Home") {
- //没连接设备时候,如果跳回home页面,设置一次
- StartCoroutine(RestartWebCam());
- }
- }
- IEnumerator RestartWebCam()
- {
- yield return new WaitForSecondsRealtime(1); // 延迟 x 秒重启 WebCamTexture
- if (_webCamTexture != null)
- {
- _webCamTexture.Stop();
- _webCamTexture.Play();
- }
- }
- private Color32[] pixels;
- private Texture2D adjustedTexture;
- private int frameCounter = 0;
- public int updateInterval = 5; // 每隔多少帧更新一次图像
- private void Update()
- {
- if (_webCamTexture)
- {
- width = _webCamTexture.width;
- height = _webCamTexture.height;
- fps = (int)_webCamTexture.requestedFPS;
- frameCounter++;
- //&& frameCounter % updateInterval == 0
- //if (_webCamTexture.didUpdateThisFrame )
- //{
- // // 获取 WebCamTexture 的像素数据
- // pixels = _webCamTexture.GetPixels32();
- // // 调整亮度和对比度
- // for (int i = 0; i < pixels.Length; i++)
- // {
- // pixels[i] = AdjustBrightnessAndContrast(pixels[i], brightness, contrast);
- // }
- // // 将调整后的像素数据应用到 Texture2D
- // adjustedTexture.SetPixels32(pixels);
- // adjustedTexture.Apply();
- // // 重置 frameCounter 以防溢出
- // if (frameCounter >= int.MaxValue - 1)
- // {
- // frameCounter = 0;
- // }
- //}
- }
- }
- public Vector2Int IndexToCoord(int i)
- {
- var y = i / width;
- var x = i % width;
- return new Vector2Int(x, y);
- }
- public int CoordToIndex(int x, int y)
- {
- return y * width + x;
- }
- public void OnClick_Open()
- {
- if (_webCamTexture != null)
- {
- Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
- return;
- }
- if (Application.HasUserAuthorization(UserAuthorization.WebCam))
- {
- WebCamDevice[] devices = WebCamTexture.devices;
- for (int i = 0; i < devices.Length; i++)
- {
- Debug.Log("devices[" + i + "].name:" + devices[i].name);
- }
- if (devices.Length == 0)
- {
- Debug.LogError("开启失败,没找到可用的摄像头!");
- return;
- }
- if (devices.Length < cameraIndex + 1)
- {
- Debug.LogError("开启失败,没有对应序号的摄像头!");
- return;
- }
- deviceName = devices[cameraIndex].name;
- _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
- _webCamTexture.Play();
- // 创建一个 Texture2D 用于存储调整后的图像
- //adjustedTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
- Debug.Log("成功开启摄像头 " + deviceName);
- ScreenLocate.Main.WebCamIsReady(_webCamTexture);
- // StartCoroutine(getResolutions(devices[cameraIndex]));
- }
- else
- {
- Debug.LogError("开启失败,用户未授予摄像头权限!");
- }
- }
- IEnumerator getResolutions(WebCamDevice device) {
- yield return new WaitForSeconds(1.0f);
- // 打印支持的分辨率
- Resolution[] resolutions = device.availableResolutions;
- if (resolutions != null && resolutions.Length > 0)
- {
- foreach (Resolution res in resolutions)
- {
- Debug.Log("支持的分辨率: " + res.width + "x" + res.height);
- }
- }
- else
- {
- Debug.Log("没有可用的分辨率信息。");
- }
- }
- public void newWebCamTexture(string name)
- {
- _webCamTexture = new WebCamTexture(name, width, height, fps);
- _webCamTexture.Play();
- //Debug.Log("[newWebCamTexture]成功开启摄像头");
- }
- public void OnClick_Close()
- {
- if (_webCamTexture != null)
- {
- _webCamTexture.Stop();
- _webCamTexture = null;
- Debug.Log("[OnClick_Close]成功关闭摄像头");
- }
- }
- public WebCamTexture newWebCamTexture(int width, int height)
- {
- _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
- _webCamTexture.Play();
- Debug.Log("[newWebCamTexture]成功开启摄像头");
- return _webCamTexture;
- }
- //修改亮度和对比度
- Color32 AdjustBrightnessAndContrast(Color32 color, float brightness, float contrast)
- {
- float r = color.r / 255.0f;
- float g = color.g / 255.0f;
- float b = color.b / 255.0f;
- // 调整对比度
- r = ((r - 0.5f) * contrast + 0.5f);
- g = ((g - 0.5f) * contrast + 0.5f);
- b = ((b - 0.5f) * contrast + 0.5f);
- // 调整亮度
- r += brightness;
- g += brightness;
- b += brightness;
- // 限制值在 0 到 1 之间
- r = Mathf.Clamp01(r);
- g = Mathf.Clamp01(g);
- b = Mathf.Clamp01(b);
- return new Color32((byte)(r * 255), (byte)(g * 255), (byte)(b * 255), color.a);
- }
- }
|