2

我正在尝试将捕获的照片保存到 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");
        }
    }
}
4

2 回答 2

0

我发现您捕获和保存照片的代码应该是正确的,我遇到了与您相同的错误。

为了避免错误,我可以通过将目录更改为来保存照片Application.persistentDataPath,即修改Start()为以下

// ...
void Start()
{
    folderPath = Application.persistentDataPath;
    PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
}
// ...

然后可以在路径中找到该照片,该路径UserFiles\LocalAppData\<APPNAME>\LocalState\可以通过 Hololens 设备门户的文件资源管理器找到。

于 2016-11-02T08:10:42.570 回答
0

请务必在保存文件之前将 Visual Studio 附加到 unity 并设置断点,并检查您的引用。我怀疑它即将出现null,还请确保在您的 中启用WebCam + Photos支持manifest,这也可能导致 null 。

于 2016-07-29T16:34:31.990 回答