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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值