我编程使用Unity的HoloLens的应用程序。在应用程序,我希望有一个按钮,点击时记录的图像。为了拍照我尝试使用Unitys捕捉(如下所述: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被启用作为输入)
正如你所说,你需要调用PhotoCapture.CreateAsync
你得到填充图像捕捉之前。
你可以得到一个代码示例here和下面的代码片段:
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);
}
该photoCaptureObject需要被实例化一个试图访问其决议之前。