none
Is it possible to send a frame from webcam to wcf service and How to ? RRS feed

  • Question

  • Hello,

    Is it possible to send a frame in continue from webcam to wcf service ? And is it the best way for a webcam streaming app to use WCF ?

    Otherwise, i have this code :

        public sealed partial class MainPage : Page
        {
            // MediaCapture and its state variables
            private MediaCapture _mediaCapture;
            private bool _isInitialized = false;
            private bool _isPreviewing = false;
    
            // Prevent the screen from sleeping while the camera is running
            private readonly DisplayRequest _displayRequest = new DisplayRequest();
    
            // For listening to media property changes
            private readonly SystemMediaTransportControls _systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
    
            public MainPage()
            {
                this.InitializeComponent();
    
                // Useful to know when to initialize/clean up the camera
                Application.Current.Suspending += Application_Suspending;
                Application.Current.Resuming += Application_Resuming;
            }
    
            private async void Application_Suspending(object sender, SuspendingEventArgs e)
            {
                // Handle global application events only if this page is active
                if (Frame.CurrentSourcePageType == typeof(MainPage))
                {
                    var deferral = e.SuspendingOperation.GetDeferral();
    
                    await CleanupCameraAsync();
    
                    deferral.Complete();
                }
            }
    
            private async void Application_Resuming(object sender, object e)
            {
                // Handle global application events only if this page is active
                if (Frame.CurrentSourcePageType == typeof(MainPage))
                {
                    await InitializeCameraAsync();
                }
            }
    
            protected override async void OnNavigatedTo(NavigationEventArgs e)
            {
                await InitializeCameraAsync();
            }
    
            protected override async void OnNavigatingFrom(NavigatingCancelEventArgs e)
            {
                // Handling of this event is included for completenes, as it will only fire when navigating between pages and this sample only includes one page
                await CleanupCameraAsync();
            }
    
            private async void SystemMediaControls_PropertyChanged(SystemMediaTransportControls sender, SystemMediaTransportControlsPropertyChangedEventArgs args)
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                {
                    // Only handle this event if this page is currently being displayed
                    if (args.Property == SystemMediaTransportControlsProperty.SoundLevel && Frame.CurrentSourcePageType == typeof(MainPage))
                    {
                        // Check to see if the app is being muted. If so, it is being minimized.
                        // Otherwise if it is not initialized, it is being brought into focus.
                        if (sender.SoundLevel == SoundLevel.Muted)
                        {
                            await CleanupCameraAsync();
                        }
                        else if (!_isInitialized)
                        {
                            await InitializeCameraAsync();
                        }
                    }
                });
            }
    
            private async void MediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
            {
                Debug.WriteLine("MediaCapture_Failed: (0x{0:X}) {1}", errorEventArgs.Code, errorEventArgs.Message);
    
                await CleanupCameraAsync();
    
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => GetPreviewFrameButton.IsEnabled = _isPreviewing);
            }
    
            private async Task InitializeCameraAsync()
            {
                Debug.WriteLine("InitializeCameraAsync");
    
                if (_mediaCapture == null)
                {
                    // Attempt to get the back camera if one is available, but use any camera device if not
                    var cameraDevice = await FindCameraDeviceAsync();
    
                    if (cameraDevice == null)
                    {
                        Debug.WriteLine("No camera device found!");
                        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)
                    {
                        Debug.WriteLine("The app was denied access to the camera");
                    }
    
                    // If initialization succeeded, start the preview
                    if (_isInitialized)
                    {
                        await StartPreviewAsync();
                    }
                }
            }
    
            private async Task StartPreviewAsync()
            {
                Debug.WriteLine("StartPreviewAsync");
    
                // Prevent the device from sleeping while the preview is running
                _displayRequest.RequestActive();
    
                // Register to listen for media property changes
                _systemMediaControls.PropertyChanged += SystemMediaControls_PropertyChanged;
    
                // Set the preview source in the UI and mirror it if necessary
                PreviewControl.Source = _mediaCapture;
    
                // Start the preview
                await _mediaCapture.StartPreviewAsync();
                _isPreviewing = true;
    
                // Enable / disable the button depending on the preview state
                GetPreviewFrameButton.IsEnabled = _isPreviewing;
            }
    
            private async Task StopPreviewAsync()
            {
                _isPreviewing = false;
                await _mediaCapture.StopPreviewAsync();
    
                // Use the dispatcher because this method is sometimes called from non-UI threads
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    PreviewControl.Source = null;
    
                    // Allow the device to sleep now that the preview is stopped
                    _displayRequest.RequestRelease();
    
                    GetPreviewFrameButton.IsEnabled = _isPreviewing;
                });
            }
    
            private async Task CleanupCameraAsync()
            {
                if (_isInitialized)
                {
                    if (_isPreviewing)
                    {
                        // The call to stop the preview is included here for completeness, but can be
                        // safely removed if a call to MediaCapture.Dispose() is being made later,
                        // as the preview will be automatically stopped at that point
                        await StopPreviewAsync();
                    }
    
                    _isInitialized = false;
                }
    
                if (_mediaCapture != null)
                {
                    _mediaCapture.Failed -= MediaCapture_Failed;
                    _mediaCapture.Dispose();
                    _mediaCapture = null;
                }
            }
    
            private static async Task<DeviceInformation> FindCameraDeviceAsync()
            {
                // Get available devices for capturing pictures
                var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
    
                // return the first device found
                return allVideoDevices.FirstOrDefault();
            }
    
            private async void GetPreviewFrameButton_Click(object sender, RoutedEventArgs e)
            {
                // If preview is not running, no preview frames can be acquired
                if (!_isPreviewing) return;
    
                await GetPreviewFrameAsSoftwareBitmapAsync();
            }
    
            private async Task GetPreviewFrameAsSoftwareBitmapAsync()
            {
                // Get information about the preview
                var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
    
                // Create the video frame to request a SoftwareBitmap preview frame
                var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
    
                // Capture the preview frame
                using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
                {
                    // Collect the resulting frame
                    SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
    
                    // Show the frame information
                    FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);
    
                    // Save the frame (as is, no rotation is being applied)
                    var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("PreviewFrame.jpg", CreationCollisionOption.GenerateUniqueName);
    
                    Debug.WriteLine("Saving preview frame to " + file.Path);
    
                    await SaveSoftwareBitmapAsync(previewFrame, file);
                }
            }
    
            private static async Task SaveSoftwareBitmapAsync(SoftwareBitmap bitmap, StorageFile file)
            {
                using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);
    
                    // Grab the data from the SoftwareBitmap
                    encoder.SetSoftwareBitmap(bitmap);
                    await encoder.FlushAsync();
                }
            }
        }

    What do I have to do with the "previewFrame" var to be able to send it ?

    Thanks in advance


    • Edited by juavenel Thursday, May 4, 2017 5:36 PM
    Thursday, May 4, 2017 5:36 PM

All replies