private void InitializeSettings() { settings = new MediaCaptureInitializationSettings(); settings.MediaCategory = MediaCategory.Speech; settings.AudioProcessing = AudioProcessing.Default; settings.MemoryPreference = MediaCaptureMemoryPreference.Auto; settings.SharingMode = MediaCaptureSharingMode.SharedReadOnly; settings.StreamingCaptureMode = m_isVideo ? StreamingCaptureMode.AudioAndVideo : StreamingCaptureMode.Audio; }
public async Task InitializeCameraAsync() { if (MediaCapture == null) { // daj sve devices, slicno kao i serial var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); // pokusace prvo pozadinsku kameru mobitela DeviceInformation cameraDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); // Ako je ne nadje prva kamera koja ima onda se uzima, tj spojena usb kamera cameraDevice = cameraDevice ?? allVideoDevices.FirstOrDefault(); //negdje zabiljeziti statuse kad kamera ne radi ili varijabla ili exception, sta je vise pogodno if (cameraDevice == null) { internalStatus = "No camera device found."; return; } //Inicijalizacija api za mediacapture MediaCapture = new MediaCapture(); var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; try { //incijalizirati kameru await MediaCapture.InitializeAsync(mediaInitSettings); _isInitialized = true; } catch (UnauthorizedAccessException) { internalStatus = ("The app was denied access to the camera"); } catch (Exception ex) { internalStatus = ("Exception when initializing MediaCapture with " + cameraDevice.Id + ex.ToString()); } // Ako se incijalizirala, prikazati preview u preview kontroli if (_isInitialized) { // Kako baratati sa naopakim kamerama, da li prednju ili zadnju kameru na mobitelu treba flipati if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { _externalCamera = true; } else { _externalCamera = false; _mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } await StartPreviewAsync(); } } }
public IAsyncOperation <List <CaptureCapability> > GetVideoCaptureCapabilities() { MediaDevice device = this; return(Task.Run(async() => { var settings = new MediaCaptureInitializationSettings() { VideoDeviceId = device.Id, MediaCategory = MediaCategory.Communications, }; using (var capture = new MediaCapture()) { await capture.InitializeAsync(settings); var caps = capture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.VideoRecord); var arr = new List <CaptureCapability>(); foreach (var cap in caps) { if (cap.Type != "Video") { continue; } var videoCap = (VideoEncodingProperties)cap; if (videoCap.FrameRate.Denominator == 0 || videoCap.FrameRate.Numerator == 0 || videoCap.Width == 0 || videoCap.Height == 0) { continue; } var captureCap = new CaptureCapability() { Width = videoCap.Width, Height = videoCap.Height, FrameRate = videoCap.FrameRate.Numerator / videoCap.FrameRate.Denominator, }; captureCap.FrameRateDescription = $"{captureCap.FrameRate} fps"; captureCap.ResolutionDescription = $"{captureCap.Width} x {captureCap.Height}"; /*captureCap.PixelAspectRatio = new MediaRatio() * { * Numerator = videoCap.PixelAspectRatio.Numerator, * Denominator = videoCap.PixelAspectRatio.Denominator, * };*/ captureCap.FullDescription = $"{captureCap.ResolutionDescription} {captureCap.FrameRateDescription}"; arr.Add(captureCap); } return arr.GroupBy(o => o.FullDescription).Select(o => o.First()).ToList(); } }).AsAsyncOperation()); }
private async Task ToggleCameraState() { if (mediaCapture==null) { try { MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.VideoDeviceId = cameraCollection[nextCamera].Id; mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(settings); // Docs say must be from UI thread Log("MediaCapture Initialized"); displayRequest.RequestActive(); DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; } catch (UnauthorizedAccessException) { cameraStatus.StatusText = "Permission denied"; Log("Camera access denied", LoggingLevel.Error); mediaCapture.Dispose(); mediaCapture = null; return; } } if (isPreviewing) { await CleanupAsync(); cameraStatus.StatusColor = Colors.Gray; cameraStatus.StatusText = "Not connected"; isPreviewing = false; nextCamera++; if (nextCamera >= cameraCollection.Count) { nextCamera = 0; } } else { try { cameraPreview.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); isPreviewing = true; Log("Viewport preview started"); cameraStatus.StatusColor = Colors.Green; cameraStatus.StatusText = cameraCollection[nextCamera].Name; } catch (System.IO.FileLoadException) { cameraStatus.StatusText = "Camera in use"; Log("Unable to obtain control of camera", LoggingLevel.Error); } } }
private async Task <bool> CheckDeviceAccessAsync(bool audio, ChatRecordMode mode) { // For some reason, as far as I understood, CurrentStatus is always Unspecified on Xbox if (string.Equals(AnalyticsInfo.VersionInfo.DeviceFamily, "Windows.Xbox")) { return(true); } var access = DeviceAccessInformation.CreateFromDeviceClass(audio ? DeviceClass.AudioCapture : DeviceClass.VideoCapture); if (access.CurrentStatus == DeviceAccessStatus.Unspecified) { MediaCapture capture = null; try { capture = new MediaCapture(); var settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = mode == ChatRecordMode.Video ? StreamingCaptureMode.AudioAndVideo : StreamingCaptureMode.Audio; await capture.InitializeAsync(settings); } catch { } finally { if (capture != null) { capture.Dispose(); capture = null; } } return(false); } else if (access.CurrentStatus != DeviceAccessStatus.Allowed) { var message = audio ? mode == ChatRecordMode.Voice ? Strings.Resources.PermissionNoAudio : Strings.Resources.PermissionNoAudioVideo : Strings.Resources.PermissionNoCamera; this.BeginOnUIThread(async() => { var confirm = await MessagePopup.ShowAsync(message, Strings.Resources.AppName, Strings.Resources.PermissionOpenSettings, Strings.Resources.OK); if (confirm == ContentDialogResult.Primary) { await Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app")); } }); return(false); } return(true); }
private async Task <bool> StartStreamingAsync() { bool result = true; PaintingCanvas.Children.Clear(); try { MediaCaptureInitializationSettings initializationSettings = new MediaCaptureInitializationSettings(); if (_localSettings.Values["CameraId"] == null) { ShowAlertHelper.ShowDialog("Cannot get your CamreaId, plase check your setting."); } _cameraId = _localSettings.Values["CameraId"].ToString(); initializationSettings.VideoDeviceId = _cameraId; initializationSettings.StreamingCaptureMode = StreamingCaptureMode.Video; var cp = (CameraPosition)_localSettings.Values["CameraPosition"]; switch (cp) { case CameraPosition.Front: CameraPreview.FlowDirection = FlowDirection.RightToLeft; PaintingCanvas.FlowDirection = FlowDirection.RightToLeft; break; case CameraPosition.Back: CameraPreview.FlowDirection = FlowDirection.LeftToRight; PaintingCanvas.FlowDirection = FlowDirection.LeftToRight; break; default: break; } _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(initializationSettings); _mediaCapture.Failed += _mediaCapture_Failed; CameraPreview.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); _properties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; } catch (UnauthorizedAccessException) { result = false; } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); result = false; } return(result); }
async void CaptureImage() { this.IsEnabled = false; try { var di = (DeviceInformation)listBox1.SelectedItem; if (di != null) { using (MediaCapture mediaCapture = new MediaCapture()) { mediaCapture.Failed += (s, e) => { MessageBox.Show("キャプチャできませんでした:" + e.Message, "Error", MessageBoxButton.OK); }; MediaCaptureInitializationSettings setting = new MediaCaptureInitializationSettings(); setting.VideoDeviceId = di.Id;//カメラ選択 setting.StreamingCaptureMode = StreamingCaptureMode.Video; await mediaCapture.InitializeAsync(setting); //調整しないと暗い場合があるので var vcon = mediaCapture.VideoDeviceController; vcon.Brightness.TrySetAuto(true); vcon.Contrast.TrySetAuto(true); var pngProperties = ImageEncodingProperties.CreatePng(); pngProperties.Width = (uint)image1.ActualWidth; pngProperties.Height = (uint)image1.ActualHeight; using (var randomAccessStream = new InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(pngProperties, randomAccessStream); randomAccessStream.Seek(0); //ビットマップにして表示 var bmp = new BitmapImage(); using (System.IO.Stream stream = System.IO.WindowsRuntimeStreamExtensions.AsStream(randomAccessStream)) { bmp.BeginInit(); bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.StreamSource = stream; bmp.EndInit(); } this.image1.Source = bmp; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } this.IsEnabled = true; }
/// <summary> /// Note that this method only checks the Settings->Privacy->Microphone setting, it does not handle /// the Cortana/Dictation privacy check. /// /// You should perform this check every time the app gets focus, in case the user has changed /// the setting while the app was suspended or not in focus. /// </summary> /// <returns>True, if the microphone is available.</returns> public async static Task <bool> RequestMicrophonePermission() { try { // Request access to the audio capture device. MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Audio; settings.MediaCategory = MediaCategory.Speech; MediaCapture capture = new MediaCapture(); await capture.InitializeAsync(settings); } catch (TypeLoadException) { // Thrown when a media player is not available. var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components are unavailable."); await messageDialog.ShowAsync(); return(false); } catch (UnauthorizedAccessException) { // Thrown when permission to use the audio capture device is denied. // If this occurs, show an error or disable recognition functionality. var dialog = new MessageDialog("Microphone permissions are allowed in app settings, but are disabled in system settings. Do you want to enable microphone access for this app?"); dialog.Commands.Add(new UICommand("No", (command) => { // get the setting for voice detection in the database, mark it as disabled, and update it in the database Setting voiceDetectionSetting = StoredProcedures.QuerySettingByName("Voice Activation"); voiceDetectionSetting.SelectOption("Disabled"); StoredProcedures.SelectOption(voiceDetectionSetting.SettingID, voiceDetectionSetting.GetSelectedOption().OptionID); var confirmationDialog = new MessageDialog("Ok, voice detection will be disabled. To Re-activate it, go to the settings page."); confirmationDialog.ShowAsync(); })); dialog.Commands.Add(new UICommand("Yes, take me to the system settings", (command) => Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-microphone")))); dialog.DefaultCommandIndex = 1; await dialog.ShowAsync(); return(false); } catch (Exception exception) { // Thrown when an audio capture device is not present. if (exception.HResult == NoCaptureDevicesHResult) { var messageDialog = new Windows.UI.Popups.MessageDialog("No Audio Capture devices are present on this system."); await messageDialog.ShowAsync(); return(false); } else { throw; } } return(true); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { DeviceInformationCollection oCameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); switch (oCameras.Count) { case 0: throw new Exception("No cameras found"); case 1: //Only front camera is available Camera = oCameras[(int)CameraLocation.Front]; break; default: //By default, we want the back camera Camera = oCameras[(int)CameraLocation.Back]; break; } MediaCaptureInitializationSettings oCameraSettings = new MediaCaptureInitializationSettings(); oCameraSettings.VideoDeviceId = Camera.Id; MediaCapture oCamera = new MediaCapture(); await oCamera.InitializeAsync(oCameraSettings); // resolution variables //int iMaxResolution = 0; //int iHeight = 0; //int iWidth = 0; //int iSelectedIndex = 0; //IReadOnlyList<IMediaEncodingProperties> oAvailableResolutions = oCamera.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); //// if no settings available, bail //if (oAvailableResolutions.Count < 1) return; //// list the different format settings //for (int i = 0; i < oAvailableResolutions.Count; i++) //{ // VideoEncodingProperties oProperties = (VideoEncodingProperties)oAvailableResolutions[i]; // if (oProperties.Width * oProperties.Height > iMaxResolution) // { // iHeight = (int)oProperties.Height; // iWidth = (int)oProperties.Width; // iMaxResolution = (int)oProperties.Width; // iSelectedIndex = i; // } //} //// set resolution //await oCamera.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, oAvailableResolutions[iSelectedIndex]); oMediaCapture.Source = oCamera; await oMediaCapture.Source.StartPreviewAsync(); }
public async Task StartStreamAsync(bool isForRealTimeProcessing = false) { try { if (captureManager == null || captureManager.CameraStreamState == CameraStreamState.Shutdown || captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (captureManager != null) { captureManager.Dispose(); } captureManager = new MediaCapture(); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); var allCameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); var selectedCamera = allCameras.FirstOrDefault(c => c.Name == SettingsHelper.Instance.CameraName); if (selectedCamera != null) { settings.VideoDeviceId = selectedCamera.Id; } await captureManager.InitializeAsync(settings); await SetVideoEncodingToHighestResolution(isForRealTimeProcessing); this.webCamCaptureElement.Source = captureManager; } if (captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (this.faceTracker == null) { this.faceTracker = await FaceTracker.CreateAsync(); } this.videoProperties = this.captureManager.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; await captureManager.StartPreviewAsync(); if (this.frameProcessingTimer != null) { this.frameProcessingTimer.Cancel(); frameProcessingSemaphore.Release(); } TimeSpan timerInterval = TimeSpan.FromMilliseconds(66); //15fps this.frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(ProcessCurrentVideoFrame), timerInterval); this.cameraControlSymbol.Symbol = Symbol.Camera; this.webCamCaptureElement.Visibility = Visibility.Visible; } } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Error starting the camera."); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; // // Doing all that video processing is too much for low-end phones like the Lumia 520 // Pick-and-choose which piece should run // VideoPreview.MediaFailed += VideoPreview_MediaFailed; //var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/video.cvmpilj.mjpg")); //var stream = await file.OpenAsync(FileAccessMode.Read); //var source = await HttpMjpegCaptureSource.CreateFromStreamAsync(stream, "myboundary"); var source = await HttpMjpegCaptureSource.CreateFromUriAsync("http://216.123.238.208/axis-cgi/mjpg/video.cgi?camera&resolution=640x480"); VideoPreview.SetMediaStreamSource(source.Source); var settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video }; await settings.SelectVideoDeviceAsync(VideoDeviceSelection.BackOrFirst); _capture = new MediaCapture(); await _capture.InitializeAsync(settings); var graphicsDevice = MediaGraphicsDevice.CreateFromMediaCapture(_capture); var previewProps = (VideoEncodingProperties)_capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); TextLog.Text += String.Format("Preview: {0} {1}x{2} {3}fps\n", previewProps.Subtype, previewProps.Width, previewProps.Height, previewProps.FrameRate.Numerator / (float)previewProps.FrameRate.Denominator); TextLog.Text += "Creating MediaSamplePresenter from SurfaceImageSource\n"; var image = new SurfaceImageSource((int)previewProps.Width, (int)previewProps.Height); ImagePreview.Source = image; _imagePresenter = ImagePresenter.CreateFromSurfaceImageSource(image, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height); TextLog.Text += "Creating MediaSamplePresenter from SwapChainPanel\n"; _swapChainPresenter = ImagePresenter.CreateFromSwapChainPanel( SwapChainPreview, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); TextLog.Text += "Creating MediaReader\n"; _mediaReader = await MediaReader.CreateFromMediaCaptureAsync(_capture, AudioInitialization.Deselected, VideoInitialization.Bgra8); TextLog.Text += "Starting video loop\n"; var ignore = Task.Run(() => VideoLoop()); }
//Record Voice By Microphone private async void InitializeAudioRecording() { _mediaCaptureManager = new MediaCapture(); var settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Audio; settings.MediaCategory = MediaCategory.Other; settings.AudioProcessing = AudioProcessing.Default; await _mediaCaptureManager.InitializeAsync(settings); }
private async void CreateMediaCapture() { MediaCapture = new MediaCapture(); MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Video; await MediaCapture.InitializeAsync(settings); CreateFrameReader(); }
private void InitCaptureSettings(string id) { // Set the capture setting captureInitSettings = null; captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings(); captureInitSettings.AudioDeviceId = id; captureInitSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.AudioAndVideo; }
/// <summary> /// Initializes this camera instance asynchronous. /// </summary> /// <returns>Whether the camera was initialized or not.</returns> public async Task <bool> InitializeCameraAsync() { Debug.WriteLine("InitializeCameraAync"); if (this.mediaCapture == null) { // Get available devices for capturing pictures var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (allVideoDevices.Count == 0) { Debug.WriteLine("There is not any camera device detected."); return(false); } // Get the desired camera by panel DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Panel.Back); // If there is no device mounted on the desired panel, return the first device found var cameraDevice = desiredDevice ?? allVideoDevices.FirstOrDefault(); this.mediaCapture = new MediaCapture(); // Register for a notification when video recording has reached the maximum time and when something goes wrong this.mediaCapture.RecordLimitationExceeded += this.MediaCapture_RecordLimitationExceeded; this.mediaCapture.Failed += this.MediaCapture_Failed; var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; try { await this.mediaCapture.InitializeAsync(settings); this.isInitialized = true; } catch (UnauthorizedAccessException) { Debug.WriteLine("the app was denied access to the camera."); } if (this.isInitialized) { this.allStreamResolutions = this.mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x => new StreamResolution(x)); var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); // Fall back to the local app storage if the Pictures Library is not available this.captureFolder = picturesLibrary.SaveFolder ?? ApplicationData.Current.LocalFolder; } } return(this.isInitialized); }
/// <summary> /// On desktop/tablet systems, users are prompted to give permission to use capture devices on a /// per-app basis. Along with declaring the microphone DeviceCapability in the package manifest, /// this method tests the privacy setting for microphone access for this application. /// Note that this only checks the Settings->Privacy->Microphone setting, it does not handle /// the Cortana/Dictation privacy check, however (Under Settings->Privacy->Speech, Inking and Typing). /// /// Developers should ideally perform a check like this every time their app gains focus, in order to /// check if the user has changed the setting while the app was suspended or not in focus. /// </summary> /// <returns>true if the microphone can be accessed without any permissions problems.</returns> public async static Task <bool> RequestMicrophonePermission() { try { // Request access to the microphone only, to limit the number of capabilities we need // to request in the package manifest. MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.Audio; settings.MediaCategory = MediaCategory.Speech; MediaCapture capture = new MediaCapture(); await capture.InitializeAsync(settings); } catch (TypeLoadException) { // On SKUs without media player (eg, the N SKUs), we may not have access to the Windows.Media.Capture // namespace unless the media player pack is installed. Handle this gracefully. var messageDialog = new Windows.UI.Popups.MessageDialog("Media player components are unavailable."); await messageDialog.ShowAsync(); return(false); } catch (UnauthorizedAccessException) { // The user has turned off access to the microphone. If this occurs, we should show an error, or disable // functionality within the app to ensure that further exceptions aren't generated when // recognition is attempted. return(false); } catch (Exception exception) { // This can be replicated by using remote desktop to a system, but not redirecting the microphone input. // Can also occur if using the virtual machine console tool to access a VM instead of using remote desktop. if (exception.HResult == NoCaptureDevicesHResult) { await new MessageDialog("Aparentemente, no hay micrófonos seleccionados.").ShowAsync(); return(false); } else if ((uint)exception.HResult == PrivacyStatementDeclinedHResult) { await new MessageDialog( "The privacy statement was declined." + "Go to Settings -> Privacy -> Speech, inking and typing, and ensure you" + "have viewed the privacy policy, and 'Get To Know You' is enabled.").ShowAsync(); // Open the privacy/speech, inking, and typing settings page. await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-accounts")); } else { throw; } } return(true); }
/// <summary> /// Initializes the camera. /// Will raise `CameraInit*` events. /// </summary> /// <returns>Task.</returns> public async Task InitializeCameraAsync(Size previewControlSize) { // Set ui-related values. this.previewControlSize = previewControlSize; // Ensure that the media capture hasn't been init, yet. if (MediaCapture != null) { return; } // Get all camera devices. var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); // Ensure there has been exactly one camera found. if (devices.Count != 1) { IsFaceDetectionControlAvailable = false; CameraInitFailed(this, new MessageEventArgs("No or more than one camera found. No face detection available.")); } // Create new media capture instance. MediaCapture = new MediaCapture(); // Setup callbacks. MediaCapture.Failed += MediaCapture_Failed; // Init the actual capturing. var settings = new MediaCaptureInitializationSettings { VideoDeviceId = devices[0].Id }; await MediaCapture.InitializeAsync(settings); // Updated preview properties from mediaCapture. previewProperties = MediaCapture .VideoDeviceController .GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; // Setup face detection var definition = new FaceDetectionEffectDefinition { SynchronousDetectionEnabled = false, DetectionMode = FaceDetectionMode.HighPerformance }; faceDetectionEffect = (FaceDetectionEffect)await MediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview); faceDetectionEffect.DesiredDetectionInterval = TimeSpan.FromMilliseconds(33); faceDetectionEffect.FaceDetected += FaceDetectionEffect_FaceDetected; // Operation was successful. IsFaceDetectionControlAvailable = true; CameraInitSucceeded(this, new MessageEventArgs("Face detection is now available.")); }
private async Task InitializeCameraAsync() { if (_mediaCapture == null) { // If there is no device mounted on the desired panel, return the first device found var cameraDevice = _cameraInfo ?? CameraList.FirstOrDefault(); if (cameraDevice == null) { return; } // Create MediaCapture and its settings _mediaCapture = new MediaCapture(); // Register for a notification when something goes wrong _mediaCapture.Failed += MediaCapture_Failed; var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id }; // Initialize MediaCapture try { await _mediaCapture.InitializeAsync(settings); _isInitialized = true; } catch (UnauthorizedAccessException) { } // If initialization succeeded, start the preview if (_isInitialized) { // Figure out where the camera is located if (cameraDevice.EnclosureLocation == null || cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Unknown) { // No information on the location of the camera, assume it's an external camera, not integrated on the device _externalCamera = true; } else { // Camera is fixed on the device _externalCamera = false; // Only mirror the preview if the camera is on the front panel _mirroringPreview = (cameraDevice.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Front); } await StartPreviewAsync(); } } }
// webcameraのスタート private async Task StartWebCameraAsync() { try { if (_captureManager == null || _captureManager.CameraStreamState == CameraStreamState.Shutdown || _captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (_captureManager != null) { // captureManagerのリソースを解放する _captureManager.Dispose(); } // LifeCam優先で、それがなければ取得した全カメラIDのリストから先頭のものを使用する MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); var allCameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); var selectedCamera = allCameras.FirstOrDefault(c => c.Name.Contains("LifeCam")) ?? allCameras.FirstOrDefault(); if (selectedCamera != null) { settings.VideoDeviceId = selectedCamera.Id; } _captureManager = new MediaCapture(); await _captureManager.InitializeAsync(settings); WebCamCaptureElement.Source = _captureManager; } if (_captureManager.CameraStreamState == CameraStreamState.NotStreaming) { if (_frameProcessingTimer != null) { _frameProcessingTimer.Cancel(); _frameProcessingSemaphore.Release(); } // 15fpsで動作 // 66milsecond毎にCreatePeriodicTimerの第一引き数に渡したHandlerで指定したメソッドが実行される TimeSpan timeInterval = TimeSpan.FromMilliseconds(66); _frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(ProcessCurrentVideoFrame), timeInterval); _videoProperties = _captureManager.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; await _captureManager.StartPreviewAsync(); WebCamCaptureElement.Visibility = Visibility.Visible; } } catch (Exception ex) { Debug.Write("6"); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); } }
public void PrintMicrophoneSample() { MediaCapture capture; IRandomAccessStream stream; const int BufferSize = 64000; bool recording; float volume = 100; capture = new MediaCapture(); stream = new InMemoryRandomAccessStream(); var captureInitSettings2 = new MediaCaptureInitializationSettings(); captureInitSettings2.StreamingCaptureMode = StreamingCaptureMode.Audio; capture.InitializeAsync(captureInitSettings2).AsTask().Wait(); capture.AudioDeviceController.VolumePercent = volume; MediaEncodingProfile profile = new MediaEncodingProfile(); AudioEncodingProperties audioProperties = AudioEncodingProperties.CreatePcm(16000, 1, 16); profile.Audio = audioProperties; profile.Video = null; profile.Container = new ContainerEncodingProperties() { Subtype = MediaEncodingSubtypes.Wave }; capture.StartRecordToStreamAsync(profile, stream).GetResults(); recording = true; // waste time for (int i = 0; i < 5; i++) { i = i * 232323 + 89;// WriteLine(i); } capture.StopRecordAsync().GetResults(); byte[] wav = new byte[stream.Size]; stream.Seek(0); stream.ReadAsync(wav.AsBuffer(), (uint)stream.Size, InputStreamOptions.None).GetResults(); int sum = 0; for (int i = 0; i < wav.Count(); i++) { sum += (int)wav[i]; } WriteLine((double)wav.Count() / sum); }
private async void OnButtonClicked(object sender, RoutedEventArgs e) { if (!_recording) { MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Audio }; _capture = new MediaCapture(); await _capture.InitializeAsync(settings); _capture.RecordLimitationExceeded += async(MediaCapture s) => { await new MessageDialog("Record limtation exceeded", "Error").ShowAsync(); }; _capture.Failed += async(MediaCapture s, MediaCaptureFailedEventArgs args) => { await new MessageDialog("Media capture failed: " + args.Message, "Error").ShowAsync(); }; _buffer = new InMemoryRandomAccessStream(); var profile = MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto); profile.Audio = AudioEncodingProperties.CreatePcm(16000, 1, 16); // Must be mono (1 channel) await _capture.StartRecordToStreamAsync(profile, _buffer); TheButton.Content = "Verify"; _recording = true; } else // Recording { if (_capture != null && _buffer != null && _id != null && _id != Guid.Empty) { await _capture.StopRecordAsync(); IRandomAccessStream stream = _buffer.CloneStream(); var client = new SpeakerVerificationServiceClient(_key); var response = await client.VerifyAsync(stream.AsStream(), _id); string message = String.Format("Result: {0}, Confidence: {1}", response.Result, response.Confidence); await new MessageDialog(message).ShowAsync(); _capture.Dispose(); _capture = null; _buffer.Dispose(); _buffer = null; } TheButton.Content = "Start"; _recording = false; } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { try { var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (cameras.Count < 1) { Error.Text = "No camera found, decoding static image"; await DecodeStaticResource(); return; } MediaCaptureInitializationSettings settings; if (cameras.Count == 1) { settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[0].Id }; // 0 => front, 1 => back } else { settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras[1].Id }; // 0 => front, 1 => back } await _mediaCapture.InitializeAsync(settings); VideoCapture.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); while (_result == null) { var photoStorageFile = await KnownFolders.PicturesLibrary.CreateFileAsync("scan.jpg", CreationCollisionOption.GenerateUniqueName); await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoStorageFile); var stream = await photoStorageFile.OpenReadAsync(); // initialize with 1,1 to get the current size of the image var writeableBmp = new WriteableBitmap(1, 1); writeableBmp.SetSource(stream); // and create it again because otherwise the WB isn't fully initialized and decoding // results in a IndexOutOfRange writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight); stream.Seek(0); writeableBmp.SetSource(stream); _result = ScanBitmap(writeableBmp); await photoStorageFile.DeleteAsync(StorageDeleteOption.PermanentDelete); } await _mediaCapture.StopPreviewAsync(); VideoCapture.Visibility = Visibility.Collapsed; CaptureImage.Visibility = Visibility.Visible; ScanResult.Text = _result.Text; } catch (Exception ex) { Error.Text = ex.Message; } }
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- public async Task StartPreviewAsync(DeviceInformation device) { if (previewRunning) { Logger.Info("Camera preview already started."); return; } previewRunning = true; UpdateViewState(Loading_State.Name); try { cameraCapture = new MediaCapture(); // Make sure we only access the video but not the audio stream for scanning QR Codes: MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings() { StreamingCaptureMode = StreamingCaptureMode.Video, MemoryPreference = MediaCaptureMemoryPreference.Cpu, VideoDeviceId = device.Id, MediaCategory = MediaCategory.Other }; await cameraCapture.InitializeAsync(settings); await SetupCameraAsync(cameraCapture, device); DisplayInformation.AutoRotationPreferences = DisplayOrientations.Portrait; displayRequest.RequestActive(); camera_ce.Source = cameraCapture; } catch (UnauthorizedAccessException) { Logger.Error("Camera access denied."); await OnErrorAsync(PreviewError.ACCESS_DENIED); previewRunning = false; return; } try { await cameraCapture.StartPreviewAsync(); UpdateViewState(Preview_State.Name); } catch (FileLoadException) { Logger.Error("Camera access denied. An other app has exclusive access. Try again later."); await StopPreviewAsync(); await OnErrorAsync(PreviewError.ACCESS_DENIED_OTHER_APP); previewRunning = false; } await StartFrameListenerAsync(); }
private async Task InitializeCameraAsync() { Debug.WriteLine("InitializeCameraAsync()"); if (MediaCapture == null) { // Find specified camera. //var cameraDeviceName = "Integrated Camera"; var cameraDeviceName = "C922 Pro Stream Webcam"; var cameraDevice = await FindCameraDeviceByDeviceNameAsync(cameraDeviceName); if (cameraDevice == null) { await ShowMessageToUser("Camera device not found", string.Format("Couldn't find Camera device '{0}'.", cameraDeviceName)); return; } MediaCapture = new MediaCapture(); try { // Initialize MediaCapture. var mediaInitSettings = new MediaCaptureInitializationSettings { VideoDeviceId = cameraDevice.Id, StreamingCaptureMode = StreamingCaptureMode.Video, MediaCategory = MediaCategory.Media, }; await MediaCapture.InitializeAsync(mediaInitSettings); IsMediaCaptureInitialized = true; } catch (UnauthorizedAccessException) { Debug.WriteLine("Couldn't initialize media capture. The app was denied access to the camera."); } // Focus InitializeFocusSetting(); // Zoom InitializeZoomSetting(); if (IsMediaCaptureInitialized) { await StartPreviewAsync(); } else { await ShowMessageToUser("UnauthorizedAccessException", "Couldn't initialize media capture. The app was denied access to the camera."); } } }
private async void InitVideoCapture() { ///摄像头的检测 var cameraDevice = await FindCameraDeviceByPanelAsync(Windows.Devices.Enumeration.Panel.Back); // var cameraDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back); if (cameraDevice == null) { Debug.WriteLine("No camera device found!"); return; } var settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video, //必须,否则截图的时候会很卡很慢 PhotoCaptureSource = PhotoCaptureSource.VideoPreview, MediaCategory = MediaCategory.Other, AudioProcessing = AudioProcessing.Default, VideoDeviceId = cameraDevice.Id }; _mediaCapture = new MediaCapture(); //初始化 await _mediaCapture.InitializeAsync(settings); VideoCapture.Source = _mediaCapture; await _mediaCapture.StartPreviewAsync(); var props = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); props.Properties.Add(RotationKey, 90); await _mediaCapture.SetEncodingPropertiesAsync(MediaStreamType.VideoPreview, props, null); var focusControl = _mediaCapture.VideoDeviceController.FocusControl; if (focusControl.Supported) { await focusControl.UnlockAsync(); var setting = new FocusSettings { Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange }; focusControl.Configure(setting); await focusControl.FocusAsync(); } _isPreviewing = true; _isInitVideo = true; InitVideoTimer(); }
public static async Task <VideoFrameProcessor> CreateAsync() { IReadOnlyList <MediaFrameSourceGroup> groups = await MediaFrameSourceGroup.FindAllAsync(); MediaFrameSourceGroup selectedGroup = null; MediaFrameSourceInfo selectedSourceInfo = null; // Pick first color source. foreach (var sourceGroup in groups) { foreach (var sourceInfo in sourceGroup.SourceInfos) { if (sourceInfo.MediaStreamType == MediaStreamType.VideoPreview && sourceInfo.SourceKind == MediaFrameSourceKind.Color) { selectedSourceInfo = sourceInfo; break; } } if (selectedSourceInfo != null) { selectedGroup = sourceGroup; break; } } // No valid camera was found. This will happen on the emulator. if (selectedGroup == null || selectedSourceInfo == null) { return(null); } MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.MemoryPreference = MediaCaptureMemoryPreference.Cpu; // Need SoftwareBitmaps for FaceAnalysis settings.StreamingCaptureMode = StreamingCaptureMode.Video; // Only need to stream video settings.SourceGroup = selectedGroup; MediaCapture mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(settings); MediaFrameSource selectedSource = mediaCapture.FrameSources[selectedSourceInfo.Id]; MediaFrameReader reader = await mediaCapture.CreateFrameReaderAsync(selectedSource); MediaFrameReaderStartStatus status = await reader.StartAsync(); // Only create a VideoFrameProcessor if the reader successfully started if (status == MediaFrameReaderStartStatus.Success) { return(new VideoFrameProcessor(mediaCapture, reader, selectedSource)); } return(null); }
public async void preview() { //Initialize media var media = new MediaCaptureInitializationSettings(); await this.capture.InitializeAsync(media); //set media capture element in UI to the capture device and start the stream this.Capture1.Source = capture; await this.capture.StartPreviewAsync(); }
private async void VoiceCaptureButton_Click(object sender, RoutedEventArgs e) { string output; //开始录音 if (VoiceRecordSym == true) { _memoryBuffer = new InMemoryRandomAccessStream(); VoiceCaptureButton.FontFamily = new FontFamily("Segoe UI"); VoiceCaptureButton.Content = "停止录音"; VoiceRecordSym = false; if (IsRecording) { throw new InvalidOperationException("Recording already in progress!"); } MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Audio }; _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(settings); //将录音文件存入_memoryBuffer里面 await _mediaCapture.StartRecordToStreamAsync(MediaEncodingProfile.CreateWav(AudioEncodingQuality.Auto), _memoryBuffer); IsRecording = true; } //停止录音 else { await _mediaCapture.StopRecordAsync(); IsRecording = false; VoiceCaptureButton.FontFamily = new FontFamily("Segoe MDL2 Assets"); VoiceCaptureButton.Content = "\xE1D6"; VoiceRecordSym = true; progessRing.IsActive = true; Input.IsReadOnly = true; //转换InMemoryRandomAccessStream成Stream Stream tempStream = WindowsRuntimeStreamExtensions.AsStreamForRead(_memoryBuffer.GetInputStreamAt(0)); using (var stream = new MemoryStream()) { tempStream.CopyTo(stream); VoiceToText voiceToText = new VoiceToText(); //传入VoiceToText函数 output = await voiceToText.ReadVoice(stream, "yue"); } //tempStream.Position = 0; progessRing.IsActive = false; Input.IsReadOnly = false; Input.Text += output; } }
private async Task InitMediaCapture() { m_mediaCapture = new MediaCapture(); var captureInitSettings = new MediaCaptureInitializationSettings(); captureInitSettings.StreamingCaptureMode = StreamingCaptureMode.Audio; await m_mediaCapture.InitializeAsync(captureInitSettings); m_mediaCapture.Failed += MediaCaptureOnFailed; m_mediaCapture.RecordLimitationExceeded += MediaCaptureOnRecordLimitationExceeded; }
/// <summary> /// Method to Initialize the Camera present in the device /// </summary> private async void InitializeMediaCapture() { try { DeviceInformationCollection devices = null; captureMgr = new MediaCapture(); devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); // Use the front camera if found one if (devices == null || devices.Count == 0) { isCameraFound = false; return; } DeviceInformation info = null; info = devices[0]; MediaCaptureInitializationSettings settings; settings = new MediaCaptureInitializationSettings { VideoDeviceId = info.Id }; // 0 => front, 1 => back await captureMgr.InitializeAsync(settings); VideoEncodingProperties resolutionMax = null; int max = 0; var resolutions = captureMgr.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); for (var i = 0; i < resolutions.Count; i++) { VideoEncodingProperties res = (VideoEncodingProperties)resolutions[i]; if (res.Width * res.Height > max) { max = (int)(res.Width * res.Height); resolutionMax = res; } } await captureMgr.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax); capturePreview.Source = captureMgr; isCameraFound = true; await captureMgr.StartPreviewAsync(); } catch (Exception ex) { MessageDialog dialog = new MessageDialog("Error while initializing media capture device: " + ex.Message); dialog.ShowAsync(); GC.Collect(); } }