HoloLens-在Unity中捕获照片并将其保存到磁盘 [英] HoloLens -- Capturing Photo in Unity and Saving to Disk
问题描述
我正在尝试将捕获的照片保存到HoloLens的磁盘上.尝试基于在Unity中保存照片时遇到以下异常错误这个例子.有一阵子我无法找到正确的目录来保存文件,现在我想我终于可以使那部分工作了(请参阅下面的getFolderPath函数).但是,现在我在下面引用了此异常.有关如何解决此问题的任何想法?
I'm trying to save a captured photo to the disk on HoloLens. I'm getting the following exception error when trying to save a photo in Unity, based on this example. For a while I couldn't get the correct directory to save the file in, and now I think I've finally got that part working (see the getFolderPath function below). However, now I get this exception quoted below. Any ideas on how to fix this?
我正在使用Origami示例代码,并将此脚本PhotoCaptureTest.cs附加到Unity中的OrigamiCollection游戏对象.有人知道如何解决此问题,或者我如何可以更轻松地对其进行调试?
I'm using the Origami example code and I've simply attached this script PhotoCaptureTest.cs to the OrigamiCollection game object in Unity. Does anyone know how to fix this or how I can debug this more easily?
引发的异常:UnityEngine.dll中的'System.NullReferenceException' NullReferenceException:对象引用未设置为的实例 目的.在 UnityEngine.VR.WSA.WebCam.PhotoCapture.InvokeOnCapturedPhotoToDiskDelegate(OnCapturedToDiskCallback 回调,Int64 hResult)位于 UnityEngine.VR.WSA.WebCam.PhotoCapture.$ Invoke9(Int64实例,Int64 * args)在UnityEngine.Internal.$ MethodUtility.InvokeMethod(Int64 实例,Int64 *参数,IntPtr方法)(文件名:行:0)
Exception thrown: 'System.NullReferenceException' in UnityEngine.dll NullReferenceException: Object reference not set to an instance of an object. at UnityEngine.VR.WSA.WebCam.PhotoCapture.InvokeOnCapturedPhotoToDiskDelegate(OnCapturedToDiskCallback callback, Int64 hResult) at UnityEngine.VR.WSA.WebCam.PhotoCapture.$Invoke9(Int64 instance, Int64* args) at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method) (Filename: Line: 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");
}
}
}
推荐答案
我发现您捕获和保存照片的代码应该正确,并且遇到了与您相同的错误.
I find that your code to capture and save photos should be correct and I ran into the same error as you.
为避免该错误,我能够通过将目录更改为Application.persistentDataPath
来保存照片,即将Start()
修改为以下内容
To avoid the error, I was able to save photos by changing the directory to Application.persistentDataPath
, i.e., modifying Start()
to the following
// ...
void Start()
{
folderPath = Application.persistentDataPath;
PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);
}
// ...
然后可以在路径UserFiles\LocalAppData\<APPNAME>\LocalState\
上找到照片,可以通过Hololens设备门户的文件资源管理器找到该文件.
The photo is then available at the path UserFiles\LocalAppData\<APPNAME>\LocalState\
, which can be found through the Hololens Device Portal's File Explorer.
这篇关于HoloLens-在Unity中捕获照片并将其保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!