Health/Assets/Scripts/UI/Component/MotionWebCaptureManager.cs

124 lines
4.4 KiB
C#

using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using UnityEngine;
namespace Yoga
{
public class MotionWebCaptureManager : CaptureManagerBase
{
private WebCamTextureToMatHelper _webCamTextureToMatHelper;
public WebCamTextureToMatHelper WebCamTextureToMatHelper => _webCamTextureToMatHelper;
protected override void EventRegist()
{
base.EventRegist();
EventManager.Instance.AddEventListener(YogaEventType.ChangeCaptureCameraDevice, ChangeCaptureCameraDevice);
}
protected override void EventRemove()
{
base.EventRemove();
EventManager.Instance.RemoveEventListener(YogaEventType.ChangeCaptureCameraDevice, ChangeCaptureCameraDevice);
}
protected override void MatInitial()
{
if (_webCamTextureToMatHelper == null)
{
_webCamTextureToMatHelper = transform.GetComponent<WebCamTextureToMatHelper>();
}
#if UNITY_ANDROID && !UNITY_EDITOR
// Avoids the front camera low light issue that occurs in only some Android devices (e.g. Google Pixel, Pixel2).
_webCamTextureToMatHelper.avoidAndroidFrontCameraLowLightIssue = true;
#endif
_webCamTextureToMatHelper.Initialize();
}
private void ChangeCaptureCameraDevice()
{
var deviceName = WebCamTexture.devices[GlobalData.Instance.CameraIndex].name;
var width = _webCamTextureToMatHelper.GetWidth();
var height = _webCamTextureToMatHelper.GetHeight();
var fps = _webCamTextureToMatHelper.GetFPS();
if (fps > 0)
{
_webCamTextureToMatHelper.Initialize(deviceName, width, height, requestedFPS: fps);
}
else
{
_webCamTextureToMatHelper.Initialize(deviceName, width, height);
}
}
protected override Mat GetMat()
{
Mat img = null;
if (_webCamTextureToMatHelper.IsPlaying() && _webCamTextureToMatHelper.DidUpdateThisFrame())
{
img = _webCamTextureToMatHelper.GetMat();
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB);
}
return img;
}
protected override void OnRelease()
{
base.OnRelease();
_webCamTextureToMatHelper.Dispose();
}
public void OnWebCamTextureToMatHelperInitialized()
{
LogPrint.Log("OnWebCamTextureToMatHelperInitialized");
Mat webCamTextureMat = _webCamTextureToMatHelper.GetMat();
_displayTexture = new Texture2D(webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGB24, false);
Utils.matToTexture2D(webCamTextureMat, _displayTexture);
TargetDisplayer.texture = _displayTexture;
gameObject.transform.localScale = new Vector3(webCamTextureMat.cols() / 10, webCamTextureMat.rows() / 10, 1);
LogPrint.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = webCamTextureMat.width();
float height = webCamTextureMat.height();
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale)
{
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
}
else
{
Camera.main.orthographicSize = height / 2;
}
_bgrMat = new Mat(webCamTextureMat.rows(), webCamTextureMat.cols(), CvType.CV_8UC3);
}
public void OnWebCamTextureToMatHelperDisposed()
{
LogPrint.Log("OnWebCamTextureToMatHelperDisposed");
if (_bgrMat != null)
_bgrMat.Dispose();
if (_displayTexture != null)
{
Texture2D.Destroy(_displayTexture);
_displayTexture = null;
}
}
public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode)
{
LogPrint.Log("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
}
}
}