Esempio n. 1
0
        /// <summary>
        /// Stop video recording
        /// </summary>
        /// <returns></returns>
        private async Task StopVideoRecordingAsync()
        {
            try
            {
                await _mediaCapture.StopRecordAsync();

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => PhotoButton.IsEnabled = false);

                if (_lowLagPhotoCapture != null)
                {
                    await _lowLagPhotoCapture.FinishAsync();

                    _lowLagPhotoCapture = null;
                }
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => PhotoButton.IsEnabled = true);
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("StopVideoRecording did not complete: {0}", ex.Message);
                Debug.Write(errorMessage);
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    await new MessageDialog(errorMessage).ShowAsync();
                });
            }
        }
        private async Task CleanupCameraAsync()
        {
            if (mediaCapture != null)
            {
                if (isPreviewing)
                {
                    await mediaCapture.StopPreviewAsync();
                }

                await MainPage.runOnUIThread(async() =>
                {
                    if (lowLagCapture != null)
                    {
                        await lowLagCapture.FinishAsync();
                    }
                    previewControl.Source = null;
                    if (displayRequest != null)
                    {
                        displayRequest.RequestRelease();
                    }

                    mediaCapture.Dispose();
                });
            }
            isPreviewing = false;
        }
        /// <summary>
        /// Takes the camera output and displays the Clarifai predictions on the pane.
        /// </summary>
        /// <returns>a task</returns>
        private async Task RunPredictionsAndDisplayResponse()
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                try
                {
                    if (_predictionApi == null)
                    {
                        return;
                    }

                    LowLagPhotoCapture lowLagCapture =
                        await _mediaCapture.PrepareLowLagPhotoCaptureAsync(
                            ImageEncodingProperties.CreateUncompressed(MediaPixelFormat
                                                                       .Bgra8));
                    CapturedPhoto capturedPhoto   = await lowLagCapture.CaptureAsync();
                    SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;
                    byte[] data = await EncodedBytes(softwareBitmap,
                                                     BitmapEncoder.JpegEncoderId);

                    try
                    {
                        ConceptsTextBlock.Text = await _predictionApi
                                                 .PredictConcepts(data, _selectedModel);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageToUser("Error: " + ex.Message);
                    }

                    try
                    {
                        (double camWidth, double camHeight) = CameraOutputDimensions();

                        List <Rect> rects = await _predictionApi.PredictFaces(data,
                                                                              camWidth, camHeight);
                        CameraGrid.Children.Clear();
                        foreach (Rect r in rects)
                        {
                            CameraGrid.Children.Add(CreateGuiRectangle(r, camWidth, camHeight));
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessageToUser("Error: " + ex.Message);
                    }

                    await lowLagCapture.FinishAsync();
                }
                catch (COMException) // This is thrown when application exits.
                {
                    // No need to handle this since the application is exiting.
                }
            });
Esempio n. 4
0
 async private void CloseLagPhotoCapture_Click(object sender, RoutedEventArgs e)
 {
     // Release the LowLagPhotoCapture object and resources
     await lowLagCaptureMgr.FinishAsync();
 }
Esempio n. 5
0
 public void Dispose()
 {
     _capture.FinishAsync().GetResults();
     _mediaCapture.StopPreviewAsync();
     _mediaCapture.Dispose();
 }
Esempio n. 6
0
        private async void Camera_Click(object sender, RoutedEventArgs e)
        {
            if (isPreviewing)
            {
                CameraControl.Visibility = Visibility.Collapsed;
                LowLagPhotoCapture lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(
                    ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));

                CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync();

                SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;
                if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                    softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
                {
                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap,
                                                            BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }

                var source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(softwareBitmap);

                Job j = DataContext as Job;
                if (j != null)
                {
                    j.Photo = softwareBitmap;
                }

                await lowLagCapture.FinishAsync();
                await CleanupCameraAsync();

                isPreviewing = false;
            }
            else
            {
                CameraControl.Visibility = Visibility.Visible;

                try
                {
                    MediaCaptureInitializationSettings mediaInitSettings =
                        new MediaCaptureInitializationSettings();

                    DeviceInformationCollection devices =
                        await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                    foreach (var device in devices)
                    {
                        EnclosureLocation loc = device.EnclosureLocation;
                        Debug.WriteLine($"Location: {loc.Panel.ToString()} ID: {device.Id}");
                        if (sender is Button &&
                            ((Button)sender).Tag.ToString() == loc.Panel.ToString())
                        {
                            mediaInitSettings.VideoDeviceId        = device.Id;
                            mediaInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
                            mediaInitSettings.PhotoCaptureSource   = PhotoCaptureSource.VideoPreview;


                            break;
                        }
                    }

                    mediaCapture = new MediaCapture();
                    await mediaCapture.InitializeAsync(mediaInitSettings);

                    var resolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(
                        MediaStreamType.Photo).Select(x => x as VideoEncodingProperties);
                    var minRes = resolutions.OrderBy(x => x.Height * x.Width).FirstOrDefault();
                    await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(
                        MediaStreamType.VideoPreview, minRes);

                    PreviewControl.Source = mediaCapture;
                    await mediaCapture.StartPreviewAsync();

                    isPreviewing = true;

                    displayRequest.RequestActive();
                    DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
                }
                catch (UnauthorizedAccessException)
                {
                    // This will be thrown if the user denied access to the camera in privacy settings
                    Debug.WriteLine("The app was denied access to the camera");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
                }
            }
        }
Esempio n. 7
0
 public void Dispose()
 {
     Task.Run(async() => { await _lowLagCapture.FinishAsync(); });
     _mediaCapture.Dispose();
 }