【问题标题】:SupportedResolutions of Unitys PhotoCapture is emptyUnitys PhotoCapture 的 SupportedResolutions 为空
【发布时间】:2018-05-02 07:49:49
【问题描述】:

我正在使用 Unity 为 HoloLens 编写应用程序。在应用程序中,我想要一个按钮,单击该按钮会记录图像。为了拍照,我尝试使用 Unitys PhotoCapture(如此处所述:https://docs.unity3d.com/560/Documentation/ScriptReference/VR.WSA.WebCam.PhotoCapture.html 或此处:https://docs.microsoft.com/en-us/windows/mixed-reality/locatable-camera-in-unity)。 当我运行代码并单击按钮时,出现以下错误:System.InvalidOperationException: 'Sequence contains no elements' 关于以下行:cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); 通过调试我发现SupportedResolutions 为空。我该如何解决这个问题?

其余代码:

using System.Collections;
using System.Collections.Generic;
using System.Linq;

using UnityEngine;
using UnityEngine.XR.WSA.WebCam;
using UnityEngine.XR.WSA.Input;

using HoloToolkit.Unity.InputModule;

public class Record : MonoBehaviour, IInputClickHandler {

    PhotoCapture photoCaptureObject = null;
    Resolution cameraResolution;

    void Start () {}

    public void OnInputClicked(InputClickedEventData eventData)
    {
        cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
        // Create a PhotoCapture object and so on
    }

(在 Unity 中启用了麦克风和网络摄像头作为输入)

【问题讨论】:

    标签: c# unity3d hololens


    【解决方案1】:

    如您所说,您需要先致电PhotoCapture.CreateAsync,然后才能填充 PhotoCapture。

    您可以在下面获取代码示例here 和代码sn-p:

    private void Start()
    {
        PhotoCapture.CreateAsync(false, this.OnPhotoCreated);
    }
    
    // This method store the PhotoCapture object just created and retrieve the high quality
    // available for the camera and then request to start capturing the photo with the
    // given camera parameters.
    private void OnPhotoCreated(PhotoCapture captureObject)
    {    
        Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
    
        CameraParameters c = new CameraParameters()
        {
            hologramOpacity = 0.0f,
            cameraResolutionWidth = cameraResolution.width,
            cameraResolutionHeight = cameraResolution.height,
            pixelFormat = CapturePixelFormat.BGRA32
        };
    
    //    captureObject.StartPhotoModeAsync(c, this.OnPhotoModeStarted);
    }
    

    【讨论】:

      【解决方案2】:

      在尝试访问其分辨率之前,需要先实例化 photoCaptureObject。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多