123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
-
- namespace NRKernal.Record
- {
- using System;
- using UnityEngine;
- using System.Collections.Generic;
-
- public class NRPhotoCapture : IDisposable
- {
- private const int CameraDataReadyTimeOut = 3000;
-
- private static IEnumerable<Resolution> m_SupportedResolutions;
-
-
- public static IEnumerable<Resolution> SupportedResolutions
- {
- get
- {
- if (m_SupportedResolutions == null)
- {
- var resolutions = new List<Resolution>();
- var resolution = new Resolution();
- NativeResolution rgbResolution = new NativeResolution(1280, 720);
- if (NRDevice.Subsystem.IsFeatureSupported(NRSupportedFeature.NR_FEATURE_RGB_CAMERA))
- rgbResolution = NRFrame.GetDeviceResolution(NativeDevice.RGB_CAMERA);
- resolution.width = rgbResolution.width;
- resolution.height = rgbResolution.height;
- resolutions.Add(resolution);
- m_SupportedResolutions = resolutions;
- }
- return m_SupportedResolutions;
- }
- }
-
- private FrameCaptureContext m_CaptureContext;
-
-
- public Texture PreviewTexture
- {
- get
- {
- return m_CaptureContext?.PreviewTexture;
- }
- }
-
-
-
- public static void CreateAsync(bool showHolograms, OnCaptureResourceCreatedCallback onCreatedCallback)
- {
- NRPhotoCapture photocapture = new NRPhotoCapture();
- photocapture.m_CaptureContext = FrameCaptureContextFactory.Create();
- onCreatedCallback?.Invoke(photocapture);
- }
-
- public void Dispose()
- {
- if (m_CaptureContext != null)
- {
- m_CaptureContext.Release();
- m_CaptureContext = null;
- }
- }
-
-
-
-
- public IntPtr GetUnsafePointerToVideoDeviceController()
- {
- NRDebugger.Warning("[NRPhotoCapture] Interface not supported...");
- return IntPtr.Zero;
- }
-
-
-
-
- public void StartPhotoModeAsync(CameraParameters setupParams, OnPhotoModeStartedCallback onPhotoModeStartedCallback, bool autoAdaptBlendMode = false)
- {
- PhotoCaptureResult result = new PhotoCaptureResult();
- try
- {
- setupParams.camMode = CamMode.PhotoMode;
- if (autoAdaptBlendMode)
- {
- var blendMode = m_CaptureContext.AutoAdaptBlendMode(setupParams.blendMode);
- if (blendMode != setupParams.blendMode)
- {
- NRDebugger.Warning("[PhotoCapture] AutoAdaptBlendMode : {0} => {1}", setupParams.blendMode, blendMode);
- setupParams.blendMode = blendMode;
- }
- }
- if (setupParams.frameRate <= 0)
- NRDebugger.Warning("[PhotoCapture] frameRate need to be bigger than zero");
- m_CaptureContext.StartCaptureMode(setupParams);
- m_CaptureContext.StartCapture();
- NRKernalUpdater.Instance.StartCoroutine(OnPhotoModeStartedReady((ready) =>
- {
- if (ready)
- {
- result.resultType = CaptureResultType.Success;
- }
- else
- {
- result.resultType = CaptureResultType.TimeOutError;
- }
- onPhotoModeStartedCallback?.Invoke(result);
- }));
- }
- catch (Exception)
- {
- result.resultType = CaptureResultType.UnknownError;
- onPhotoModeStartedCallback?.Invoke(result);
- throw;
- }
- }
-
-
-
- private System.Collections.IEnumerator OnPhotoModeStartedReady(Action<bool> callback)
- {
- System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
- stopwatch.Start();
- while (!this.m_CaptureContext.GetFrameProvider().IsFrameReady())
- {
- if (stopwatch.ElapsedMilliseconds > CameraDataReadyTimeOut)
- {
- callback?.Invoke(false);
- NRDebugger.Error("[PhotoCapture] Get rgbcamera data timeout...");
- yield break;
- }
- NRDebugger.Debug("[PhotoCapture] Wait for the first frame ready...");
- yield return new WaitForEndOfFrame();
- }
- yield return new WaitForEndOfFrame();
- yield return new WaitForEndOfFrame();
- yield return new WaitForEndOfFrame();
- callback?.Invoke(true);
- }
-
-
- public void StopPhotoModeAsync(OnPhotoModeStoppedCallback onPhotoModeStoppedCallback)
- {
- PhotoCaptureResult result = new PhotoCaptureResult();
- try
- {
- m_CaptureContext.StopCaptureMode();
- result.resultType = CaptureResultType.Success;
- onPhotoModeStoppedCallback?.Invoke(result);
- }
- catch (Exception)
- {
- result.resultType = CaptureResultType.UnknownError;
- onPhotoModeStoppedCallback?.Invoke(result);
- throw;
- }
- }
-
-
-
-
- public void TakePhotoAsync(string filename, PhotoCaptureFileOutputFormat fileOutputFormat, OnCapturedToDiskCallback onCapturedPhotoToDiskCallback)
- {
- try
- {
- var capture = m_CaptureContext.GetBehaviour();
- ((NRCaptureBehaviour)capture).Do(filename, fileOutputFormat);
- }
- catch (Exception)
- {
- throw;
- }
- }
-
-
- public void TakePhotoAsync(OnCapturedToMemoryCallback onCapturedPhotoToMemoryCallback)
- {
- try
- {
- var capture = m_CaptureContext.GetBehaviour();
- ((NRCaptureBehaviour)capture).DoAsyn(onCapturedPhotoToMemoryCallback);
- }
- catch (Exception)
- {
- throw;
- }
- }
-
- public enum CaptureResultType
- {
-
-
-
- Success = 0,
-
-
-
- UnknownError = 1,
-
-
-
- TimeOutError = 2,
- }
-
-
- public struct PhotoCaptureResult
- {
-
-
- public CaptureResultType resultType;
-
- public long hResult;
-
-
- public bool success
- {
- get
- {
- return resultType == CaptureResultType.Success;
- }
- }
- }
-
-
- public delegate void OnCaptureResourceCreatedCallback(NRPhotoCapture captureObject);
-
-
- public delegate void OnPhotoModeStartedCallback(PhotoCaptureResult result);
-
-
- public delegate void OnPhotoModeStoppedCallback(PhotoCaptureResult result);
-
-
-
- public delegate void OnCapturedToDiskCallback(PhotoCaptureResult result);
-
-
-
-
-
- public delegate void OnCapturedToMemoryCallback(PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame);
- }
- }
|