我正在尝试将捕获的照片保存到 HoloLens 上的磁盘。基于此示例,尝试在 Unity 中保存照片时出现以下异常错误。有一段时间我无法获得正确的目录来保存文件,现在我想我终于让那部分工作了(请参阅下面的 getFolderPath 函数)。但是,现在我得到下面引用的这个异常。有想法该怎么解决这个吗?
我正在使用 Origami 示例代码,并且我只是将此脚本 PhotoCaptureTest.cs 附加到 Unity 中的 OrigamiCollection 游戏对象。有谁知道如何解决这个问题或者我如何更容易地调试它?
抛出异常:UnityEngine.dll 中的“System.NullReferenceException” NullReferenceException:对象引用未设置为对象的实例。在 UnityEngine.VR.WSA.WebCam.PhotoCapture.$Invoke9(Int64 实例,Int64* args) 在 UnityEngine.Internal.$MethodUtility.InvokeMethod( Int64 实例,Int64* args,IntPtr 方法)(文件名:行:0)
using UnityEngine;
using System.Collections;
using UnityEngine.VR.WSA.WebCam;
using System.Linq;
using Windows.Storage;
using System;
using System.IO;
public class PhotoCaptureTest : MonoBehaviour {
PhotoCapture photoCaptureObject = null;
string folderPath = "";
bool haveFolderPath = false;
// Use this for initialization
void Start ()
{
getFolderPath();
while (!haveFolderPath)
{
Debug.Log("Waiting for folder path...");
}
Debug.Log("About to call CreateAsync");
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
Debug.Log("Called CreateAsync");
}
// Update is called once per frame
void Update () {
}
async void getFolderPath()
{
StorageLibrary myPictures = await Windows.Storage.StorageLibrary.GetLibraryAsync(Windows.Storage.KnownLibraryId.Pictures);
Windows.Storage.StorageFolder savePicturesFolder = myPictures.SaveFolder;
Debug.Log("savePicturesFolder.Path is " + savePicturesFolder.Path);
folderPath = savePicturesFolder.Path;
haveFolderPath = true;
}
void OnPhotoCaptureCreated(PhotoCapture captureObject)
{
photoCaptureObject = captureObject;
Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
CameraParameters c = new CameraParameters();
c.hologramOpacity = 0.0f;
c.cameraResolutionWidth = cameraResolution.width;
c.cameraResolutionHeight = cameraResolution.height;
c.pixelFormat = CapturePixelFormat.BGRA32;
captureObject.StartPhotoModeAsync(c, false, OnPhotoModeStarted);
}
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
photoCaptureObject.Dispose();
photoCaptureObject = null;
}
private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
string filename = string.Format(@"\CapturedImage{0}_n.jpg", Time.time);
string filePath = folderPath + filename;
string currentDir = Directory.GetCurrentDirectory();
Debug.Log("Current working direcotry is " + currentDir);
Debug.Log("Saving photo to " + filePath);
try
{
photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
}
catch (System.ArgumentException e)
{
Debug.LogError("System.ArgumentException:\n" + e.Message);
}
}
else
{
Debug.LogError("Unable to start photo mode!");
}
}
void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result)
{
if (result.success)
{
Debug.Log("Saved Photo to disk!");
photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
else
{
Debug.Log("Failed to save Photo to disk");
}
}
}