首页 > 解决方案 > Unity PhotoCapture 返回“没有可用的捕获设备。” Hololens2 上的错误

问题描述

我想在 Hololens2 中拍照或获取流视频,我使用 Unity:2020.3.2f1 平台:UWP。

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

但是,当我在 Hololens2 设备上测试此示例时,我收到两个错误,例如:“提供的流编号无效。” “没有可用的捕获设备。”

这是我的 Unity 项目设置: 在此处输入图像描述 在此处 输入图像描述

我还启用了 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();
}

}

标签: unity3dhololens

解决方案


请创建一个新的 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");
    }
}

推荐阅读