HoloLens 升级到RS4后前摄像头拍照储存

using HoloToolkit.Unity.InputModule;
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Assertions;


/// Manages taking and saving photos.
/// </summary>
/// 


public class PhotoManager : MonoBehaviour, IInputClickHandler
{
    /// <summary>
    /// Displays status information of the camera.
    /// </summary>
    public TextMesh Info;


    /// <summary>
    /// Actual camera instance.
    /// </summary>
    private UnityEngine.XR.WSA.WebCam.PhotoCapture capture;


    /// <summary>
    /// True, if the camera is ready to take photos.
    /// </summary>
    private bool isReady = false;


    /// <summary>
    /// The path to the image in the applications local folder.
    /// </summary>
    private string currentImagePath;


    /// <summary>
    /// The path to the users picture folder.
    /// </summary>
    private string pictureFolderPath;


    private void Awake()
    {
        Assert.IsNotNull(Info, "The PhotoManager requires a text mesh.");


        Info.text = "Camera off";
        UnityEngine.XR.WSA.WebCam.PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
        Info.text = " UnityEngine.XR.WSA.WebCam.PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);";
#if NETFX_CORE
        getPicturesFolderAsync();
#endif


    }




    private void Start() { }


#if NETFX_CORE


    private async void getPicturesFolderAsync() {
        Windows.Storage.StorageLibrary picturesStorage = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
        pictureFolderPath = picturesStorage.SaveFolder.Path;
    }


#endif


    private void OnPhotoCaptureCreated(UnityEngine.XR.WSA.WebCam.PhotoCapture captureObject)
    {
        Info.text = "OnPhotoCaptureCreated";
        capture = captureObject;


        Resolution resolution = UnityEngine.XR.WSA.WebCam.PhotoCapture.SupportedResolutions.OrderByDescending(res => res.width * res.height).First();


        UnityEngine.XR.WSA.WebCam.CameraParameters c = new UnityEngine.XR.WSA.WebCam.CameraParameters(UnityEngine.XR.WSA.WebCam.WebCamMode.PhotoMode);
        c.hologramOpacity = 0.0f;
        c.cameraResolutionWidth = resolution.width;
        c.cameraResolutionHeight = resolution.height;
        c.pixelFormat = UnityEngine.XR.WSA.WebCam.CapturePixelFormat.BGRA32;


        capture.StartPhotoModeAsync(c, OnPhotoModeStarted);
    }


    private void OnPhotoModeStarted(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result)
    {
        isReady = result.success;
        Info.text = "Camera ready";
    }




    /// <summary>
    /// Take a photo and save it to a temporary application folder.
    /// </summary>
    public void TakePhoto()
    {
        if (isReady)
        {
            string file = string.Format(@"Image_{0:yyyy-MM-dd_hh-mm-ss-tt}.jpg", DateTime.Now);
            currentImagePath = System.IO.Path.Combine(Application.persistentDataPath, file);


            capture.TakePhotoAsync(currentImagePath, UnityEngine.XR.WSA.WebCam.PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
            Info.text = currentImagePath;
        }
        else
        {
            Info.text = "The camera is not yet ready.";
        }
    }


    /// <summary>
    /// Stop the photo mode.
    /// </summary>
    public void StopCamera()
    {
        if (isReady)
        {
            capture.StopPhotoModeAsync(OnPhotoModeStopped);
        }
    }


    private void OnCapturedPhotoToDisk(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result)
    {
        if (result.success)
        {


#if NETFX_CORE
            try 
            {
                if(pictureFolderPath != null)
                {
                    System.IO.File.Move(currentImagePath, System.IO.Path.Combine(pictureFolderPath, "Camera Roll", System.IO.Path.GetFileName(currentImagePath)));
                    Info.text = "Saved photo in camera roll";
                }
                else 
                {
                    Info.text = "Saved photo to temp";
                }
            } 
            catch(Exception e) 
            {
               // Info.text = "Failed to move to camera roll";
            }
#else
            Info.text = "Saved photo";
            Debug.Log("Saved image at " + currentImagePath);
#endif


        }
        else
        {
            Info.text = "Failed to save photo";
            Debug.LogError(string.Format("Failed to save photo to disk ({0})", result.hResult));
        }
    }


    private void OnPhotoModeStopped(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result)
    {
        capture.Dispose();
        capture = null;
        isReady = false;


       Info.text = "Camera off";
    }


    public void OnInputClicked(InputClickedEventData eventData)
    {
        TakePhoto();
       


    }




}


//环境须为windows SDK 10.0.17134  unity需要20174.4.f1 vs2017

猜你喜欢

转载自blog.csdn.net/xiuluowangzi/article/details/80638354