Ver Fonte

改成列表选择camera

ZIM há 1 ano atrás
pai
commit
faa3e9b4bd

+ 67 - 48
Assets/InfraredProject/WebCamera/Script/ZIM/ZIMUnity/ZIMWebCamera.cs

@@ -8,7 +8,7 @@ using UnityEngine.UI;
 public class ZIMWebCamera : MonoBehaviour
 public class ZIMWebCamera : MonoBehaviour
 {
 {
     int cameraIndex = 0;
     int cameraIndex = 0;
-    [SerializeField] public InputField inputIndex;
+    [SerializeField] public Dropdown dropdownIndex;
 
 
     public int width = 1280;
     public int width = 1280;
     public int height = 720;
     public int height = 720;
@@ -24,10 +24,23 @@ public class ZIMWebCamera : MonoBehaviour
 
 
     public Transform mParent;
     public Transform mParent;
 
 
+    private WebCamDevice[] webCamDevices;
+
+    private void GetDevices()
+    {
+        webCamDevices = WebCamTexture.devices;
+
+        var camIndexList = new System.Collections.Generic.List<Dropdown.OptionData>();
+        for (int i = 0; i < webCamDevices.Length; i++)
+            camIndexList.Add(new Dropdown.OptionData("Cam " + (i + 1)));
+
+        dropdownIndex.options = camIndexList;
+    }
 
 
     private IEnumerator Start()
     private IEnumerator Start()
     {
     {
         yield return new WaitForEndOfFrame();
         yield return new WaitForEndOfFrame();
+        GetDevices();
         OnClick_Open();
         OnClick_Open();
     }
     }
 
 
@@ -77,48 +90,76 @@ public class ZIMWebCamera : MonoBehaviour
 
 
     public void OnClick_Open()
     public void OnClick_Open()
     {
     {
-        if (!int.TryParse(inputIndex.text, out cameraIndex))
-        {
-            cameraIndex = 0;
-            inputIndex.text = "0";
-        }
+        cameraIndex = dropdownIndex.value;
 
 
         if (_webCamTexture != null)
         if (_webCamTexture != null)
         {
         {
-            Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
-            return;
+            //Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
+            CloseWebCamera();
         }
         }
+        StartCoroutine(OpenWebCamera());
+    }
+
+
+    public void newWebCamTexture(string name)
+    {
+        _webCamTexture = new WebCamTexture(name, width, height, fps);
+        _webCamTexture.Play();
+        if (rawImage) rawImage.texture = _webCamTexture;
+
+    }
+
+    public void OnClick_Close()
+    {
+        if (_webCamTexture != null)
+            CloseWebCamera();
+
+        GetDevices();
+    }
+
+    public void OnClick_ScreenLocateManual()
+    {
+        ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
+        InfraredScreenPositioningView _infraredScreenPositioningView = FindObjectOfType<InfraredScreenPositioningView>();
+        _infraredScreenPositioningView.enterFromZimWebCamera = true;
+        _infraredScreenPositioningView.transform.SetParent(mParent);
+        ScreenLocate.Main.ResetScreenIdentification();
+    }
+
+    // 打开默认去下一帧执行
+    private IEnumerator OpenWebCamera()
+    {
+        yield return null;
         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
         {
         {
-            WebCamDevice[] devices = WebCamTexture.devices;
-            for (int i = 0; i < devices.Length; i++)
+
+            for (int i = 0; i < webCamDevices.Length; i++)
             {
             {
-                Debug.Log("devices[" + i + "].name:" + devices[i].name);
-                this.logText.text += "devices[" + i + "].name:" + devices[i].name + "\n";
+                Debug.Log("devices[" + i + "].name:" + webCamDevices[i].name);
+                this.logText.text += "devices[" + i + "].name:" + webCamDevices[i].name + "\n";
             }
             }
-            if (devices.Length == 0)
+            if (webCamDevices.Length == 0)
             {
             {
                 Debug.LogError("开启失败,没找到可用的摄像头!");
                 Debug.LogError("开启失败,没找到可用的摄像头!");
-                return;
+                yield break;
             }
             }
-            if (devices.Length < cameraIndex + 1)
+            if (webCamDevices.Length < cameraIndex + 1)
             {
             {
                 Debug.Log($"<color=yellow>开启失败,没有对应序号的摄像头{cameraIndex}! 尝试启动默认摄像头0</color>");
                 Debug.Log($"<color=yellow>开启失败,没有对应序号的摄像头{cameraIndex}! 尝试启动默认摄像头0</color>");
                 cameraIndex = 0;
                 cameraIndex = 0;
-                inputIndex.text = "0";
                 //return;
                 //return;
             }
             }
-            string deviceName = devices[cameraIndex].name;
+            string deviceName = webCamDevices[cameraIndex].name;
             _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
             _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
 
 
             //try
             //try
             {
             {
                 _webCamTexture.Play();
                 _webCamTexture.Play();
-               // if (rawImage) rawImage.texture = _webCamTexture;
+                // if (rawImage) rawImage.texture = _webCamTexture;
                 Debug.Log("ZIMWebCamera fps:" + fps + ",size:[" + width + "," + height + "]");
                 Debug.Log("ZIMWebCamera fps:" + fps + ",size:[" + width + "," + height + "]");
                 ScreenLocate.Main.WebCamIsReady(_webCamTexture);
                 ScreenLocate.Main.WebCamIsReady(_webCamTexture);
 
 
-               // rawImage.texture = ScreenLocate.Main.getUVCTexture;
+                // rawImage.texture = ScreenLocate.Main.getUVCTexture;
 
 
                 Debug.Log("ZIMWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
                 Debug.Log("ZIMWebCamera成功开启摄像头name:" + deviceName + ",size:[" + _webCamTexture.width + "," + _webCamTexture.height + "]");
             }
             }
@@ -139,38 +180,16 @@ public class ZIMWebCamera : MonoBehaviour
         }
         }
     }
     }
 
 
-
-    public void newWebCamTexture(string name)
-    {
-        _webCamTexture = new WebCamTexture(name, width, height, fps);
-        _webCamTexture.Play();
-        if (rawImage) rawImage.texture = _webCamTexture;
-
-    }
-
-    public void OnClick_Close()
+    private void CloseWebCamera()
     {
     {
-        if (_webCamTexture != null)
-        {
-            _webCamTexture.Stop();
-            _webCamTexture = null;
-            if (rawImage) rawImage.texture = null;
-            Debug.Log("成功关闭摄像头");
-            //o0WebCam.Dispose();
-            //o0WebCam = null;
-        }
-    }
-
-    public void OnClick_ScreenLocateManual()
-    {
-        ViewManager2.ShowView(ViewManager2.Path_InfraredScreenPositioningView);
-        InfraredScreenPositioningView _infraredScreenPositioningView = FindObjectOfType<InfraredScreenPositioningView>();
-        _infraredScreenPositioningView.enterFromZimWebCamera = true;
-        _infraredScreenPositioningView.transform.SetParent(mParent);
-        ScreenLocate.Main.ResetScreenIdentification();
+        _webCamTexture.Stop();
+        _webCamTexture = null;
+        if (rawImage) rawImage.texture = null;
+        Debug.Log("成功关闭摄像头");
+        //o0WebCam.Dispose();
+        //o0WebCam = null;
     }
     }
 
 
-
     void AdjustBrightnessContrast(Color32[] pixels, float brightness, float contrast)
     void AdjustBrightnessContrast(Color32[] pixels, float brightness, float contrast)
     {
     {
         float contrastFactor = 1.0f + contrast;
         float contrastFactor = 1.0f + contrast;

Diff do ficheiro suprimidas por serem muito extensas
+ 798 - 134
Assets/InfraredProject/WebCamera/zimWebCamera.unity


Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff