PhotoCaptureExample.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. using NRKernal.Record;
  10. using System;
  11. using System.IO;
  12. using System.Linq;
  13. using UnityEngine;
  14. namespace NRKernal.NRExamples
  15. {
  16. #if UNITY_ANDROID && !UNITY_EDITOR
  17. using GalleryDataProvider = NativeGalleryDataProvider;
  18. #else
  19. using GalleryDataProvider = MockGalleryDataProvider;
  20. #endif
  21. /// <summary> A photo capture example. </summary>
  22. [HelpURL("https://developer.nreal.ai/develop/unity/video-capture")]
  23. public class PhotoCaptureExample : MonoBehaviour
  24. {
  25. /// <summary> The photo capture object. </summary>
  26. private NRPhotoCapture m_PhotoCaptureObject;
  27. /// <summary> The camera resolution. </summary>
  28. private Resolution m_CameraResolution;
  29. private bool isOnPhotoProcess = false;
  30. GalleryDataProvider galleryDataTool;
  31. void Update()
  32. {
  33. if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
  34. {
  35. TakeAPhoto();
  36. }
  37. }
  38. /// <summary> Use this for initialization. </summary>
  39. void Create(Action<NRPhotoCapture> onCreated)
  40. {
  41. if (m_PhotoCaptureObject != null)
  42. {
  43. NRDebugger.Info("The NRPhotoCapture has already been created.");
  44. return;
  45. }
  46. // Create a PhotoCapture object
  47. NRPhotoCapture.CreateAsync(false, delegate (NRPhotoCapture captureObject)
  48. {
  49. m_CameraResolution = NRPhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
  50. if (captureObject == null)
  51. {
  52. NRDebugger.Error("Can not get a captureObject.");
  53. return;
  54. }
  55. m_PhotoCaptureObject = captureObject;
  56. CameraParameters cameraParameters = new CameraParameters();
  57. cameraParameters.cameraResolutionWidth = m_CameraResolution.width;
  58. cameraParameters.cameraResolutionHeight = m_CameraResolution.height;
  59. cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
  60. cameraParameters.frameRate = NativeConstants.RECORD_FPS_DEFAULT;
  61. cameraParameters.blendMode = BlendMode.Blend;
  62. // Activate the camera
  63. m_PhotoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate (NRPhotoCapture.PhotoCaptureResult result)
  64. {
  65. NRDebugger.Info("Start PhotoMode Async");
  66. if (result.success)
  67. {
  68. onCreated?.Invoke(m_PhotoCaptureObject);
  69. }
  70. else
  71. {
  72. isOnPhotoProcess = false;
  73. this.Close();
  74. NRDebugger.Error("Start PhotoMode faild." + result.resultType);
  75. }
  76. }, true);
  77. });
  78. }
  79. /// <summary> Take a photo. </summary>
  80. void TakeAPhoto()
  81. {
  82. if (isOnPhotoProcess)
  83. {
  84. NRDebugger.Warning("Currently in the process of taking pictures, Can not take photo .");
  85. return;
  86. }
  87. isOnPhotoProcess = true;
  88. if (m_PhotoCaptureObject == null)
  89. {
  90. this.Create((capture) =>
  91. {
  92. capture.TakePhotoAsync(OnCapturedPhotoToMemory);
  93. });
  94. }
  95. else
  96. {
  97. m_PhotoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
  98. }
  99. }
  100. /// <summary> Executes the 'captured photo memory' action. </summary>
  101. /// <param name="result"> The result.</param>
  102. /// <param name="photoCaptureFrame"> The photo capture frame.</param>
  103. void OnCapturedPhotoToMemory(NRPhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
  104. {
  105. var targetTexture = new Texture2D(m_CameraResolution.width, m_CameraResolution.height);
  106. // Copy the raw image data into our target texture
  107. photoCaptureFrame.UploadImageDataToTexture(targetTexture);
  108. // Create a gameobject that we can apply our texture to
  109. GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
  110. Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer;
  111. quadRenderer.material = new Material(Resources.Load<Shader>("Record/Shaders/CaptureScreen"));
  112. var headTran = NRSessionManager.Instance.NRHMDPoseTracker.centerAnchor;
  113. quad.name = "picture";
  114. quad.transform.localPosition = headTran.position + headTran.forward * 3f;
  115. quad.transform.forward = headTran.forward;
  116. quad.transform.localScale = new Vector3(1.6f, 0.9f, 0);
  117. quadRenderer.material.SetTexture("_MainTex", targetTexture);
  118. SaveTextureAsPNG(targetTexture);
  119. SaveTextureToGallery(targetTexture);
  120. // Release camera resource after capture the photo.
  121. this.Close();
  122. }
  123. void SaveTextureAsPNG(Texture2D _texture)
  124. {
  125. try
  126. {
  127. string filename = string.Format("Nreal_Shot_{0}.png", NRTools.GetTimeStamp().ToString());
  128. string path = string.Format("{0}/NrealShots", Application.persistentDataPath);
  129. string filePath = string.Format("{0}/{1}", path, filename);
  130. byte[] _bytes = _texture.EncodeToPNG();
  131. NRDebugger.Info("Photo capture: {0}Kb was saved to [{1}]", _bytes.Length / 1024, filePath);
  132. if (!Directory.Exists(path))
  133. {
  134. Directory.CreateDirectory(path);
  135. }
  136. File.WriteAllBytes(string.Format("{0}/{1}", path, filename), _bytes);
  137. }
  138. catch (Exception e)
  139. {
  140. NRDebugger.Error("Save picture faild!");
  141. throw e;
  142. }
  143. }
  144. /// <summary> Closes this object. </summary>
  145. void Close()
  146. {
  147. if (m_PhotoCaptureObject == null)
  148. {
  149. NRDebugger.Error("The NRPhotoCapture has not been created.");
  150. return;
  151. }
  152. // Deactivate our camera
  153. m_PhotoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
  154. }
  155. /// <summary> Executes the 'stopped photo mode' action. </summary>
  156. /// <param name="result"> The result.</param>
  157. void OnStoppedPhotoMode(NRPhotoCapture.PhotoCaptureResult result)
  158. {
  159. // Shutdown our photo capture resource
  160. m_PhotoCaptureObject?.Dispose();
  161. m_PhotoCaptureObject = null;
  162. isOnPhotoProcess = false;
  163. }
  164. /// <summary> Executes the 'destroy' action. </summary>
  165. void OnDestroy()
  166. {
  167. // Shutdown our photo capture resource
  168. m_PhotoCaptureObject?.Dispose();
  169. m_PhotoCaptureObject = null;
  170. }
  171. public void SaveTextureToGallery(Texture2D _texture)
  172. {
  173. try
  174. {
  175. string filename = string.Format("Nreal_Shot_{0}.png", NRTools.GetTimeStamp().ToString());
  176. byte[] _bytes = _texture.EncodeToPNG();
  177. NRDebugger.Info(_bytes.Length / 1024 + "Kb was saved as: " + filename);
  178. if (galleryDataTool == null)
  179. {
  180. galleryDataTool = new GalleryDataProvider();
  181. }
  182. galleryDataTool.InsertImage(_bytes, filename, "Screenshots");
  183. }
  184. catch (Exception e)
  185. {
  186. NRDebugger.Error("[TakePicture] Save picture faild!");
  187. throw e;
  188. }
  189. }
  190. }
  191. }