|
|
@@ -1,14 +1,6 @@
|
|
|
using UnityEngine;
|
|
|
using System;
|
|
|
-using System.Collections;
|
|
|
using System.Runtime.InteropServices;
|
|
|
-using SLAMUVC;
|
|
|
-using UnityEngine.UI;
|
|
|
-using System.Collections.Generic;
|
|
|
-using AOT;
|
|
|
-using System.Threading;
|
|
|
-using UnityEngine.Events;
|
|
|
-using UnityEngine.Android;
|
|
|
|
|
|
namespace SLAMUVC
|
|
|
{
|
|
|
@@ -49,8 +41,8 @@ namespace SLAMUVC
|
|
|
public enum CameraMirror
|
|
|
{
|
|
|
NONE,
|
|
|
- HORIZONTAL,//水平翻转
|
|
|
- VERTICAL//垂直翻转
|
|
|
+ HORIZONTAL,//水平锟斤拷转
|
|
|
+ VERTICAL//锟斤拷直锟斤拷转
|
|
|
}
|
|
|
public enum UVCCameraPixelFormat
|
|
|
{
|
|
|
@@ -62,364 +54,97 @@ namespace SLAMUVC
|
|
|
PIXEL_FORMAT_RGB565,
|
|
|
PIXEL_FORMAT_RGBX
|
|
|
}
|
|
|
- //UVC的管理类
|
|
|
- public class UVCInterface : MonoBehaviour
|
|
|
+ public enum CameraStatus
|
|
|
{
|
|
|
- private const string UVC_MANAGER = "com.slambb.myuvc.MyUVCManager";
|
|
|
- //java对象
|
|
|
- private AndroidJavaObject uvcManagerObj = null;
|
|
|
+ Stopped = 0, // 鎽勫儚澶村仠姝�
|
|
|
+ Started = 1, // 鎽勫儚澶村惎鍔�
|
|
|
+ Connecting = 2, // 杩炴帴璁惧�涓�
|
|
|
+ Active = 3, // UI 婵€娲�
|
|
|
+ Idle = 4, // UI 绌洪棽
|
|
|
+ Scanning = 5, // 纭�欢鎵�弿涓�
|
|
|
+ NoDevice = 6, // 鏃犺�澶�
|
|
|
+ ResolutionChanged = 7, // 鍒嗚鲸鐜囦慨鏀�
|
|
|
+ RenderUpdate = 8, // 娓叉煋鏇存柊
|
|
|
+ }
|
|
|
+ //UVC锟侥癸拷锟斤拷锟斤拷
|
|
|
+ public abstract class UVCInterface : MonoBehaviour
|
|
|
+ {
|
|
|
+ // 鍒濆�鍖栫浉鏈�
|
|
|
+ public abstract void InitCamera(int width, int height);
|
|
|
|
|
|
- CameraFormat mCameraFormat = CameraFormat.MJPEG;
|
|
|
- CameraMirror mCameraMirror = CameraMirror.VERTICAL;//默认垂直
|
|
|
- int Width = 1280;
|
|
|
- int Height = 720;
|
|
|
- Texture2D _cameraTexture;
|
|
|
- public Texture2D CameraTexture => _cameraTexture;
|
|
|
+ // 鎵撳紑鐩告満
|
|
|
+ public abstract void OpenCamera();
|
|
|
|
|
|
- private UVCCtrl[] uvcCtrls;
|
|
|
- public UVCCtrl[] UVCCtrls => uvcCtrls;
|
|
|
+ // 鍏抽棴鐩告満
|
|
|
+ public abstract void CloseCamera();
|
|
|
|
|
|
+ // 鏀瑰彉鐩告満鍒嗚鲸鐜�
|
|
|
+ public abstract void ChangeCameraInfo(int width, int height);
|
|
|
|
|
|
- private SynchronizationContext mainContext;
|
|
|
+ // 鏀瑰彉鐩告満鍒嗚鲸鐜囧洖璋�
|
|
|
+ public abstract void ChangeCameraInfoCallback(int width, int height);
|
|
|
+ // 鑾峰彇鏀�寔鐨勫垎杈ㄧ巼鍒楄〃
|
|
|
+ public abstract string[] GetSupportedResolutions();
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 初始化相机权限成功
|
|
|
- /// </summary>
|
|
|
- public CameraPermissionHandle systemCameraPermissionHandle;
|
|
|
- public delegate void CameraPermissionHandle();
|
|
|
- /// <summary>
|
|
|
- /// 纹理更新事件
|
|
|
- /// </summary>
|
|
|
- public CameraTextureHandle cameraTextureHandle;
|
|
|
- public delegate void CameraTextureHandle();
|
|
|
+ // 鑾峰彇 UVC 鎺у埗鍒楄〃锛堝�浜�害銆佸�姣斿害绛夋帶鍒跺姛鑳斤級
|
|
|
+ public abstract string GetUvcCtrlList();
|
|
|
|
|
|
+ // 鎺у埗浜�害
|
|
|
+ public abstract void SetBrightness(int value);
|
|
|
|
|
|
+ // 鎺у埗瀵规瘮搴�
|
|
|
+ public abstract void SetContrast(int value);
|
|
|
|
|
|
- private static readonly string ActivityClassName = "com.slambb.myuvc.UsbPermissionActivity";
|
|
|
+ // 璁剧疆 UVC 鎺у埗鍔熻兘鐨勫€硷紙濡備寒搴︺€佸�姣斿害锛�
|
|
|
+ public abstract int SetCtrlValue(string type, int value);
|
|
|
|
|
|
- //public void RequestUsbPermission()
|
|
|
- //{
|
|
|
- // using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
|
|
- // {
|
|
|
- // AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
|
|
- // AndroidJavaClass usbPermissionActivity = new AndroidJavaClass(ActivityClassName);
|
|
|
+ //
|
|
|
+ public abstract void FlipHorizontally();
|
|
|
|
|
|
- // // Start UsbPermissionActivity
|
|
|
- // activity.Call("startActivity", usbPermissionActivity.CallStatic<AndroidJavaObject>("newIntent", activity));
|
|
|
- // }
|
|
|
- //}
|
|
|
+ //
|
|
|
+ public abstract void FlipVertically();
|
|
|
|
|
|
- void Awake()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- // 创建 uvcManagerObj 对象
|
|
|
- uvcManagerObj = new AndroidJavaObject(UVC_MANAGER);
|
|
|
+ //閲嶇疆鍙傛暟
|
|
|
+ public abstract void resetControlParams();
|
|
|
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- Debug.LogError("UVC_MANAGER Exception: " + e.Message);
|
|
|
- }
|
|
|
+ public abstract bool GetIsPreviewing();
|
|
|
|
|
|
- //RequestUsbPermission();
|
|
|
- }
|
|
|
+ public abstract void OnTextureUpdate(int width, int height, bool bUpdate);
|
|
|
|
|
|
- void Start()
|
|
|
- {
|
|
|
|
|
|
- OpenCameraPermisson();
|
|
|
- }
|
|
|
+ // 鐩告満绾圭悊
|
|
|
+ public abstract Texture2D CameraTexture { get; }
|
|
|
|
|
|
- private void OnDestroy()
|
|
|
- {
|
|
|
- Remove(this);
|
|
|
- uvcManagerObj.Call("clearCameraHelper");
|
|
|
+ // UVC灞炴€�
|
|
|
+ public abstract UVCCtrl[] UVCCtrls { get; }
|
|
|
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 创建一个纹理id到插件
|
|
|
- /// </summary>
|
|
|
- private void CreateTextureAndPassToPlugin()
|
|
|
- {
|
|
|
- // 销毁纹理
|
|
|
- if (_cameraTexture != null)
|
|
|
- {
|
|
|
- Destroy(_cameraTexture);
|
|
|
- _cameraTexture = null;
|
|
|
- }
|
|
|
- // Create a texture
|
|
|
- Texture2D tex = new Texture2D(Width, Height, TextureFormat.ARGB32, false);
|
|
|
- // 设置点过滤,以便我们可以清楚地看到像素
|
|
|
- tex.filterMode = FilterMode.Point;
|
|
|
- // 调用Apply(),使其实际上传到GPU
|
|
|
- tex.Apply();
|
|
|
- _cameraTexture = tex;
|
|
|
- //将纹理指针传递给插件
|
|
|
- SetTexture(tex.GetNativeTexturePtr(), tex.width, tex.height);
|
|
|
-
|
|
|
- cameraTextureHandle?.Invoke();
|
|
|
- }
|
|
|
- //private IEnumerator CallPluginAtEndOfFrames()
|
|
|
- //{
|
|
|
- // while (true)
|
|
|
- // {
|
|
|
- // //等待所有帧渲染完成
|
|
|
- // yield return new WaitForEndOfFrame();
|
|
|
- // //发出具有任意整数标识符的插件事件。
|
|
|
- // //该插件可以区分不同的
|
|
|
- // //它需要根据这个ID做的事情。
|
|
|
- // //对于我们的简单插件,我们在这里传递哪个ID并不重要。
|
|
|
- // if(GetIsPreviewing()) GL.IssuePluginEvent(GetRenderEventFunc(), 1);
|
|
|
- // }
|
|
|
- //}
|
|
|
+ // 杩囧害鍒嗚鲸鐜�
|
|
|
+ public abstract int[] tempCameraSize { get; }
|
|
|
/// <summary>
|
|
|
- /// 更新纹理
|
|
|
+ /// 鎽勫儚澶存潈闄愭垚鍔熶簨浠�
|
|
|
/// </summary>
|
|
|
- /// <param name="width"></param>
|
|
|
- /// <param name="height"></param>
|
|
|
- /// <param name="bUpdate"></param>
|
|
|
- public void OnTextureUpdate(int width, int height, bool bUpdate)
|
|
|
- {
|
|
|
-
|
|
|
- mainContext.Post(__ =>
|
|
|
- {
|
|
|
- if (bUpdate)
|
|
|
- {
|
|
|
- Debug.Log("创建新纹理:size=[" + width + "," + height + "]");
|
|
|
- Width = width;
|
|
|
- Height = height;
|
|
|
- CreateTextureAndPassToPlugin();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- GL.IssuePluginEvent(GetRenderEventFunc(), 1);
|
|
|
- }
|
|
|
-
|
|
|
- }, null);
|
|
|
- }
|
|
|
- public bool GetIsPreviewing()
|
|
|
- {
|
|
|
- if (uvcManagerObj != null)
|
|
|
- {
|
|
|
- if (uvcManagerObj.Call<int>("GetIsPreviewing") == 1)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
+ public event Action systemCameraPermissionHandle;
|
|
|
|
|
|
- return false;
|
|
|
- }
|
|
|
/// <summary>
|
|
|
- /// 初始化相机和对应的操作,初始化后会自动打开相机
|
|
|
+ /// 鎽勫儚澶寸汗鐞嗘洿鏂颁簨浠�
|
|
|
/// </summary>
|
|
|
- /// <param name="width"></param>
|
|
|
- /// <param name="height"></param>
|
|
|
- public void InitCamera(int width, int height)
|
|
|
- {
|
|
|
- Width = width;
|
|
|
- Height = height;
|
|
|
- uvcManagerObj.Call("initCameraHelper", Width, Height, mCameraFormat.ToString(), mCameraMirror.ToString());
|
|
|
- //主线程
|
|
|
- mainContext = SynchronizationContext.Current;
|
|
|
- //注册c回调
|
|
|
- Add(this);
|
|
|
- //mainContext.Post(__ =>
|
|
|
- //{
|
|
|
- // //CreateTextureAndPassToPlugin();
|
|
|
- //}, null);
|
|
|
- //StartCoroutine("CallPluginAtEndOfFrames");
|
|
|
- }
|
|
|
-
|
|
|
- public void OpenCamera()
|
|
|
- {
|
|
|
- uvcManagerObj.Call("OpenCamera");
|
|
|
- }
|
|
|
- public void CloseCamera()
|
|
|
- {
|
|
|
- uvcManagerObj.Call("CloseCamera");
|
|
|
- }
|
|
|
- public void ChangeCameraInfo(int width, int height)
|
|
|
- {
|
|
|
- uvcManagerObj.Call("ChangeCameraInfo", width, height, mCameraFormat.ToString());
|
|
|
- }
|
|
|
- public string[] GetSupportedResolutions()
|
|
|
- {
|
|
|
- if (uvcManagerObj != null)
|
|
|
- {
|
|
|
- var objPtr = uvcManagerObj.Call<AndroidJavaObject>("GetSupportedResolutions").GetRawObject();
|
|
|
-
|
|
|
- if (objPtr != IntPtr.Zero)
|
|
|
- return AndroidJNIHelper.ConvertFromJNIArray<string[]>(objPtr);
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
+ public event Action cameraTextureHandle;
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 水平翻转
|
|
|
+ /// 瑙﹀彂鎽勫儚澶存潈闄愭垚鍔熶簨浠讹紙渚涘瓙绫昏皟鐢�級
|
|
|
/// </summary>
|
|
|
- public void FlipHorizontally()
|
|
|
+ public void OnCameraPermissionGranted()
|
|
|
{
|
|
|
- uvcManagerObj.Call("FlipHorizontally");
|
|
|
+ systemCameraPermissionHandle?.Invoke();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 垂直翻转
|
|
|
+ /// 瑙﹀彂鎽勫儚澶寸汗鐞嗘洿鏂颁簨浠讹紙渚涘瓙绫昏皟鐢�級
|
|
|
/// </summary>
|
|
|
- public void FlipVertically()
|
|
|
- {
|
|
|
- uvcManagerObj.Call("FlipVertically");
|
|
|
- }
|
|
|
- public void resetControlParams()
|
|
|
- {
|
|
|
- uvcManagerObj.Call("resetControlParams");
|
|
|
- }
|
|
|
- public void GetUvcCtrlList()
|
|
|
+ public void OnCameraTextureUpdated()
|
|
|
{
|
|
|
-
|
|
|
- if (uvcManagerObj != null)
|
|
|
- {
|
|
|
- string json = uvcManagerObj.Call<string>("GetUvcCtrlList");
|
|
|
-
|
|
|
- UVCCtrlWrapper wrapper = JsonUtility.FromJson<UVCCtrlWrapper>("{\"uvcCtrls\":" + json + "}");
|
|
|
- if (wrapper != null && wrapper.uvcCtrls != null)
|
|
|
- {
|
|
|
-
|
|
|
- //赋值
|
|
|
- uvcCtrls = wrapper.uvcCtrls;
|
|
|
-
|
|
|
- //foreach (UVCCtrl ctrl in wrapper.uvcCtrls)
|
|
|
- //{
|
|
|
- // Debug.Log($"name:{ctrl.name}, isAuto: {ctrl.isAuto}, isEnable: {ctrl.isEnable}, limit: {string.Join(", ", ctrl.limit)}, value: {ctrl.value}");
|
|
|
- //}
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Debug.LogError("Failed to parse JSON data.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- public void SetBrightness(int value)
|
|
|
- {
|
|
|
- uvcManagerObj.Call("SetBrightness", value);
|
|
|
- }
|
|
|
- public void SetContrast(int value)
|
|
|
- {
|
|
|
- uvcManagerObj.Call("SetContrast", value);
|
|
|
- }
|
|
|
-
|
|
|
- public int SetCtrlValue(string type, Int32 value)
|
|
|
- {
|
|
|
- int result = -1;
|
|
|
- if (type == "PU_BRIGHTNESS")
|
|
|
- {
|
|
|
- uvcManagerObj.Call("SetBrightness", value);
|
|
|
- result = 0;
|
|
|
- }
|
|
|
- else if (type == "PU_CONTRAST")
|
|
|
- {
|
|
|
- uvcManagerObj.Call("SetContrast", value);
|
|
|
- result = 0;
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- //--------------------------------------------------------------------------------
|
|
|
- /**
|
|
|
- * 先处理相机权限
|
|
|
- */
|
|
|
- private void OpenCameraPermisson()
|
|
|
- {
|
|
|
-
|
|
|
- if (Permission.HasUserAuthorizedPermission(Permission.Camera))
|
|
|
- {
|
|
|
- //Debug.LogError("HasUserAuthorizedPermission!");
|
|
|
- systemCameraPermissionHandle?.Invoke();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- bool useCallbacks = true;
|
|
|
- if (!useCallbacks)
|
|
|
- {
|
|
|
- // We do not have permission to use the microphone.
|
|
|
- // Ask for permission or proceed without the functionality enabled.
|
|
|
- Permission.RequestUserPermission(Permission.Camera);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- var callbacks = new PermissionCallbacks();
|
|
|
- callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
|
|
|
- callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
|
|
|
- callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
|
|
|
- Permission.RequestUserPermission(Permission.Camera, callbacks);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- void PermissionCallbacks_PermissionDenied(string PermissionName)
|
|
|
- {
|
|
|
- Debug.LogError($"PermissionCallbacks_PermissionDenied[{PermissionName}]");
|
|
|
- }
|
|
|
-
|
|
|
- //本次允许
|
|
|
- void PermissionCallbacks_PermissionGranted(string PermissionName)
|
|
|
- {
|
|
|
- Debug.Log($"PermissionCallbacks_PermissionGranted[{PermissionName}]");
|
|
|
- systemCameraPermissionHandle?.Invoke();
|
|
|
- }
|
|
|
- void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string PermissionName)
|
|
|
- {
|
|
|
- Debug.Log($"PermissionCallbacks_PermissionDeniedAndDontAskAgain[{PermissionName}]");
|
|
|
- systemCameraPermissionHandle?.Invoke();
|
|
|
- }
|
|
|
-
|
|
|
- //设置GL纹理
|
|
|
- [DllImport("TransferTexture")]
|
|
|
- private static extern void SetTexture(IntPtr texture, int w, int h);
|
|
|
- //GL渲染事件
|
|
|
- [DllImport("TransferTexture")]
|
|
|
- private static extern IntPtr GetRenderEventFunc();
|
|
|
-
|
|
|
-
|
|
|
- [UnmanagedFunctionPointer(CallingConvention.StdCall)]
|
|
|
- public delegate void TextureUpdateDelegate(Int32 id, int width, int height, bool bUpdate);
|
|
|
-
|
|
|
- [DllImport("TransferTexture")]
|
|
|
- private static extern void RegisterTextureUpdateCallback(Int32 id, TextureUpdateDelegate callback);
|
|
|
- [DllImport("TransferTexture")]
|
|
|
- private static extern void UnregisterTextureUpdateCallback(Int32 id, TextureUpdateDelegate callback);
|
|
|
- //记录MyUVCInterface
|
|
|
- private static Dictionary<Int32, UVCInterface> mUVCInterfaces = new Dictionary<Int32, UVCInterface>();
|
|
|
-
|
|
|
- private static TextureUpdateDelegate textureUpdateDelegate;
|
|
|
- //注册回调事件
|
|
|
- public static void Add(UVCInterface myUVCInterface)
|
|
|
- {
|
|
|
- Int32 id = myUVCInterface.GetHashCode();
|
|
|
- textureUpdateDelegate = new TextureUpdateDelegate(TextureUpdateHandler);
|
|
|
- mUVCInterfaces.Add(id, myUVCInterface);
|
|
|
- RegisterTextureUpdateCallback(id, textureUpdateDelegate);
|
|
|
- }
|
|
|
- //移除回调事件
|
|
|
- public static void Remove(UVCInterface myUVCInterface)
|
|
|
- {
|
|
|
- Int32 id = myUVCInterface.GetHashCode();
|
|
|
- UnregisterTextureUpdateCallback(id, textureUpdateDelegate);
|
|
|
- mUVCInterfaces.Remove(id);
|
|
|
- }
|
|
|
- //数据更新时候回调
|
|
|
- [MonoPInvokeCallback(typeof(TextureUpdateDelegate))]
|
|
|
- private static void TextureUpdateHandler(Int32 id, int width, int height, bool bUpdate)
|
|
|
- {
|
|
|
- var myUVCInterface = mUVCInterfaces.ContainsKey(id) ? mUVCInterfaces[id] : null;
|
|
|
- if (myUVCInterface != null)
|
|
|
- {
|
|
|
- myUVCInterface.OnTextureUpdate(width, height, bUpdate);
|
|
|
- }
|
|
|
+ cameraTextureHandle?.Invoke();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-}
|
|
|
+}
|