SupportedResolutions of Unitys PhotoCapture is empty
Asked Answered
R

3

1

I am programming an application for the HoloLens using Unity. In the application I want a button, which records an image when clicked. In order to take a picture I try to use Unitys PhotoCapture (as described here: https://docs.unity3d.com/560/Documentation/ScriptReference/VR.WSA.WebCam.PhotoCapture.html or here: https://learn.microsoft.com/en-us/windows/mixed-reality/locatable-camera-in-unity). When I run the Code and click the button following error appears: System.InvalidOperationException: 'Sequence contains no elements' regarding following line: cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); Through Debugging I found that SupportedResolutions is empty. How can I fix this?

Rest of code:

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
    }

(Microphone and Webcam are enabled in Unity as input)

Ruano answered 2/5, 2018 at 7:49 Comment(0)
B
3

As you said, you need to call PhotoCapture.CreateAsync before you get the PhotoCapture populated.

You can get an code example here and a code snippet below:

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);
}
Barby answered 2/5, 2018 at 13:20 Comment(0)
R
0

The photoCaptureObject needs to be instantiated before one tries to access its resolutions.

Ruano answered 2/5, 2018 at 10:52 Comment(0)
N
0

Try add using System.Linq; in your code, it work for me.

Nautilus answered 23/9, 2022 at 2:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.