112 lines
3.5 KiB
C#
112 lines
3.5 KiB
C#
using OpenCVForUnity.CoreModule;
|
|
using OpenCVForUnity.UnityUtils;
|
|
using Serenegiant.UVC;
|
|
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
|
|
namespace Yoga
|
|
{
|
|
public class MotionUSBCameraCaptureManager : CaptureManagerBase, IUVCDrawer
|
|
{
|
|
protected override Mat GetMat()
|
|
{
|
|
Mat img = null;
|
|
|
|
if (UVCManager.Instance != null &&
|
|
UVCManager.Instance.GetAttachedDevices() != null && UVCManager.Instance.GetAttachedDevices().Count > 0)
|
|
{
|
|
var devices = UVCManager.Instance.GetAttachedDevices();
|
|
for (int i = 0; i < devices.Count; i++)
|
|
{
|
|
UVCManager.CameraInfo device = devices[i];
|
|
LogPrint.Log($"id:{i},pid:{device.Pid},vid:{device.Vid},deviceName:{device.DeviceName}");
|
|
}
|
|
Texture picCaptured = null;
|
|
var index = PlayerPrefs.GetInt("USBCameraIndex");
|
|
if (index >= devices.Count) //数量超出时候改为默认第一个
|
|
index = 0;
|
|
|
|
picCaptured = devices[index].previewTexture;
|
|
if (picCaptured == null)
|
|
throw new Exception("No preview Texture found!");
|
|
|
|
Texture2D tmpTex = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight);
|
|
Utils.textureToTexture2D(picCaptured, tmpTex);
|
|
tmpTex = PictureUtility.HorizontalFlipTexture(tmpTex);//picCaptured texture水平反转
|
|
img = new Mat(UVCManager.Instance.DefaultHeight, UVCManager.Instance.DefaultWidth, CvType.CV_8UC3);
|
|
Utils.texture2DToMat(tmpTex, img);
|
|
}
|
|
|
|
return img;
|
|
}
|
|
|
|
protected override void MatInitial()
|
|
{
|
|
if (UVCManager.Instance == null)
|
|
throw new Exception("没有连接USB摄像头");
|
|
|
|
Debug.LogWarning("USB Camera!");
|
|
}
|
|
|
|
#region UVC
|
|
public UVCFilter[] UVCFilters;
|
|
private Texture SavedTexture;
|
|
public bool OnUVCAttachEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
var result = !device.IsRicoh || device.IsTHETA;
|
|
|
|
result &= UVCFilter.Match(device, UVCFilters);
|
|
|
|
return result;
|
|
}
|
|
|
|
public void OnUVCDetachEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
LogPrint.Log("OnUVCDetachEvent " + device);
|
|
}
|
|
|
|
public bool CanDraw(UVCManager manager, UVCDevice device)
|
|
{
|
|
return UVCFilter.Match(device, UVCFilters);
|
|
}
|
|
|
|
public void OnUVCStartEvent(UVCManager manager, UVCDevice device, Texture tex)
|
|
{
|
|
HandleOnStartPreview(tex);
|
|
}
|
|
|
|
public void OnUVCStopEvent(UVCManager manager, UVCDevice device)
|
|
{
|
|
HandleOnStopPreview();
|
|
}
|
|
|
|
private void HandleOnStartPreview(Texture tex)
|
|
{
|
|
SavedTexture = TargetDisplayer.texture;
|
|
_displayTexture = new Texture2D(UVCManager.Instance.DefaultWidth, UVCManager.Instance.DefaultHeight, TextureFormat.RGB24, false);
|
|
TargetDisplayer.texture = _displayTexture;
|
|
}
|
|
private void HandleOnStopPreview()
|
|
{
|
|
RestoreTexture();
|
|
}
|
|
|
|
private void RestoreTexture()
|
|
{
|
|
try
|
|
{
|
|
transform.GetComponent<Renderer>().material.mainTexture = SavedTexture;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogException(e);
|
|
}
|
|
SavedTexture = null;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|