CameraModelView.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. /// <summary> A camera model view used to receive camera image. </summary>
  13. public class CameraModelView
  14. {
  15. /// <summary> Gets the width. </summary>
  16. /// <value> The width. </value>
  17. public int Width
  18. {
  19. get
  20. {
  21. return m_NativeCameraProxy.Resolution.width;
  22. }
  23. }
  24. /// <summary> Gets the height. </summary>
  25. /// <value> The height. </value>
  26. public int Height
  27. {
  28. get
  29. {
  30. return m_NativeCameraProxy.Resolution.height;
  31. }
  32. }
  33. /// <summary> Gets a value indicating whether this object is playing. </summary>
  34. /// <value> True if this object is playing, false if not. </value>
  35. public bool IsPlaying
  36. {
  37. get
  38. {
  39. return m_State == State.Playing;
  40. }
  41. }
  42. /// <summary> Values that represent states. </summary>
  43. public enum State
  44. {
  45. /// <summary> An enum constant representing the playing option. </summary>
  46. Playing,
  47. /// <summary> An enum constant representing the paused option. </summary>
  48. Paused,
  49. /// <summary> An enum constant representing the stopped option. </summary>
  50. Stopped
  51. }
  52. /// <summary> The state. </summary>
  53. private State m_State = State.Stopped;
  54. /// <summary> Gets a value indicating whether the did update this frame. </summary>
  55. /// <value> True if did update this frame, false if not. </value>
  56. public bool DidUpdateThisFrame
  57. {
  58. get
  59. {
  60. return m_NativeCameraProxy.HasFrame();
  61. }
  62. }
  63. /// <summary> Gets or sets the number of frames. </summary>
  64. /// <value> The number of frames. </value>
  65. public int FrameCount { get; protected set; }
  66. /// <summary> The native camera proxy. </summary>
  67. protected NativeCameraProxy m_NativeCameraProxy;
  68. /// <summary> Gets or sets the native camera proxy. </summary>
  69. /// <value> The native camera proxy. </value>
  70. public NativeCameraProxy NativeCameraProxy
  71. {
  72. get
  73. {
  74. return this.m_NativeCameraProxy;
  75. }
  76. set
  77. {
  78. this.m_NativeCameraProxy = value;
  79. }
  80. }
  81. public CameraImageFormat ImageFormat { get; protected set; }
  82. /// <summary> Default constructor. </summary>
  83. public CameraModelView() { }
  84. /// <summary> Constructor. </summary>
  85. /// <param name="format"> Camera image format.</param>
  86. public CameraModelView(CameraImageFormat format)
  87. {
  88. ImageFormat = format;
  89. this.CreateRGBCameraProxy(format);
  90. m_NativeCameraProxy.Regist(this);
  91. }
  92. /// <summary> Use RGB_888 format default. </summary>
  93. /// <param name="format"> (Optional) Camera image format.</param>
  94. protected void CreateRGBCameraProxy(CameraImageFormat format = CameraImageFormat.RGB_888)
  95. {
  96. if (m_NativeCameraProxy != null)
  97. {
  98. return;
  99. }
  100. m_NativeCameraProxy = CameraProxyFactory.CreateRGBCameraProxy();
  101. m_NativeCameraProxy.SetImageFormat(format);
  102. }
  103. /// <summary> Plays this object. </summary>
  104. public void Play()
  105. {
  106. if (m_State == State.Playing)
  107. {
  108. return;
  109. }
  110. NRKernalUpdater.OnUpdate += UpdateTexture;
  111. m_NativeCameraProxy.Play();
  112. m_NativeCameraProxy.Regist(this);
  113. m_State = State.Playing;
  114. }
  115. /// <summary> Pauses this object. </summary>
  116. public void Pause()
  117. {
  118. if (m_State == State.Paused || m_State == State.Stopped)
  119. {
  120. return;
  121. }
  122. NRKernalUpdater.OnUpdate -= UpdateTexture;
  123. m_State = State.Paused;
  124. }
  125. /// <summary> Updates the texture. </summary>
  126. private void UpdateTexture()
  127. {
  128. if (!DidUpdateThisFrame || !IsPlaying)
  129. {
  130. return;
  131. }
  132. FrameRawData frame = m_NativeCameraProxy.GetFrame();
  133. if (frame.data == null)
  134. {
  135. NRDebugger.Error("[CameraModelView] Get camera raw data faild...");
  136. return;
  137. }
  138. FrameCount++;
  139. OnRawDataUpdate(frame);
  140. }
  141. /// <summary> Stops this object. </summary>
  142. public void Stop()
  143. {
  144. if (m_State == State.Stopped)
  145. {
  146. return;
  147. }
  148. m_NativeCameraProxy.Remove(this);
  149. m_NativeCameraProxy.Stop();
  150. NRKernalUpdater.OnUpdate -= UpdateTexture;
  151. FrameCount = 0;
  152. m_State = State.Stopped;
  153. this.OnStopped();
  154. }
  155. /// <summary> Load raw texture data. </summary>
  156. /// <param name="frame"> .</param>
  157. protected virtual void OnRawDataUpdate(FrameRawData frame) { }
  158. /// <summary> On texture stopped. </summary>
  159. protected virtual void OnStopped() { }
  160. }
  161. }