Tracking2dController.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace EZXR.Glass.Tracking2D
  5. {
  6. public class Tracking2dController
  7. {
  8. private string m_asset_path = "";
  9. private IntPtr m_tracking2d;
  10. /// <summary>
  11. /// 初始化
  12. /// </summary>
  13. /// <param name="model_path">模型路径</param>
  14. /// <returns></returns>
  15. public int CreateTracking2d(string model_path)
  16. {
  17. if (m_tracking2d != System.IntPtr.Zero)
  18. {
  19. Debug.LogError("tracking2d is created");
  20. return 0;
  21. }
  22. m_asset_path = model_path;
  23. Debug.Log("model_path:" + model_path);
  24. m_tracking2d = ExternApi.create2dDetector(model_path);
  25. if (m_tracking2d == System.IntPtr.Zero)
  26. {
  27. Debug.LogError("tracking2d ctrate failed");
  28. return -1;
  29. }
  30. return 1;
  31. }
  32. /// <summary>
  33. /// 释放
  34. /// </summary>
  35. public void DestroyTracking2d()
  36. {
  37. if (m_tracking2d != System.IntPtr.Zero)
  38. {
  39. ExternApi.destroy2dDetector(m_tracking2d);
  40. m_tracking2d = System.IntPtr.Zero;
  41. }
  42. }
  43. /// <summary>
  44. /// 开始追踪
  45. /// </summary>
  46. /// <returns></returns>
  47. public int SetImage(IntPtr imageptr, float[] extrisic, float[] intrinsic, double timestamp, int width, int height, int format)
  48. {
  49. if (m_tracking2d == System.IntPtr.Zero)
  50. {
  51. Debug.LogError("tracking2d is not created");
  52. return 0;
  53. }
  54. if (imageptr == System.IntPtr.Zero)
  55. {
  56. Debug.LogError("image is null");
  57. return 0;
  58. }
  59. EZXR_InputImage inputImage = new EZXR_InputImage();
  60. inputImage.imagePtr = imageptr;
  61. inputImage.extrisic = extrisic;
  62. inputImage.intrisic = intrinsic;
  63. inputImage.timestamp = timestamp;
  64. inputImage.width = width;
  65. inputImage.height = height;
  66. inputImage.format = format;
  67. int ret = ExternApi.setImage(m_tracking2d, inputImage);
  68. return ret;
  69. }
  70. public EZXR_Detect2DResult GetDetectedMarker()
  71. {
  72. EZXR_Detect2DResult result = new EZXR_Detect2DResult();
  73. result.state = (int)EZXR_Detect2DState.EZXR_Detect2DState_UnInit;
  74. if (m_tracking2d == System.IntPtr.Zero)
  75. {
  76. Debug.LogError("tracking2d is not created");
  77. return result;
  78. }
  79. result = ExternApi.getDetectedMarker(m_tracking2d);
  80. return result;
  81. }
  82. }
  83. }