I am tring to take a picture or get a stream video in Hololens2 and I am used Unity: 2020.3.2f1 Platform: UWP.
I am trid to test this sample: https://docs.unity3d.com/2020.2/Documentation/ScriptReference/Windows.WebCam.PhotoCapture.html
But when I test this sample on my Hololens2 device I get two error like: 'The stream number provided was invalid.' 'No capture devices are available.'
This is my Unity project setting: enter image description here enter image description here
I also enalbe Package.appxmanifest microphone an WeCam,uninstall the app from hololens2 and build again and deploy but I still get the two error.
Is there anyway to fix this error?
Please forgive me for being terrible in my English.
This is my current code. I think I only invoke one Media 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();
}
}
CodePudding user response:
Please create a new Unity project and try the following code, it works for me in the 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");
}
}
CodePudding user response:
Ok,I think the question is down.
I trid the code form 'Hernando - MSFT' but it still return the two error,but when I open the Hololens Windows Device Portal and open the project folder I can see the picture!!!
I also test my origianl code,and it is work too.
I didn't know why it always return the two error when I capture a picture,but I think it didn't attack the capture function. enter image description here