private async Task startPreviewAsync()
        {
            try
            {
                mediaCapture = new MediaCapture();
                await mediaCapture.InitializeAsync(settings);


                displayRequest = new DisplayRequest();
                displayRequest.RequestActive();
                DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
                lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
            }
            catch (UnauthorizedAccessException)
            {
                MainPage.ShowMessage("Unable to start");
                return;
            }

            try
            {
                previewControl.Source = mediaCapture;
                await mediaCapture.StartPreviewAsync();

                isPreviewing = true;
            }
            catch (System.IO.FileLoadException)
            {
                mediaCapture.CaptureDeviceExclusiveControlStatusChanged += MediaCapture_CaptureDeviceExclusiveControlStatusChanged;
            }
        }
Esempio n. 2
0
        public async Task InitializeAsync(DeviceInformation camera)
        {
            if (camera != null)
            {
                try
                {
                    _mediaCapture = new MediaCapture();
                    await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = camera.Id, PhotoCaptureSource = PhotoCaptureSource.VideoPreview });

                    try
                    {
                        var res = new VideoEncodingProperties {
                            Height = 360, Width = 640, Bitrate = 121651200, ProfileId = 0, Subtype = "YUY2"
                        };
                        await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, res);
                    }
                    catch
                    {
                    }

                    _lowLagCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                    _mediaCapture = null;
                }
            }
            else
            {
                _mediaCapture = null;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Start video recording
        /// </summary>
        /// <returns></returns>
        private async Task StartVideoRecordingAsync()
        {
            try
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => PhotoButton.IsEnabled = false);

                if (_mediaCapture.MediaCaptureSettings.ConcurrentRecordAndPhotoSupported)
                {
                    ImageEncodingProperties imageEncodingProperties = ImageEncodingProperties.CreateJpeg();
                    _lowLagPhotoCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(imageEncodingProperties);

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

                var mediaEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
                var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Cam360", CreationCollisionOption.OpenIfExists);

                var file = await folder.CreateFileAsync("test.mp4", CreationCollisionOption.GenerateUniqueName);

                await _mediaCapture.StartRecordToStorageFileAsync(mediaEncodingProfile, file);
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("StartVideoRecording did not complete: {0}", ex.Message);
                Debug.Write(errorMessage);
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    await new MessageDialog(errorMessage).ShowAsync();
                });
            }
        }
Esempio n. 4
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();
                });
            }
        }
Esempio n. 5
0
 public async Task Init()
 {
     var initTask = Task.Run(async() =>
     {
         await mMediaCapture.InitializeAsync();
         mLowLagCapture = await mMediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg());
     });
 }
Esempio n. 6
0
        public async Task InitializeAsync()
        {
            _mediaCapture = new MediaCapture();
            await _mediaCapture.InitializeAsync();

            _mediaCapture.VideoDeviceController.PrimaryUse = CaptureUse.Photo;
            _capture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(GetImageEncodingProperties());
        }
Esempio n. 7
0
        private async Task <bool> Initialize()
        {
            await _mediaCapture.InitializeAsync();

            _lowLagPhotoCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(GetLowestFormat());

            _mediaCapture.VideoDeviceController.Focus.TrySetAuto(true);
            return(true);
        }
Esempio n. 8
0
        public async Task <SoftwareBitmap> GetImageAsync()
        {
            if (_photoCapture == null)
            {
                _photoCapture = await MediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
            }

            var cc = await _photoCapture.CaptureAsync();

            return(cc.Frame.SoftwareBitmap);
        }
        /// <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. 10
0
        async private void InitLowLagPhotoCapture_Click(object sender, RoutedEventArgs e)
        {
            // Enable thumbnail images
            mediaCaptureManager.VideoDeviceController.LowLagPhoto.ThumbnailEnabled     = true;
            mediaCaptureManager.VideoDeviceController.LowLagPhoto.ThumbnailFormat      = MediaThumbnailFormat.Bmp;
            mediaCaptureManager.VideoDeviceController.LowLagPhoto.DesiredThumbnailSize = 25;

            // Image properties
            ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();

            // Create LowLagPhotoCapture object
            lowLagCaptureMgr = await mediaCaptureManager.PrepareLowLagPhotoCaptureAsync(imgFormat);
        }
Esempio n. 11
0
        public async Task InitializeCameraAsync()
        {
            // Asynchronously initialize this instance.
            var mediaInitSettings = new MediaCaptureInitializationSettings {
                VideoDeviceId = this.DeviceId
            };

            mediaInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
            await _mediaCapture.InitializeAsync(mediaInitSettings);

            await this.SetCameraResolutionAsync(1280, 720);

            _lowLagCapture = await _mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
        }
        private async Task Init()
        {
            try
            {
                mMediaCapture = new MediaCapture();
                await mMediaCapture.InitializeAsync();

                mLowLagCapture = await mMediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateJpeg());

                mStreamWebSocket         = new StreamWebSocket();
                mStreamWebSocket.Closed += MStreamWebSocket_Closed;
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// Initializes the photo capture on the current camera device.
        /// </summary>
        /// <param name="parameters">Capture parameters.</param>
        /// <returns>Awaitable task.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="parameters"/> is <see langword="null"/>.
        ///     <para>-or-</para>
        /// Image encoding is not set in the <paramref name="parameters"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">Photo capture is already initialized.</exception>
        public override async Task InitializeAsync(CaptureParameters parameters)
        {
            if (parameters == null || parameters.ImageEncoding == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (this.photoCapture != null)
            {
                throw new InvalidOperationException("Low lag photo capture is already initialized.");
            }

            this.captureParameters = parameters;
            this.UpdateCameraSettings(this.captureParameters);

            this.photoCapture = await this.CameraController.MediaCapture.PrepareLowLagPhotoCaptureAsync(parameters.ImageEncoding);
        }
Esempio n. 14
0
        private async void M_videoTimer_Tick(object sender, object e)
        {
            if (m_mediaCapture == null)
            {
                m_videoTimer.Stop();
                m_videoTimer = null;
                return;
            }

            lock (m_mediaCapture)
            {
                if (isrunning)
                {
                    return;
                }
                isrunning = true;
            }

            LowLagPhotoCapture cap = await m_mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateBmp());

            CapturedPhoto photo = await cap.CaptureAsync();


            WriteableBitmap bitmap = new WriteableBitmap((int)photo.Frame.Width, (int)photo.Frame.Height);
            await bitmap.SetSourceAsync(photo.Frame.AsStreamForRead().AsRandomAccessStream());

            bitmap.Invalidate();
            byte[] imageArray = bitmap.PixelBuffer.ToArray();

            int  starting = (bitmap.PixelHeight / 2) * bitmap.PixelWidth * 4 + (bitmap.PixelWidth * 2);
            byte blue     = imageArray[starting];
            byte green    = imageArray[starting + 1];
            byte red      = imageArray[starting + 2];

            m_liveColor[0, 0] = red;
            m_liveColor[0, 1] = green;
            m_liveColor[0, 2] = blue;

            MakeLiveSettingsUpdate();

            isrunning = false;
        }
        /// <summary>
        /// Unloads the internal sequence objects.
        /// </summary>
        /// <returns>Awaitable task.</returns>
        public override async Task UnloadAsync()
        {
            if (this.photoCapture == null)
            {
                return;
            }

            await this.StopAsync();

            try
            {
                await this.photoCapture.FinishAsync();
            }
            catch (Exception e)
            {
                Tracing.Trace("LowLagCapture: Exception while finishing capture:\r\n{0}", e);
            }

            this.photoCapture = null;
        }
        public static async Task <SoftwareBitmap> CaptureImage(MediaCapture MediaCaptureElement)
        {
            if (lowLagCapture == null)
            {
                lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
            }

            try
            {
                CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync();

                SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;

                return(softwareBitmap);
            }
            catch
            {
                lowLagCapture = null;
                return(await CaptureImage(MediaCaptureElement));
            }
        }
        /// <summary>
        /// Initializes a new instance of the MediaCapture class setting a camera device.
        /// </summary>
        public async Task InitializeCameraAsync()
        {
            try
            {
                var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                var device = devices[0];
                _capture = new MediaCapture();
                var settings = new MediaCaptureInitializationSettings
                {
                    VideoDeviceId = device.Id
                };
                await _capture.InitializeAsync(settings);

                _lowLagPhotoCapture = await _capture.PrepareLowLagPhotoCaptureAsync(
                    ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));

                _capture.VideoDeviceController.Focus.TrySetAuto(true);
            }
            catch (Exception e)
            {
                await OnProcessNotice(e.ToString());
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Unloads the internal sequence objects.
        /// </summary>
        /// <returns>Awaitable task.</returns>
        public override async Task UnloadAsync()
        {
            if (this.photoCapture == null)
            {
                return;
            }

            await this.StopAsync();

            try
            {
                await this.photoCapture.FinishAsync();
            }
            catch (Exception e)
            {
                Tracing.Trace("LowLagCapture: Exception while finishing capture:\r\n{0}", e);
            }

            this.photoCapture = null;
        }
Esempio n. 19
0
        /// <summary>
        /// Initializes the photo capture on the current camera device.
        /// </summary>
        /// <param name="parameters">Capture parameters.</param>
        /// <returns>Awaitable task.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="parameters"/> is <see langword="null"/>.
        ///     <para>-or-</para>
        /// Image encoding is not set in the <paramref name="parameters"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">Photo capture is already initialized.</exception>
        public override async Task InitializeAsync(CaptureParameters parameters)
        {
            if (parameters == null || parameters.ImageEncoding == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (this.photoCapture != null)
            {
                throw new InvalidOperationException("Low lag photo capture is already initialized.");
            }

            this.captureParameters = parameters;
            this.UpdateCameraSettings(this.captureParameters);

            this.photoCapture = await this.CameraController.MediaCapture.PrepareLowLagPhotoCaptureAsync(parameters.ImageEncoding);
        }
Esempio n. 20
0
        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());
            }
        }
Esempio n. 21
0
 private void CameraCleanup()
 {
     if (_LLPC != null)
     {
         _LLPC.FinishAsync().GetAwaiter().GetResult();
         _LLPC = null;
     }
     if (_mediaCapture != null)
     {
         // Cleanup MediaCapture object
         _mediaCapture.Dispose();
         _mediaCapture = null;
     }
 }
Esempio n. 22
0
        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);
            }
        }
Esempio n. 23
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);
                }
            }
        }