NRCaptureBehaviour.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Record
  10. {
  11. using UnityEngine;
  12. using System.IO;
  13. /// <summary>
  14. /// Capture a image from the MR world. You can capture a RGB only,Virtual only or Blended image
  15. /// through this class. </summary>
  16. public class NRCaptureBehaviour : CaptureBehaviourBase
  17. {
  18. /// <summary> Gets the image encoder. </summary>
  19. /// <value> The image encoder. </value>
  20. private ImageEncoder ImageEncoder
  21. {
  22. get
  23. {
  24. return this.GetContext().GetEncoder() as ImageEncoder;
  25. }
  26. }
  27. /// <summary> Does the given file. </summary>
  28. /// <param name="width"> The width.</param>
  29. /// <param name="height"> The height.</param>
  30. /// <param name="format"> Describes the format to use.</param>
  31. /// <param name="outpath"> The outpath.</param>
  32. /// <returns> True if it succeeds, false if it fails. </returns>
  33. public bool Do(int width, int height, PhotoCaptureFileOutputFormat format, string outpath)
  34. {
  35. var data = this.ImageEncoder.Encode(width, height, format);
  36. if (data == null)
  37. {
  38. return false;
  39. }
  40. File.WriteAllBytes(outpath, data);
  41. return true;
  42. }
  43. /// <summary> Does the given file. </summary>
  44. /// <param name="width"> The width.</param>
  45. /// <param name="height"> The height.</param>
  46. /// <param name="format"> Describes the format to use.</param>
  47. /// <param name="data"> [in,out] The data.</param>
  48. /// <returns> True if it succeeds, false if it fails. </returns>
  49. public bool Do(int width, int height, PhotoCaptureFileOutputFormat format, ref byte[] data)
  50. {
  51. data = this.ImageEncoder.Encode(width, height, format);
  52. if (data == null)
  53. {
  54. return false;
  55. }
  56. return true;
  57. }
  58. /// <summary>
  59. /// Capture a image Asyn. if system supports AsyncGPUReadback, using AsyncGPUReadback to get the
  60. /// captured image, else getting the image by synchronization way. </summary>
  61. /// <param name="task"> The task.</param>
  62. private void DoAsyn(CaptureTask task)
  63. {
  64. if (SystemInfo.supportsAsyncGPUReadback)
  65. {
  66. this.ImageEncoder.Commit(task);
  67. }
  68. else
  69. {
  70. var data = ImageEncoder.Encode(task.Width, task.Height, task.CaptureFormat);
  71. if (task.OnReceive != null)
  72. {
  73. task.OnReceive(task, data);
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Capture a image Asyn. if system supports AsyncGPUReadback, using AsyncGPUReadback to get the
  79. /// captured image, else getting the image by synchronization way. </summary>
  80. /// <param name="oncapturedcallback"> The oncapturedcallback.</param>
  81. public void DoAsyn(NRPhotoCapture.OnCapturedToMemoryCallback oncapturedcallback)
  82. {
  83. var captureTask = new CaptureTask();
  84. var cameraParam = this.GetContext().RequestCameraParam();
  85. captureTask.Width = cameraParam.cameraResolutionWidth;
  86. captureTask.Height = cameraParam.cameraResolutionHeight;
  87. captureTask.CaptureFormat = cameraParam.pixelFormat == CapturePixelFormat.PNG ? PhotoCaptureFileOutputFormat.PNG : PhotoCaptureFileOutputFormat.JPG;
  88. captureTask.OnReceive += (task, data) =>
  89. {
  90. if (oncapturedcallback != null)
  91. {
  92. var result = new NRPhotoCapture.PhotoCaptureResult();
  93. result.resultType = NRPhotoCapture.CaptureResultType.Success;
  94. CapturePixelFormat format = task.CaptureFormat == PhotoCaptureFileOutputFormat.PNG ? CapturePixelFormat.PNG : CapturePixelFormat.JPEG;
  95. PhotoCaptureFrame frame = new PhotoCaptureFrame(format, data);
  96. oncapturedcallback(result, frame);
  97. }
  98. };
  99. this.DoAsyn(captureTask);
  100. }
  101. /// <summary> Does the given file. </summary>
  102. /// <param name="filename"> Filename of the file.</param>
  103. /// <param name="fileOutputFormat"> The file output format.</param>
  104. public void Do(string filename, PhotoCaptureFileOutputFormat fileOutputFormat)
  105. {
  106. var cameraParam = this.GetContext().RequestCameraParam();
  107. this.Do(cameraParam.cameraResolutionWidth,
  108. cameraParam.cameraResolutionHeight,
  109. fileOutputFormat,
  110. filename
  111. );
  112. }
  113. }
  114. }