123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- /****************************************************************************
- * Copyright 2019 Nreal Techonology Limited. All rights reserved.
- *
- * This file is part of NRSDK.
- *
- * https://www.nreal.ai/
- *
- *****************************************************************************/
- using NRKernal.Record;
- using System;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- namespace NRKernal.NRExamples
- {
- #if UNITY_ANDROID && !UNITY_EDITOR
- using GalleryDataProvider = NativeGalleryDataProvider;
- #else
- using GalleryDataProvider = MockGalleryDataProvider;
- #endif
- /// <summary> A photo capture example. </summary>
- [HelpURL("https://developer.nreal.ai/develop/unity/video-capture")]
- public class PhotoCaptureExample : MonoBehaviour
- {
- /// <summary> The photo capture object. </summary>
- private NRPhotoCapture m_PhotoCaptureObject;
- /// <summary> The camera resolution. </summary>
- private Resolution m_CameraResolution;
- private bool isOnPhotoProcess = false;
- GalleryDataProvider galleryDataTool;
- void Update()
- {
- if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
- {
- TakeAPhoto();
- }
- }
- /// <summary> Use this for initialization. </summary>
- void Create(Action<NRPhotoCapture> onCreated)
- {
- if (m_PhotoCaptureObject != null)
- {
- NRDebugger.Info("The NRPhotoCapture has already been created.");
- return;
- }
- // Create a PhotoCapture object
- NRPhotoCapture.CreateAsync(false, delegate (NRPhotoCapture captureObject)
- {
- m_CameraResolution = NRPhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
- if (captureObject == null)
- {
- NRDebugger.Error("Can not get a captureObject.");
- return;
- }
- m_PhotoCaptureObject = captureObject;
- CameraParameters cameraParameters = new CameraParameters();
- cameraParameters.cameraResolutionWidth = m_CameraResolution.width;
- cameraParameters.cameraResolutionHeight = m_CameraResolution.height;
- cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
- cameraParameters.frameRate = NativeConstants.RECORD_FPS_DEFAULT;
- cameraParameters.blendMode = BlendMode.Blend;
- // Activate the camera
- m_PhotoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate (NRPhotoCapture.PhotoCaptureResult result)
- {
- NRDebugger.Info("Start PhotoMode Async");
- if (result.success)
- {
- onCreated?.Invoke(m_PhotoCaptureObject);
- }
- else
- {
- isOnPhotoProcess = false;
- this.Close();
- NRDebugger.Error("Start PhotoMode faild." + result.resultType);
- }
- }, true);
- });
- }
- /// <summary> Take a photo. </summary>
- void TakeAPhoto()
- {
- if (isOnPhotoProcess)
- {
- NRDebugger.Warning("Currently in the process of taking pictures, Can not take photo .");
- return;
- }
- isOnPhotoProcess = true;
- if (m_PhotoCaptureObject == null)
- {
- this.Create((capture) =>
- {
- capture.TakePhotoAsync(OnCapturedPhotoToMemory);
- });
- }
- else
- {
- m_PhotoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
- }
- }
- /// <summary> Executes the 'captured photo memory' action. </summary>
- /// <param name="result"> The result.</param>
- /// <param name="photoCaptureFrame"> The photo capture frame.</param>
- void OnCapturedPhotoToMemory(NRPhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
- {
- var targetTexture = new Texture2D(m_CameraResolution.width, m_CameraResolution.height);
- // Copy the raw image data into our target texture
- photoCaptureFrame.UploadImageDataToTexture(targetTexture);
- // Create a gameobject that we can apply our texture to
- GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
- Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer;
- quadRenderer.material = new Material(Resources.Load<Shader>("Record/Shaders/CaptureScreen"));
- var headTran = NRSessionManager.Instance.NRHMDPoseTracker.centerAnchor;
- quad.name = "picture";
- quad.transform.localPosition = headTran.position + headTran.forward * 3f;
- quad.transform.forward = headTran.forward;
- quad.transform.localScale = new Vector3(1.6f, 0.9f, 0);
- quadRenderer.material.SetTexture("_MainTex", targetTexture);
- SaveTextureAsPNG(targetTexture);
- SaveTextureToGallery(targetTexture);
- // Release camera resource after capture the photo.
- this.Close();
- }
- void SaveTextureAsPNG(Texture2D _texture)
- {
- try
- {
- string filename = string.Format("Nreal_Shot_{0}.png", NRTools.GetTimeStamp().ToString());
- string path = string.Format("{0}/NrealShots", Application.persistentDataPath);
- string filePath = string.Format("{0}/{1}", path, filename);
- byte[] _bytes = _texture.EncodeToPNG();
- NRDebugger.Info("Photo capture: {0}Kb was saved to [{1}]", _bytes.Length / 1024, filePath);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- File.WriteAllBytes(string.Format("{0}/{1}", path, filename), _bytes);
- }
- catch (Exception e)
- {
- NRDebugger.Error("Save picture faild!");
- throw e;
- }
- }
- /// <summary> Closes this object. </summary>
- void Close()
- {
- if (m_PhotoCaptureObject == null)
- {
- NRDebugger.Error("The NRPhotoCapture has not been created.");
- return;
- }
- // Deactivate our camera
- m_PhotoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
- }
- /// <summary> Executes the 'stopped photo mode' action. </summary>
- /// <param name="result"> The result.</param>
- void OnStoppedPhotoMode(NRPhotoCapture.PhotoCaptureResult result)
- {
- // Shutdown our photo capture resource
- m_PhotoCaptureObject?.Dispose();
- m_PhotoCaptureObject = null;
- isOnPhotoProcess = false;
- }
- /// <summary> Executes the 'destroy' action. </summary>
- void OnDestroy()
- {
- // Shutdown our photo capture resource
- m_PhotoCaptureObject?.Dispose();
- m_PhotoCaptureObject = null;
- }
- public void SaveTextureToGallery(Texture2D _texture)
- {
- try
- {
- string filename = string.Format("Nreal_Shot_{0}.png", NRTools.GetTimeStamp().ToString());
- byte[] _bytes = _texture.EncodeToPNG();
- NRDebugger.Info(_bytes.Length / 1024 + "Kb was saved as: " + filename);
- if (galleryDataTool == null)
- {
- galleryDataTool = new GalleryDataProvider();
- }
- galleryDataTool.InsertImage(_bytes, filename, "Screenshots");
- }
- catch (Exception e)
- {
- NRDebugger.Error("[TakePicture] Save picture faild!");
- throw e;
- }
- }
- }
- }
|