简体   繁体   中英

Knowing when MediaCapture is previewing

I'm making a simple photo app using MediaCapture . I want the app to automatically start a countdown as soon as the webcam has finished initiating and is showing the preview.. Everything is working great, except the countdown is starting before the camera preview appears. Here is the gist of my code...

mediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();
previewElement.Source = mediaCapture;
await mediaCapture.StartPreviewAsync();
startCountdown();

xaml:

<CaptureElement x:Name="previewElement"  Visibility="Visible" FlowDirection="RightToLeft"/>

Is there a way I can check when the camera has fully initiated and the preview is showing an image?

On my search for an answer I stumbled across effects and discovered MediaCapture has builtin face detection . When a face is detected it's safe to say the camera is initiated.

//setup face detection
var definition = new FaceDetectionEffectDefinition();
definition.SynchronousDetectionEnabled = false;
definition.DetectionMode = FaceDetectionMode.HighPerformance;
faceDetection = (FaceDetectionEffect)await mediaCapture.AddVideoEffectAsync(definition, MediaStreamType.Photo);
faceDetection.DesiredDetectionInterval = TimeSpan.FromMilliseconds(33);
faceDetection.FaceDetected += FaceDetection_FaceDetected;
faceDetection.Enabled = true;

...

private async void FaceDetection_FaceDetected(FaceDetectionEffect sender, FaceDetectedEventArgs args) {

    if(args.ResultFrame.DetectedFaces.Count() > 0) {
        Debug.WriteLine(string.Format("Detected {0} faces", args.ResultFrame.DetectedFaces.Count()));
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => HighlightDetectedFaces(args.ResultFrame.DetectedFaces));
        if (!countdownStarted)
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => startCountdown());
    }
}

Though it's not an answer to my question, it's a solution to my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM