private async void startDevice() { // CamView.Visibility = Visibility.Visible; try { await Task.Delay(TimeSpan.FromSeconds(3)); m_bReversePreviewRotation = false; m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); var chosenDevInfo = m_devInfoCollection[devListView.SelectedIndex]; settings.VideoDeviceId = chosenDevInfo.Id; if (chosenDevInfo.EnclosureLocation != null && chosenDevInfo.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back) { m_bRotateVideoOnOrientationChange = true; m_bReversePreviewRotation = false; } else if (chosenDevInfo.EnclosureLocation != null && chosenDevInfo.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front) { m_bRotateVideoOnOrientationChange = true; m_bReversePreviewRotation = true; } else { m_bRotateVideoOnOrientationChange = false; } await m_mediaCaptureMgr.InitializeAsync(settings); m_lowLagPhoto = await m_mediaCaptureMgr.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg()); // await Task.Delay(TimeSpan.FromSeconds(2)); //myGlobals.newProc(2); } catch (Exception ex) { var dialog = new MessageDialog(ex.ToString()); await dialog.ShowAsync(); //Debug.WriteLine(ex.ToString()); } }
internal async void btnStartDevice_Click(Object sender, Windows.UI.Xaml.RoutedEventArgs e) { try { btnStartDevice2.IsEnabled = false; m_bReversePreviewRotation = false; ShowStatusMessage("Starting device"); m_mediaCaptureMgr = new Windows.Media.Capture.MediaCapture(); var settings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); var chosenDevInfo = m_devInfoCollection[EnumedDeviceList2.SelectedIndex]; settings.VideoDeviceId = chosenDevInfo.Id; if (EnumedMicrophonesList2.SelectedIndex >= 0 && m_microPhoneInfoCollection.Count > 0) { var chosenMicrophoneInfo = m_microPhoneInfoCollection[EnumedMicrophonesList2.SelectedIndex]; settings.AudioDeviceId = chosenMicrophoneInfo.Id; } if (chosenDevInfo.EnclosureLocation != null && chosenDevInfo.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back) { m_bRotateVideoOnOrientationChange = true; m_bReversePreviewRotation = false; } else if (chosenDevInfo.EnclosureLocation != null && chosenDevInfo.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front) { m_bRotateVideoOnOrientationChange = true; m_bReversePreviewRotation = true; } else { m_bRotateVideoOnOrientationChange = false; } await m_mediaCaptureMgr.InitializeAsync(settings); if (m_mediaCaptureMgr.MediaCaptureSettings.VideoDeviceId != "" && m_mediaCaptureMgr.MediaCaptureSettings.AudioDeviceId != "") { btnStartPreview2.IsEnabled = true; ShowStatusMessage("Device initialized successful"); chkAddRemoveEffect.IsEnabled = true; chkAddRemoveEffect.IsChecked = false; m_mediaCaptureMgr.RecordLimitationExceeded += new Windows.Media.Capture.RecordLimitationExceededEventHandler(RecordLimitationExceeded); m_mediaCaptureMgr.Failed += new Windows.Media.Capture.MediaCaptureFailedEventHandler(Failed); if (!m_mediaCaptureMgr.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported) { radTakePhoto.IsEnabled = true; radRecord.IsEnabled = true; //choose TakePhoto Mode as defaul radTakePhoto.IsChecked = true; } else { //prepare lowlag photo, then prepare lowlag record m_lowLagPhoto = await m_mediaCaptureMgr.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg()); btnTakePhoto2.IsEnabled = true; m_bLowLagPrepared = true; PrepareLowLagRecordAsync(); //disable check options radTakePhoto.IsEnabled = false; radRecord.IsEnabled = false; } EnumerateSceneModeAsync(); } else { btnStartDevice2.IsEnabled = true; ShowStatusMessage("No VideoDevice/AudioDevice Found"); } } catch (Exception exception) { ShowExceptionMessage(exception); } }
private async void CameraInit() { try { if (_mediaCapture != null) { // Cleanup MediaCapture object CameraCleanup(); } // Task.Delay(TimeSpan.FromSeconds(10)).Wait(); Debug.WriteLine("Initing Video Capture... "); // Use default-ish initialization MediaCaptureInitializationSettings capInitSettings = new MediaCaptureInitializationSettings(); capInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video; _mediaCapture = new MediaCapture(); _mediaCapture.Failed += new MediaCaptureFailedEventHandler(CameraInit_Failed); var deviceList = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); foreach (DeviceInformation di in deviceList) { Debug.WriteLine("-> " + di.Id + " -> " + di.Name); } // capInitSettings.VideoDeviceId = MediaCapture. await _mediaCapture.InitializeAsync(capInitSettings); IReadOnlyList<IMediaEncodingProperties> resolutions = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview); VideoEncodingProperties bestRes = null; // search for minimal available frame rate and resolution. Debug.WriteLine("Available Resolutions: "); for (var i = 0; i < resolutions.Count; i++) { if (resolutions[i].GetType().Name != "VideoEncodingProperties") continue; var r = (VideoEncodingProperties) resolutions[i]; Debug.WriteLine(i.ToString() + " res: " + r.Width + "x" + r.Height + " @ " + r.FrameRate.Numerator + "/" + r.FrameRate.Denominator + " fps (" + r.Type + ": " + r.Subtype + ")"); if (r.FrameRate.Denominator != 1) continue; if (bestRes == null) { bestRes = r; continue; } if (r.FrameRate.Numerator <= bestRes.FrameRate.Numerator && r.Height*r.Width <= bestRes.Height*bestRes.Width && r.Height >= 400 && r.Subtype == "YUY2") { bestRes = r; } } await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, bestRes); _mediaCapture.VideoDeviceController.Focus.TrySetAuto(true); _LLPC = _mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg()) .GetAwaiter() .GetResult(); Debug.WriteLine("Using: " + bestRes.Width + "x" + bestRes.Height + " @ " + bestRes.FrameRate.Numerator + "/" + bestRes.FrameRate.Denominator + " fps"); PreviewElement.Source = _mediaCapture; PreviewElement.Stretch = Stretch.None; _mediaCapture.StartPreviewAsync().GetAwaiter().GetResult(); Debug.WriteLine("done."); } catch (Exception ex) { Debug.WriteLine("Unable to initialize camera for audio/video mode: " + ex.Message); } }