我试图将拍摄的照片保存到HoloLens上的磁盘上。尝试根据this example在Unity中保存照片时,我收到以下异常错误。有一段时间我无法获得正确的目录来保存文件,现在我想我终于让那部分工作了(参见下面的getFolderPath函数)。但是,现在我得到了下面引用的这个例外。关于如何解决这个问题的任何想法?
我正在使用Origami示例代码,我只是将此脚本PhotoCaptureTest.cs附加到Unity中的OrigamiCollection游戏对象。有谁知道如何解决这个问题或者我如何更容易地调试它?
抛出异常:' System.NullReferenceException'在UnityEngine.dll中 NullReferenceException:对象引用未设置为的实例 宾语。在 UnityEngine.VR.WSA.WebCam.PhotoCapture.InvokeOnCapturedPhotoToDiskDelegate(OnCapturedToDiskCallback 回调,Int64 hResult)at 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");
}
}
}
答案 0 :(得分:0)
确保在保存文件之前将Visual Studio附加到unity并设置断点,并检查引用。我怀疑它即将到来null
,同时确保在您的manifest
中启用 WebCam +照片支持,这可能会导致null。
答案 1 :(得分:0)
我发现捕获和保存照片的代码应该是正确的,我遇到了和你一样的错误。
为避免错误,我可以通过将目录更改为Application.persistentDataPath
来保存照片,即将Start()
修改为以下
// ...
void Start()
{
folderPath = Application.persistentDataPath;
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
}
// ...
然后可以通过路径UserFiles\LocalAppData\<APPNAME>\LocalState\
获取照片,该路径可以通过Hololens Device Portal的文件资源管理器找到。