I am trying to take picture with the HoloLens so I've created a photo capture script that disables Vuforia when I say "Start" and takes pictures every 3 seconds until I say "Stop". Once I say "Stop" I enable Vuforia again. To enable/disbale Vuforia I am using "VuforiaBehaviour.Instance.enabled = true/false". The problem is that unless I delay Vuforia's initialization, I get the error "Failed capturing photo (hr = 0xC00D3704)". However if I delay Vuforia's initialization or disable VuforiaBehvaiour at the start of the script, then Vuforia doesn't work even when I enable it. I am using Vuforia 10.2.5 and Unity 2020.3. I've seen so many posts that say to just enable/disbale Vuforia but no matter what I do this doesn't work. Please, any help would be greatly appreciated!
Here is my code for anyone interested:
public class HololensPhotoCapture : MonoBehaviour { PhotoCapture photoCaptureObject = null; private bool goAgain = true; private float period = 0.0f; private bool takePics = false; //used for stopping and starting the picture taking process private KeywordRecognizer keywordRecognizerStart; private KeywordRecognizer keywordRecognizerStop;
void Start() { // Setup a keyword recognizer. List<string> keywordStart = new List<string>(); List<string> keywordStop = new List<string>(); keywordStart.Add("Start"); keywordStop.Add("Stop"); keywordRecognizerStart = new KeywordRecognizer(keywordStart.ToArray()); keywordRecognizerStop = new KeywordRecognizer(keywordStop.ToArray()); //keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized; keywordRecognizerStart.Start(); keywordRecognizerStop.Start();
}
// Handle the keyword "Start" to start taking pictures. private void KeywordRecognizer_OnPhraseRecognizedStart(PhraseRecognizedEventArgs args) { VuforiaBehaviour.Instance.enabled = false;
Debug.Log("Start recognized."); takePics = true; }
// Handle the keyword "Stop" to stop taking pictures. private void KeywordRecognizer_OnPhraseRecognizedStop(PhraseRecognizedEventArgs args) { Debug.Log("Stop recognized."); takePics = false;
VuforiaBehaviour.Instance.enabled = true; }
void TakePicture() { goAgain = false; PhotoCapture.CreateAsync(false, OnPhotoCaptureCreated); }
void Update() { keywordRecognizerStart.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognizedStart; keywordRecognizerStop.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognizedStop;
if (takePics == true) { if (period >= 3.0f) { if (goAgain == true) { Debug.Log("Saving another photo."); TakePicture(); } period = 0; } period += Time.deltaTime; } }
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); } void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result) { photoCaptureObject.Dispose(); photoCaptureObject = null; goAgain = true; } 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 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"); } } }
Hey there,
Hey there,
Hi Patrick,
Hi Patrick,
Hey there,
Hey there,
Hi Patrick,
Hi Patrick,
Will do!
Will do!
Have a good weekend!
Kind regards,
Patrick Scheper
Technical Community Manager