Unity PhotoCapture return 'No capture devices are available.' Hololens2 上的错误

Unity PhotoCapture return 'No capture devices are available.' ERROR on Hololens2

我正在尝试在Hololens2中拍照或获取流视频,我使用的是Unity:2020.3.2f1 平台:UWP。

我正在尝试测试这个样本: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Windows.WebCam.PhotoCapture.html

但是当我在我的 Hololens2 设备上测试这个样本时,我得到了两个错误,比如: 'The stream number provided was invalid.' 'No capture devices are available.'

这是我的Unity项目设置: enter image description here enter image description here

我还启用了 Package.appxmanifest 麦克风和 WeCam,从 hololens2 卸载应用程序并重新构建并部署,但我仍然遇到两个错误。

有什么方法可以修复这个错误吗?

请原谅我的英语很糟糕


这是我当前的代码。 我想我只调用一个媒体 API.

using Microsoft.MixedReality.OpenXR;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.WebCam;


public class Capture : MonoBehaviour
{
/// <summary>
/// Allows this class to behave like a singleton
/// </summary>
public static Capture instance;

/// <summary>
/// Keeps track of tapCounts to name the captured images 
/// </summary>
private int tapsCount;

/// <summary>
/// PhotoCapture object used to capture images on HoloLens 
/// </summary>
private PhotoCapture photoCaptureObject = null;

/// <summary>
/// HoloLens class to capture user gestures
/// </summary>
private GestureRecognizer recognizer;

//Test

private HoloLensCameraStream.Resolution _resolution;
private VideoCapture _videoCapture;

void Awake() {
    instance = this;
}

// Start is called before the first frame update
void Start() {
    // Initialises user gestures capture 
    //recognizer = new GestureRecognizer();
    //recognizer.SetRecognizableGestures(GestureSettings.Tap);
    //recognizer.Tapped += TapHandler;
    //recognizer.StartCapturingGestures();
}

private void ExecuteImageCaptureAndAnalysis() {
    Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending
        ((res) => res.width * res.height).First();
    Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);

    PhotoCapture.CreateAsync(false, delegate (PhotoCapture captureObject)
    {
        photoCaptureObject = captureObject;

        CameraParameters c = new CameraParameters();
        c.hologramOpacity = 0.0f;
        c.cameraResolutionWidth = targetTexture.width;
        c.cameraResolutionHeight = targetTexture.height;
        c.pixelFormat = CapturePixelFormat.BGRA32;

        captureObject.StartPhotoModeAsync(c, delegate (PhotoCapture.PhotoCaptureResult result)
        {
            string filename = string.Format(@"CapturedImage{0}.jpg", tapsCount);
            string filePath = Path.Combine(Application.persistentDataPath, filename);

            // Set the image path on the FaceAnalysis class
            //FaceAnalysis.Instance.imagePath = filePath;
            photoCaptureObject.TakePhotoAsync
            (filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
        });
    });
    Debug.Log("Success!");
}

/// <summary>
/// Called right after the photo capture process has concluded
/// </summary>
void OnCapturedPhotoToDisk(PhotoCapture.PhotoCaptureResult result) {
    photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}

/// <summary>
/// Register the full execution of the Photo Capture. If successful, it will begin the Image Analysis process.
/// </summary>
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result) {
    photoCaptureObject.Dispose();
    photoCaptureObject = null;
}

public void CaptureImage() {
    ExecuteImageCaptureAndAnalysis();
}

}

请创建一个新的 Unity 项目并尝试以下代码,它适用于 HoloLens2:

public void StartCap()
{
    PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated);

}
private PhotoCapture photoCaptureObject = null;

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, OnPhotoModeStarted);
}
private void OnPhotoModeStarted(PhotoCapture.PhotoCaptureResult result)
{
    if (result.success)
    {
        string filename = string.Format(@"CapturedImage{0}_n.jpg", Time.time);
        string filePath = System.IO.Path.Combine(Application.persistentDataPath, filename);

        photoCaptureObject.TakePhotoAsync(filePath, PhotoCaptureFileOutputFormat.JPG, OnCapturedPhotoToDisk);
    }
    else
    {
        Debug.LogError("Unable to start photo mode!");
    }
}
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
    photoCaptureObject.Dispose();
    photoCaptureObject = null;
}

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");
    }
}

好的,我想问题已经解决了

我尝试了代码形式 'Hernando - MSFT' 但它仍然 return 这两个错误,但是当我打开 Hololens Windows 设备门户并打开项目文件夹时我可以看到图片!!!

我也测试了我的原始代码,它也可以工作。

不知道为什么拍照时总是return这两个错误,但我认为它没有攻击拍照功能。 enter image description here