ZIMWebCamera.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. // using AndroidJavaClass = UnityEngine.AndroidJNI.AndroidJavaClass;
  5. // using AndroidJavaObject = UnityEngine.AndroidJNI.AndroidJavaObject;
  6. public class ZIMWebCamera : MonoBehaviour {
  7. public int cameraIndex = 1;
  8. public int width = 1280;
  9. public int height = 720;
  10. public int fps = 60;
  11. public RawImage rawImage;
  12. private WebCamTexture _webCamTexture;
  13. public WebCamTexture webCamTexture { get => _webCamTexture; }
  14. public Vector2 Size => new Vector2(width, height);
  15. public Text logText;
  16. IEnumerator Start()
  17. {
  18. yield return new WaitForEndOfFrame();
  19. OnClick_Open();
  20. }
  21. void Update()
  22. {
  23. if (_webCamTexture) {
  24. width = _webCamTexture.width;
  25. height = _webCamTexture.height;
  26. fps = (int)_webCamTexture.requestedFPS;
  27. }
  28. }
  29. public Vector2Int IndexToCoord(int i)
  30. {
  31. var y = i / width;
  32. var x = i % width;
  33. return new Vector2Int(x, y);
  34. }
  35. public int CoordToIndex(int x, int y)
  36. {
  37. return y * width + x;
  38. }
  39. public void OnClick_Open()
  40. {
  41. if (_webCamTexture != null)
  42. {
  43. Debug.LogError("开启失败,请先关闭正在使用的摄像头!");
  44. return;
  45. }
  46. if (Application.HasUserAuthorization(UserAuthorization.WebCam))
  47. {
  48. WebCamDevice[] devices = WebCamTexture.devices;
  49. for (int i = 0; i < devices.Length; i++)
  50. {
  51. Debug.Log("devices["+i+"].name:"+devices[i].name);
  52. this.logText.text += "devices["+i+"].name:"+devices[i].name + "\n";
  53. }
  54. if (devices.Length == 0)
  55. {
  56. Debug.LogError("开启失败,没找到可用的摄像头!");
  57. return;
  58. }
  59. if (devices.Length < cameraIndex + 1)
  60. {
  61. Debug.LogError("开启失败,没有对应序号的摄像头!");
  62. return;
  63. }
  64. string deviceName = devices[cameraIndex].name;
  65. _webCamTexture = new WebCamTexture(deviceName, width, height, fps);
  66. //try
  67. {
  68. _webCamTexture.Play();
  69. if (rawImage) rawImage.texture = _webCamTexture;
  70. Debug.Log("成功开启摄像头 " + deviceName);
  71. ScreenLocate.Main.WebCamIsReady(rawImage.texture);
  72. }
  73. //catch (System.Exception e)
  74. //{
  75. // Debug.Log("摄像头开启失败 " + deviceName + ", 可能有其他软件占用, " + e.Message);
  76. //}
  77. //o0WebCam = new o0.Project.WebCam(_webCamTexture);
  78. /*
  79. Task.Run(() => {
  80. if(_webCamTexture.didUpdateThisFrame)
  81. });/**/
  82. }
  83. else
  84. {
  85. Debug.LogError("开启失败,用户未授予摄像头权限!");
  86. }
  87. }
  88. public void newWebCamTexture(string name)
  89. {
  90. _webCamTexture = new WebCamTexture(name, width, height, fps);
  91. _webCamTexture.Play();
  92. if (rawImage) rawImage.texture = _webCamTexture;
  93. }
  94. public void OnClick_Close()
  95. {
  96. if (_webCamTexture != null)
  97. {
  98. _webCamTexture.Stop();
  99. _webCamTexture = null;
  100. if (rawImage) rawImage.texture = null;
  101. Debug.Log("成功关闭摄像头");
  102. //o0WebCam.Dispose();
  103. //o0WebCam = null;
  104. }
  105. }
  106. }