private async void MediaControl_SoundLevelChanged(object sender, Object e) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { if (Windows.Media.MediaControl.SoundLevel != Windows.Media.SoundLevel.Muted) { ScenarioReset(); } else { if (m_bRecording) { ShowStatusMessage("Stopping Record on invisibility"); await m_mediaCaptureMgr.StopRecordAsync(); m_bRecording = false; EnableButton(true, "StartStopRecord"); } if (m_bPreviewing) { ShowStatusMessage("Stopping Preview on invisibility"); await m_mediaCaptureMgr.StopPreviewAsync(); m_bPreviewing = false; EnableButton(true, "StartPreview"); previewElement2.Source = null; } } }); }
private async void ScenarioClose() { try { if (m_bPhotoSequence) { ShowStatusMessage("Stopping PhotoSequence"); await m_photoSequenceCapture.FinishAsync(); m_photoSequenceCapture = null; m_bPhotoSequence = false; m_framePtr = null; } if (m_bPreviewing) { await m_capture.StopPreviewAsync(); m_bPreviewing = false; } if (m_capture != null) { previewElement4.Source = null; m_capture.Dispose(); } } catch (Exception exception) { ShowExceptionMessage(exception); } }
private async void ScenarioClose() { try { if (m_bRecording) { ShowStatusMessage("Stopping Record"); await m_mediaCaptureMgr.StopRecordAsync(); m_bRecording = false; } if (m_bPreviewing) { ShowStatusMessage("Stopping preview"); await m_mediaCaptureMgr.StopPreviewAsync(); m_bPreviewing = false; } if (m_mediaCaptureMgr != null) { ShowStatusMessage("Stopping Camera"); previewElement1.Source = null; m_mediaCaptureMgr.Dispose(); } } catch (Exception e) { ShowExceptionMessage(e); } }
// Stop the video capture private async void StopMediaCaptureSession() { await mediaCapture.StopRecordAsync(); recording = false; (App.Current as App).IsRecording = false; //stop the preview await mediaCapture.StopPreviewAsync(); }
// </SnippetMediaCaptureVideo_StopRecordCS> // </SnippetMediaCaptureVideo_CompleteCS> public async Task CleanupCaptureResources() { if (_recording && _mediaCapture != null) { await _mediaCapture.StopRecordAsync(); _recording = false; } if (_previewing && _mediaCapture != null) { await _mediaCapture.StopPreviewAsync(); _previewing = false; } if (_mediaCapture != null) { if (previewElement != null) { previewElement.Source = null; } _mediaCapture.Dispose(); } }
protected async override void OnNavigatedFrom(NavigationEventArgs e) { if (this.speechRecognizer != null) { if (isListening) { await this.speechRecognizer.ContinuousRecognitionSession.CancelAsync(); isListening = false; } speechRecognizer.ContinuousRecognitionSession.Completed -= ContinuousRecognitionSession_Completed; speechRecognizer.ContinuousRecognitionSession.ResultGenerated -= ContinuousRecognitionSession_ResultGenerated; speechRecognizer.StateChanged -= SpeechRecognizer_StateChanged; this.speechRecognizer.Dispose(); this.speechRecognizer = null; } await captureManager.StopPreviewAsync(); }
async private void btnChangeCamera_Click(object sender, RoutedEventArgs e) { AppBarButton button = sender as AppBarButton; if (button != null) { BitmapIcon icon = new BitmapIcon(); icon.UriSource = isBackCameraActive ? new Uri("ms-appx:///Assets/AppBar/BackCam.png"): new Uri("ms-appx:///Assets/AppBar/FrontCam.png"); button.Icon = icon; } if (isPreview) { await _captureManager.StopPreviewAsync(); } _captureManager.Dispose(); changeCamera(); initPhotoControl(); }
async private void StopCapturePreview_Click(object sender, RoutedEventArgs e) { await captureManager.StopPreviewAsync(); }
// Must be called on the UI thread private async Task DisposeCaptureAsync() { Preview.Source = null; if (autoFocus != null) { autoFocus.Dispose(); autoFocus = null; } MediaCapture mediaCapture; lock (this) { mediaCapture = this.mediaCapture; this.mediaCapture = null; } if (mediaCapture != null) { mediaCapture.Failed -= OnMediaCaptureFailed; await mediaCapture.StopPreviewAsync(); mediaCapture.Dispose(); } }
private async void ScanButton(object sender, object e) { string result = null; try { Waiter(true); var tcs = new TaskCompletionSource<object>(); StackPanel sp = new StackPanel() { Margin = new Thickness(20) }; #if WINDOWS_PHONE if (Device == null) { Windows.Foundation.Size initialResolution = new Windows.Foundation.Size(640, 480); wbm = new WriteableBitmap(640, 480); Device = await PhotoCaptureDevice.OpenAsync(CameraSensorLocation.Back, initialResolution); Device.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, Device.SensorLocation == CameraSensorLocation.Back ? Device.SensorRotationInDegrees : -Device.SensorRotationInDegrees); Device.SetProperty(KnownCameraGeneralProperties.AutoFocusRange, AutoFocusRange.Macro); Device.SetProperty(KnownCameraPhotoProperties.SceneMode, CameraSceneMode.Macro); Device.SetProperty(KnownCameraPhotoProperties.FocusIlluminationMode, FocusIlluminationMode.Off); } Rectangle previewRect = new Rectangle() { Height = 480, Width = 360 }; VideoBrush previewVideo = new VideoBrush(); previewVideo.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = (Device.SensorLocation == CameraSensorLocation.Back ? Device.SensorRotationInDegrees : -Device.SensorRotationInDegrees) }; previewVideo.SetSource(Device); previewRect.Fill = previewVideo; Grid combiner = new Grid() { Height = previewRect.Height, Width = previewRect.Width }; combiner.Children.Add(previewRect); Grid.SetColumnSpan(previewRect, 3); Grid.SetRowSpan(previewRect, 3); int windowSize = 100; combiner.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(previewRect.Height / 2 - windowSize) }); combiner.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(windowSize) }); combiner.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(previewRect.Height / 2 - windowSize) }); combiner.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(previewRect.Width / 2 - windowSize) }); combiner.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(windowSize) }); combiner.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(previewRect.Width / 2 - windowSize) }); var maskBrush = new SolidColorBrush(Color.FromArgb(80, 0, 0, 0)); Canvas v1 = new Canvas() { Background = maskBrush }; Canvas v2 = new Canvas() { Background = maskBrush }; Canvas v3 = new Canvas() { Background = maskBrush }; Canvas v4 = new Canvas() { Background = maskBrush }; combiner.Children.Add(v1); Grid.SetRow(v1, 0); Grid.SetColumnSpan(v1, 3); combiner.Children.Add(v2); Grid.SetRow(v2, 2); Grid.SetColumnSpan(v2, 3); combiner.Children.Add(v3); Grid.SetRow(v3, 1); Grid.SetColumn(v3, 0); combiner.Children.Add(v4); Grid.SetRow(v4, 1); Grid.SetColumn(v4, 2); sp.Children.Add(combiner); BackgroundWorker bw = new BackgroundWorker(); DateTime last = DateTime.Now; bw.WorkerSupportsCancellation = true; bw.DoWork += async (s2, e2) => { var reader = new BarcodeReader(); while (!bw.CancellationPending) { if (DateTime.Now.Subtract(last).TotalSeconds > 2) { await Device.FocusAsync(); last = DateTime.Now; } try { Device.GetPreviewBufferArgb(wbm.Pixels); Debug.WriteLine(DateTime.Now.Millisecond); var codeVal = reader.Decode(wbm); if (codeVal != null) { result = codeVal.Text; e2.Cancel = true; tcs.TrySetResult(null); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } } }; bw.RunWorkerAsync(); #else var cameras = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); if (cameras.Count < 1) { return; } var settings = new MediaCaptureInitializationSettings { VideoDeviceId = cameras.Last().Id }; var imageSource = new MediaCapture(); await imageSource.InitializeAsync(settings); CaptureElement previewRect = new CaptureElement() { Width = 640, Height = 360 }; previewRect.Source = imageSource; sp.Children.Add(previewRect); await imageSource.StartPreviewAsync(); bool keepGoing = true; Action a = null; a = async () => { if (!keepGoing) return; var stream = new InMemoryRandomAccessStream(); await imageSource.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); stream.Seek(0); var tmpBmp = new WriteableBitmap(1, 1); await tmpBmp.SetSourceAsync(stream); var writeableBmp = new WriteableBitmap(tmpBmp.PixelWidth, tmpBmp.PixelHeight); stream.Seek(0); await writeableBmp.SetSourceAsync(stream); Result _result = null; var barcodeReader = new BarcodeReader { // TryHarder = true, AutoRotate = true }; try { _result = barcodeReader.Decode(writeableBmp); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } stream.Dispose(); if (_result != null) { result = _result.Text; Debug.WriteLine(_result.Text); keepGoing = false; tcs.TrySetResult(null); } else { var x = RunOnUIThread(a); } }; await RunOnUIThread(a); #endif CustomMessageBox messageBox = new CustomMessageBox() { Title = "Scan Tag", Message = "", Content = sp, LeftButtonContent = "OK", RightButtonContent = "Cancel", IsFullScreen = false, }; messageBox.Unloaded += (s2, e2) => { tcs.TrySetResult(null); }; messageBox.Show(); await tcs.Task; messageBox.Dismiss(); #if WINDOWS_PHONE bw.CancelAsync(); #else keepGoing = false; await imageSource.StopPreviewAsync(); #endif Debug.WriteLine("result: '" + result + "'"); string loc = null; string building = null; string floor = null; string room = null; if (!string.IsNullOrEmpty(result)) { var s = result.Split(new char[] { '?' }, 2); if (s.Length == 2) { var paramList = s[1].Split('&') .Select(p => p.Split(new char[] { '=' }, 2)) .Where(p => p.Length == 2); loc = pick(paramList, "cp"); building = pick(paramList, "bld"); floor = pick(paramList, "flr"); room = pick(paramList, "rm"); } } GeoCoord? locVal = GeoCoord.Parse(loc); if (!string.IsNullOrEmpty(building) && !string.IsNullOrEmpty(floor)) { await ShowMap(RoomInfo.Parse(building + "/" + (room == null ? floor : room)), locVal); } } finally { Waiter(false); } }