private void Cleanup()
        {
            System.Diagnostics.Debug.WriteLine("Cleaning up scanner");
            try
            {
                _Ready     = false;
                _Done      = true;
                _Rendering = true;

                _Controller   = null;
                _ImagePreview = null;
                _Effects      = null;

                _Render.Dispose();
                _Render = null;
                _Image  = null;

                _Source.StopPreviewAsync();
                _Source.PreviewFrameAvailable -= OnPreviewFrameAvailable;
                _Source.Dispose();
                _Source = null;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Cleaning up scanner. {0}", ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// Resolves an instance of video encoding properties with the highest frame rate available
        /// matching the given media stream type.
        /// </summary>
        /// <param name="controller">The video device controller.</param>
        /// <param name="mediaStreamType">The media stream type.</param>
        /// <returns>An instance of video encoding properties with the highest frame rate available.</returns>
        public static VideoEncodingProperties ResolveVideoEncodingPropertiesWithHighestFrameRate(
            VideoDeviceController controller, MediaStreamType mediaStreamType)
        {
            VideoEncodingProperties propertiesWithHighestFrameRate = null;

            if (controller != null)
            {
                IReadOnlyList <IMediaEncodingProperties> availableProperties =
                    controller.GetAvailableMediaStreamProperties(mediaStreamType);
                uint highestFrameRate = 0;

                foreach (IMediaEncodingProperties properties in availableProperties)
                {
                    VideoEncodingProperties videoEncodingProperties = properties as VideoEncodingProperties;

                    if (videoEncodingProperties != null)
                    {
                        uint frameRate = ResolveFrameRate(videoEncodingProperties);

                        if (frameRate > highestFrameRate)
                        {
                            propertiesWithHighestFrameRate = videoEncodingProperties;
                            highestFrameRate = frameRate;
                        }
                    }
                }
            }

            return(propertiesWithHighestFrameRate);
        }
Exemple #3
0
        /// <summary>
        /// Resolves all the instances of video encoding properties with the highest frame rate
        /// available matching the given media stream type.
        /// </summary>
        /// <param name="controller">The video device controller.</param>
        /// <param name="mediaStreamType">The media stream type.</param>
        /// <returns>All the instances of video encoding properties with the highest frame rate available.</returns>
        public static IList <VideoEncodingProperties> ResolveAllVideoEncodingPropertiesWithHighestFrameRate(
            VideoDeviceController controller, MediaStreamType mediaStreamType)
        {
            uint highestFrameRate = ResolveHighestFrameRate(controller, mediaStreamType);
            IList <VideoEncodingProperties> listOfPropertiesWithHighestFrameRate = null;

            if (highestFrameRate > 0)
            {
                listOfPropertiesWithHighestFrameRate = new List <VideoEncodingProperties>();
                IReadOnlyList <IMediaEncodingProperties> availableProperties =
                    controller.GetAvailableMediaStreamProperties(mediaStreamType);

                foreach (IMediaEncodingProperties properties in availableProperties)
                {
                    VideoEncodingProperties videoEncodingProperties = properties as VideoEncodingProperties;

                    if (videoEncodingProperties != null)
                    {
                        uint frameRate = ResolveFrameRate(videoEncodingProperties);

                        if (frameRate == highestFrameRate)
                        {
                            listOfPropertiesWithHighestFrameRate.Add(videoEncodingProperties);
                        }
                    }
                }
            }

            return(listOfPropertiesWithHighestFrameRate);
        }
Exemple #4
0
        /// <summary>
        /// Resolves a video encoding properties instance based on the given arguments.
        /// </summary>
        /// <param name="controller">The video device controller.</param>
        /// <param name="mediaStreamType">The media stream type.</param>
        /// <param name="frameRate">The desired framerate.</param>
        /// <param name="width">The desired width in pixels.</param>
        /// <param name="height">The desired height in pixels.</param>
        /// <returns>A video encoding properties instance matching the given arguments or null if not found.</returns>
        public static VideoEncodingProperties FindVideoEncodingProperties(
            VideoDeviceController controller, MediaStreamType mediaStreamType,
            uint frameRate, uint width, uint height)
        {
            VideoEncodingProperties matchingProperties = null;

            if (controller != null)
            {
                IReadOnlyList <IMediaEncodingProperties> availableProperties =
                    controller.GetAvailableMediaStreamProperties(mediaStreamType);

                foreach (IMediaEncodingProperties properties in availableProperties)
                {
                    VideoEncodingProperties videoEncodingProperties = properties as VideoEncodingProperties;

                    if (videoEncodingProperties != null)
                    {
                        if (ResolveFrameRate(videoEncodingProperties) == frameRate &&
                            videoEncodingProperties.Width == width &&
                            videoEncodingProperties.Height == height)
                        {
                            matchingProperties = videoEncodingProperties;
                            break;
                        }
                    }
                }
            }

            return(matchingProperties);
        }
Exemple #5
0
    /*
     * public void OnPhotoKeyWordDetected()
     * {
     *  //if it is capturing photo now, just return
     *  if (isCapturingPhoto)
     *  {
     *      return;
     *  }
     *
     *  isCapturingPhoto = true;
     *  //TextManager.Instance.setText("Taking picture...");
     *
     *  photoCaptureObj.TakePhotoAsync(OnPhotoCaptured);
     * }
     */

    void OnPhotoCaptured(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result, UnityEngine.XR.WSA.WebCam.PhotoCaptureFrame photoCaptureFrame)
    {
        //After the first photo, we want to lock in the current exposure and white balance settings.
        if (lockCameraSettings && currentPhoto == 1)
        {
#if WINDOWS_UWP
            unsafe {
                //This is how you access a COM object.
                VideoDeviceController vdm = (VideoDeviceController)Marshal.GetObjectForIUnknown(photoCaptureObj.GetUnsafePointerToVideoDeviceController());
                //Locks current exposure for all future images
                vdm.ExposureControl.SetAutoAsync(false); //Figureout how to better handle the Async
                //Locks the current WhiteBalance for all future images
                vdm.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Manual);
            }
#endif
        }
        //temp to store the matrix
        Matrix4x4 cameraToWorldMatrix;

        photoCaptureFrame.TryGetCameraToWorldMatrix(out cameraToWorldMatrix);
        Matrix4x4 worldToCameraMatrix = cameraToWorldMatrix.inverse;

        Matrix4x4 projectionMatrix;
        photoCaptureFrame.TryGetProjectionMatrix(out projectionMatrix);

#if UNITY_EDITOR
        projectionMatrix = GetDummyProjectionMatrix();
#endif

        projectionMatrixList.Add(projectionMatrix);
        worldToCameraMatrixList.Add(worldToCameraMatrix);

        m_Texture = new Texture2D(m_CameraParameters.cameraResolutionWidth, m_CameraParameters.cameraResolutionHeight, TextureFormat.RGBA32, false);
        photoCaptureFrame.UploadImageDataToTexture(m_Texture);

        m_Texture.wrapMode = TextureWrapMode.Clamp;

        m_Texture = ResizeTexture(m_Texture, TEXTURE_WIDTH, TEXTURE_HEIGHT);

        //textureList.Add(m_Texture);
        photoCaptureFrame.Dispose();

        //save room to png
        bytes = m_Texture.EncodeToPNG();
        //write to LocalState folder
        File.WriteAllBytes(Application.persistentDataPath + "/Room" + (currentPhoto + 1) + ".png", bytes);

        m_Texture.Compress(true);
        Graphics.CopyTexture(m_Texture, 0, 0, textureArray, currentPhoto + 1, 0);

        if (OnTextureUpdated != null)
        {
            OnTextureUpdated();
        }
        currentPhoto    += 1;
        isCapturingPhoto = false;
        Resources.UnloadUnusedAssets();
    }
        private async Task InitCamera()
        {
            var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);

            string deviceId = devices.FirstOrDefault(d => d.EnclosureLocation.Panel != Windows.Devices.Enumeration.Panel.Front).Id;

            _Source = new CameraPreviewImageSource();
            await _Source.InitializeAsync(deviceId);

            _Source.PreviewFrameAvailable += OnPreviewFrameAvailable;

            //  set auto focus
            _Controller = (VideoDeviceController)_Source.VideoDeviceController;
            if (_Controller.FocusControl.Supported)
            {
                try
                {
                    if (_Controller.FocusControl.WaitForFocusSupported)
                    {
                        _Controller.FocusControl.Configure(new FocusSettings {
                            Mode = FocusMode.Continuous
                        });
                    }
                    else
                    {
                        _Controller.FocusControl.Configure(new FocusSettings {
                            Mode = FocusMode.Auto
                        });
                    }
                }
                catch
                {
                    _Controller.FocusControl.Configure(new FocusSettings {
                        Mode = FocusMode.Auto
                    });
                }
            }

            if (_Controller.FlashControl.Supported)
            {
                _Controller.FlashControl.Auto = true;
            }
            if (_Controller.ExposureControl.Supported)
            {
                _Controller.ExposureControl.SetAutoAsync(true);
            }

            _Image = new WriteableBitmap((int)Window.Current.Bounds.Width, (int)Window.Current.Bounds.Height);
            //  filters
            _Effects         = new FilterEffect(_Source);
            _Effects.Filters = new IFilter[] { new GrayscaleFilter() };

            _Render = new WriteableBitmapRenderer(_Effects, _Image);
            _Ready  = true;
        }
Exemple #7
0
        /// <summary>
        /// Grab worldToCamera matrix and projection matrix from the Hololens
        /// camera for correct texture placement, clip and save the texture as
        /// an image, then properly clean up (dispose of) the PhotoCaptureFrame
        /// storing relevant image data.
        /// </summary>
        /// <param name="result">
        /// Information about the success of taking the picture.
        /// </param>
        /// <param name="photoCaptureFrame">
        /// The information/image associated with the picture taken.
        /// </param>
        static void OnPhotoCaptured(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
        {
            // After the first photo, we want to lock in the current exposure and white balance settings.
            if (lockCameraSettings && currentPhoto == 0)
            {
                //#if WINDOWS_UWP
#if WINDOWS_UWP
                //#if UNITY_WSA_10_0
                unsafe {
                    //This is how you access a COM object.
                    VideoDeviceController vdm = (VideoDeviceController)Marshal.GetObjectForIUnknown(photoCaptureObj.GetUnsafePointerToVideoDeviceController());
                    //Locks current exposure for all future images
                    vdm.ExposureControl.SetAutoAsync(false); //Figureout how to better handle the Async
                    //Locks the current WhiteBalance for all future images
                    vdm.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Fluorescent);

                    //vdm.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Manual);
                }
#endif
            }

            // Grab appropriate matrices, and write them to the local storage
            // arrays at the correct index
            Matrix4x4 worldToCameraMatrix;
            Matrix4x4 projectionMatrix;
            bool      matricesExtracted = ExtractCameraMatrices(photoCaptureFrame, out worldToCameraMatrix, out projectionMatrix);
            if (matricesExtracted)
            {
                WriteMatricesToArrays(worldToCameraMatrix, projectionMatrix, currentPhoto);

                // Set up local class texture to save as a picture/texture - Hololens camera requires BGRA32 format
                m_Texture = new Texture2D(m_CameraParameters.cameraResolutionWidth, m_CameraParameters.cameraResolutionHeight, TextureFormat.BGRA32, false);
                photoCaptureFrame.UploadImageDataToTexture(m_Texture);
                m_Texture          = ClipTexture(m_Texture);
                m_Texture.wrapMode = TextureWrapMode.Clamp;
                SaveTexture();

                // Reset displayed message to remind user how to take photos or end texturing process
                TextManager.SetText(Messages.PhotoPrompt + Messages.AppendNumPhotosTaken());
            }
            else
            {
                TextManager.SetText(Messages.MatrixFail);
            }
            // Clean up camera memory
            photoCaptureFrame.Dispose();

            // Automatically shut down the operation if the maximum number of
            // textures is reached.
            if (currentPhoto >= MaxPhotoNum)
            {
                StopTextureCapture();
            }
        }
Exemple #8
0
        private async void Init()
        {
            //Get back camera
            var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            var backCameraId =
                devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Panel.Back).Id;

            //Start preview
            _cameraPreviewImageSource = new CameraPreviewImageSource();
            await _cameraPreviewImageSource.InitializeAsync(backCameraId);

            var properties = await _cameraPreviewImageSource.StartPreviewAsync();

            //Setup preview
            _width  = 640.0;
            _height = _width / properties.Width * properties.Height;
            var bitmap = new WriteableBitmap((int)_width, (int)_height);

            _writeableBitmap = bitmap;

            PreviewImage.Source = _writeableBitmap;

            _writeableBitmapRenderer = new WriteableBitmapRenderer(_cameraPreviewImageSource, _writeableBitmap);

            _cameraPreviewImageSource.PreviewFrameAvailable += OnPreviewFrameAvailable;

            _videoDevice = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;

            //Set timer for auto focus
            if (_videoDevice.FocusControl.Supported)
            {
                var focusSettings = new FocusSettings
                {
                    AutoFocusRange        = AutoFocusRange.Macro,
                    Mode                  = FocusMode.Auto,
                    WaitForFocus          = false,
                    DisableDriverFallback = false
                };

                _videoDevice.FocusControl.Configure(focusSettings);

                _timer = new DispatcherTimer
                {
                    Interval = new TimeSpan(0, 0, 0, 2, 0)
                };
                _timer.Tick += TimerOnTick;
                _timer.Start();
            }

            await _videoDevice.ExposureControl.SetAutoAsync(true);

            _initialized = true;
        }
        private static void ReglerControleurVideo(MediaFrameSource sourceImages)
        {
            VideoDeviceController controleurVideo = sourceImages.Controller.VideoDeviceController;

            controleurVideo.DesiredOptimization = MediaCaptureOptimization.Quality;
            controleurVideo.PrimaryUse          = CaptureUse.Video;
            if (controleurVideo.Exposure.Capabilities.Supported && controleurVideo.Exposure.Capabilities.AutoModeSupported)
            {
                controleurVideo.Exposure.TrySetAuto(true);
            }
        }
Exemple #10
0
 /// <summary>
 /// Dispose(bool disposing) executes in two distinct scenarios.
 /// If disposing equals true, the method has been called directly
 /// or indirectly by a user's code. Managed and unmanaged resources
 /// can be disposed.
 /// If disposing equals false, the method has been called by the
 /// runtime from inside the finalizer and you should not reference
 /// other objects. Only unmanaged resources can be disposed.
 /// If that's confusing to you too, maybe read this: https://docs.microsoft.com/en-us/dotnet/api/system.idisposable.dispose
 /// </summary>
 public void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (controller != null)
         {
             Marshal.ReleaseComObject(controller);
             controller = null;
         }
         disposed = true;
     }
 }
        /// <summary>
        /// Disposes the camera
        /// </summary>
        public void Dispose()
        {
#if CAN_USE_UWP_TYPES
            if (frameReader != null)
            {
                frameReader.FrameArrived -= OnMediaFrameArrived;
                frameReader.Dispose();
            }
            mediaCapture?.Dispose();
            mediaCapture          = null;
            videoDeviceController = null;
#endif
        }
Exemple #12
0
        private async void Initialize()
        {
            var cameraId = await ViewModel.GetCameraIdAsync(Windows.Devices.Enumeration.Panel.Back);

            _cameraPreviewImageSource = new CameraPreviewImageSource();
            await _cameraPreviewImageSource.InitializeAsync(cameraId.Id);

            var properties = await _cameraPreviewImageSource.StartPreviewAsync();

            _width  = Window.Current.Bounds.Width;
            _height = Window.Current.Bounds.Height;
            var bitmap = new WriteableBitmap((int)_width, (int)_height);

            _writeableBitmap = bitmap;

            PreviewImage.Source = _writeableBitmap;

            _writeableBitmapRenderer = new WriteableBitmapRenderer(_cameraPreviewImageSource, _writeableBitmap);

            _cameraPreviewImageSource.PreviewFrameAvailable += OnPreviewFrameAvailable;

            _videoDevice = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;

            if (_videoDevice.FocusControl.Supported)
            {
                var focusSettings = new FocusSettings
                {
                    AutoFocusRange        = AutoFocusRange.Macro,
                    Mode                  = FocusMode.Auto,
                    WaitForFocus          = false,
                    DisableDriverFallback = false
                };

                _videoDevice.FocusControl.Configure(focusSettings);

                _timer = new DispatcherTimer
                {
                    Interval = TimeSpan.FromSeconds(2)
                };
                _timer.Tick += OnTick;
                _timer.Start();
            }

            await _videoDevice.ExposureControl.SetAutoAsync(true);

            _initialized = true;
        }
        public async Task InitializeAsync()
        {
            _timeout = BarCodeManager.MaxTry;
            _sw.Restart();
            _capturing = true;
            _cleanedUp = false;
            // Create a camera preview image source (from Imaging SDK)
            _cameraPreviewImageSource = new CameraPreviewImageSource();

            // La sélection de la caméra arrière plante sur mon device :/
            //var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);
            //var backCamera = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
            //await _cameraPreviewImageSource.InitializeAsync(backCamera.Id);
            await _cameraPreviewImageSource.InitializeAsync(string.Empty);

            var properties = await _cameraPreviewImageSource.StartPreviewAsync();

            vdc = (VideoDeviceController)_cameraPreviewImageSource.VideoDeviceController;
            if (vdc.FocusControl.Supported)
            {
                vdc.FocusControl.Configure(new FocusSettings {
                    Mode = FocusMode.Auto
                });
                focus_period = TimeSpan.FromSeconds(5);
            }

            // Create a preview bitmap with the correct aspect ratio
            _width           = 640.0;
            _height          = (_width / properties.Width) * properties.Height;
            _writeableBitmap = new WriteableBitmap((int)_width, (int)_height);

            captureElement.Source = _writeableBitmap;

            _writeableBitmapRenderer        = new WriteableBitmapRenderer();
            _writeableBitmapRenderer.Source = _cameraPreviewImageSource;

            // Attach preview frame delegate
            _cameraPreviewImageSource.PreviewFrameAvailable += OnPreviewFrameAvailable;
        }
    private async void InitializeFacialRecognition()
    {
        if (m_faceTracker == null)
        {
            m_faceTracker = await FaceTracker.CreateAsync();
        }

        m_mediaCapture = new MediaCapture();
        MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();

        settings.StreamingCaptureMode = StreamingCaptureMode.Video;
        await m_mediaCapture.InitializeAsync(settings);

        VideoDeviceController deviceController = m_mediaCapture.VideoDeviceController;

        m_videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
        await m_mediaCapture.StartPreviewAsync();

        TimeSpan timerInterval = TimeSpan.FromMilliseconds(66);

        m_frameProcessingTimer = ThreadPoolTimer.CreatePeriodicTimer(new TimerElapsedHandler(ProcessCurrentVideoFrame), timerInterval);
    }
        public void LoadVideoMgmt_returns_correct_model_when_blank_device_is_present()
        {
            //Create the shims for test
            using (ShimsContext.Create())
            {
                //Arrange
                //Set up a provisioning data ServiceDto
                var aServiceDto = SetUpTestData("FIDVM");
                SetUpShims(aServiceDto);

                //Act
                //Call the Controller method
                var vDc = new VideoDeviceController();
                var result = vDc.LoadVideoMgmt("370001704986", "1423153155101", "     ");

                //Assert
                //Test the values returned by the Controller method
                const string expectedResult = @"HasMRDVRService  :true";
                const string deviceResponse = "DeviceActionResponse  :{  Code  :  200  ,  Message  :    },";
                TestReturnValue(result, expectedResult, deviceResponse);
            }
        }
 public void MyTestInitialize()
 {
     // Set up the SubscriberController
     VideoDeviceControllerForTests = DependencyResolver.Current.GetService<VideoDeviceController>();
 }
        public void LoadVideoMgmt_returns_correct_response_when_ServiceDto_is_null()
        {
            //Create the shims for test
            using (ShimsContext.Create())
            {
                //Arrange
                //Set up a provisioning data ServiceDto
                var aServiceDto = SetUpTestData(null);
                SetUpShims(aServiceDto);

                //Act
                //Call the Controller method
                var vDc = new VideoDeviceController();
                var result = vDc.LoadVideoMgmt("370001704986", "1423153155101", "MA1033FH1921");

                //Assert
                //Test the values returned by the Controller method
                const string expectedResult = @"HasMRDVRService  :false";
                const string expectedDeviceResponse = @"DeviceActionResponse  :{  Code  :  200  ,  Message  :    }";
                TestReturnValue(result, expectedResult, expectedDeviceResponse);
            }
        }
        public void LoadVideoMgmt_returns_correct_model_when_USI_Location_And_Device_Are_Null()
        {
            //Create the shims for test
            using (ShimsContext.Create())
            {
                //Arrange
                //Set up a provisioning data ServiceDto
                var aServiceDto = SetUpTestData("FIDVM");
                SetUpShims(aServiceDto);

                //Act
                //Call the Controller method
                var vDc = new VideoDeviceController();
                var result = vDc.LoadVideoMgmt(null, null, null);

                //Assert
                //Test the values returned by the Controller method
                const string expectedResult = @"HasMRDVRService  :false";
                const string deviceResponse = "DeviceActionResponse  :{  Code  :  200  ,  Message  :    },";
                TestReturnValue(result, expectedResult, deviceResponse);
            }
        }
Exemple #19
0
        public void Initialize(CameraSettingsActivatedEventArgs args)
        {
            videoDevController = (VideoDeviceController)args.VideoDeviceController;

            if (args.VideoDeviceExtension != null)
            {
                lcWrapper = new WinRTComponent();
                lcWrapper.Initialize(args.VideoDeviceExtension);
            }

            bool   bAuto = false;
            double value = 0.0;

            if (videoDevController.Brightness.Capabilities.Step != 0)
            {
                slBrt.Minimum       = videoDevController.Brightness.Capabilities.Min;
                slBrt.Maximum       = videoDevController.Brightness.Capabilities.Max;
                slBrt.StepFrequency = videoDevController.Brightness.Capabilities.Step;
                videoDevController.Brightness.TryGetValue(out value);
                slBrt.Value = value;
            }
            else
            {
                slBrt.IsEnabled = false;
            }
            if (videoDevController.Brightness.Capabilities.AutoModeSupported)
            {
                videoDevController.Brightness.TryGetAuto(out bAuto);
                tsBrtAuto.IsOn = bAuto;
            }
            else
            {
                tsBrtAuto.IsOn      = false;
                tsBrtAuto.IsEnabled = false;
            }

            if (videoDevController.Contrast.Capabilities.Step != 0)
            {
                slCrt.Minimum       = videoDevController.Contrast.Capabilities.Min;
                slCrt.Maximum       = videoDevController.Contrast.Capabilities.Max;
                slCrt.StepFrequency = videoDevController.Contrast.Capabilities.Step;
                videoDevController.Contrast.TryGetValue(out value);
                slCrt.Value = value;
            }
            else
            {
                slCrt.IsEnabled = false;
            }
            if (videoDevController.Contrast.Capabilities.AutoModeSupported)
            {
                videoDevController.Contrast.TryGetAuto(out bAuto);
                tsCrtAuto.IsOn = bAuto;
            }
            else
            {
                tsCrtAuto.IsOn      = false;
                tsCrtAuto.IsEnabled = false;
            }

            if (videoDevController.Focus.Capabilities.Step != 0)
            {
                slFocus.Minimum       = videoDevController.Focus.Capabilities.Min;
                slFocus.Maximum       = videoDevController.Focus.Capabilities.Max;
                slFocus.StepFrequency = videoDevController.Focus.Capabilities.Step;
                videoDevController.Focus.TryGetValue(out value);
                slFocus.Value = value;
            }
            else
            {
                slFocus.IsEnabled = false;
            }
            if (videoDevController.Focus.Capabilities.AutoModeSupported)
            {
                videoDevController.Focus.TryGetAuto(out bAuto);
                tsFocusAuto.IsOn = bAuto;
            }
            else
            {
                tsFocusAuto.IsOn      = false;
                tsFocusAuto.IsEnabled = false;
            }

            if (videoDevController.Exposure.Capabilities.Step != 0)
            {
                slExp.Minimum       = videoDevController.Exposure.Capabilities.Min;
                slExp.Maximum       = videoDevController.Exposure.Capabilities.Max;
                slExp.StepFrequency = videoDevController.Exposure.Capabilities.Step;
                videoDevController.Exposure.TryGetValue(out value);
                slExp.Value = value;
            }
            else
            {
                slExp.IsEnabled = false;
            }
            if (videoDevController.Exposure.Capabilities.AutoModeSupported)
            {
                videoDevController.Exposure.TryGetAuto(out bAuto);
                tsExpAuto.IsOn = bAuto;
            }
            else
            {
                tsExpAuto.IsOn      = false;
                tsExpAuto.IsEnabled = false;
            }

            if (lcWrapper != null)
            {
                slEffect.Minimum       = 0;
                slEffect.Maximum       = 100;
                slEffect.StepFrequency = 1;

                DspSettings dspSettings = lcWrapper.GetDspSetting();
                slEffect.Value = dspSettings.percentOfScreen;

                if (dspSettings.isEnabled == 1)
                {
                    tsEffectEnabled.IsOn = true;
                }
                else
                {
                    tsEffectEnabled.IsOn = false;
                    slEffect.IsEnabled   = false;
                }
            }
            else
            {
                tsEffectEnabled.IsEnabled = false;
                slEffect.IsEnabled        = false;
            }
        }
        /// <summary>
        /// Enumerate video preview formats and select the one
        /// whose dimensions are nearest to the input width/height.
        /// </summary>
        /// <returns>Selected format</returns>
        public static async Task <VideoEncodingProperties> SelectNearestPreviewResolutionAsync(this VideoDeviceController controller, double width, double height)
        {
            var formats = controller.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);

            var format = (VideoEncodingProperties)formats.OrderBy((item) =>
            {
                var props = (VideoEncodingProperties)item;
                return(Math.Abs(props.Width - width) + Math.Abs(props.Height - height));
            }).First();

            await controller.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format);

            return(format);
        }
Exemple #21
0
        public async Task <bool> InitializeAsync()
        {
            if (Initialized)
            {
                // Already intialized
                return(true);
            }

            _deviceInformationCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // Find the back camera
            _deviceInformation =
                (from camera in _deviceInformationCollection
                 where camera.EnclosureLocation != null &&
                 camera.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back
                 select camera).FirstOrDefault();

            if (_deviceInformation == null && _deviceInformationCollection.Count != 0)
            {
                // Fallback to whatever is available (e.g. webcam on laptop)
                _deviceInformation = _deviceInformationCollection[0];
            }

            if (_deviceInformation == null)
            {
                throw new Exception("No camera device found");
            }

            MediaCapture = new MediaCapture();
            MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings();

            settings.StreamingCaptureMode = StreamingCaptureMode.Video;
            settings.VideoDeviceId        = _deviceInformation.Id;
            await MediaCapture.InitializeAsync(settings);

#if WINDOWS_PHONE_APP
            _lightSensor.ReportInterval  = _lightSensor.MinimumReportInterval;
            _lightSensor.ReadingChanged += OnLightSensorReadingChangedAsync;
#endif

            _videoDeviceController = MediaCapture.VideoDeviceController;

            IList <VideoEncodingProperties> listOfPropertiesWithHighestFrameRate =
                CameraUtils.ResolveAllVideoEncodingPropertiesWithHighestFrameRate(_videoDeviceController, RecordMediaStreamType);

            if (listOfPropertiesWithHighestFrameRate != null && listOfPropertiesWithHighestFrameRate.Count > 0)
            {
                VideoEncodingProperties selectedRecordingVideoEncodingProperties = listOfPropertiesWithHighestFrameRate.ElementAt(0);
                uint selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width;

                for (int i = 1; i < listOfPropertiesWithHighestFrameRate.Count; ++i)
                {
                    VideoEncodingProperties currentProperties = listOfPropertiesWithHighestFrameRate.ElementAt(i);

                    if (selectedRecordingVideoWidth > MaximumVideoWidth ||
                        (currentProperties.Width <= MaximumVideoWidth && currentProperties.Width > selectedRecordingVideoWidth))
                    {
                        selectedRecordingVideoEncodingProperties = currentProperties;
                        selectedRecordingVideoWidth = selectedRecordingVideoEncodingProperties.Width;
                    }
                }

                _hfrVideoEncodingProperties = selectedRecordingVideoEncodingProperties;
                await _videoDeviceController.SetMediaStreamPropertiesAsync(RecordMediaStreamType, selectedRecordingVideoEncodingProperties);

                VideoEncodingProperties previewVideoEncodingProperties =
                    CameraUtils.FindVideoEncodingProperties(
                        _videoDeviceController, PreviewMediaStreamType,
                        PreviewFrameRate, selectedRecordingVideoWidth, selectedRecordingVideoEncodingProperties.Height);

                System.Diagnostics.Debug.WriteLine("Highest framerate for recording is "
                                                   + CameraUtils.ResolveFrameRate(selectedRecordingVideoEncodingProperties)
                                                   + " frames per second with selected resolution of "
                                                   + selectedRecordingVideoWidth + "x" + selectedRecordingVideoEncodingProperties.Height
                                                   + "\nThe preview properties for viewfinder are "
                                                   + CameraUtils.ResolveFrameRate(previewVideoEncodingProperties) + " FPS and "
                                                   + previewVideoEncodingProperties.Width + "x" + previewVideoEncodingProperties.Height);

                await _videoDeviceController.SetMediaStreamPropertiesAsync(PreviewMediaStreamType, previewVideoEncodingProperties);
            }

            if (_videoDeviceController.WhiteBalanceControl.Supported)
            {
                await _videoDeviceController.WhiteBalanceControl.SetPresetAsync(ColorTemperaturePreset.Fluorescent);
            }

            Initialized = true;
            return(Initialized);
        }
Exemple #22
0
 /// <param name="unknown">Pointer to a PhotoCapture camera.</param>
 public VideoDeviceControllerWrapperUWP(IntPtr unknown)
 {
     controller = (VideoDeviceController)Marshal.GetObjectForIUnknown(unknown);
 }
        /// <summary>
        /// Start the video stream. This just prepares the stream for capture, and doesn't start collecting frames
        /// </summary>
        /// <param name="streamDesc">The description of the stream to start.</param>
        public async void Start(StreamDescription streamDesc)
        {
#if CAN_USE_UWP_TYPES
            lock (stateLock)
            {
                if (State != CameraState.Initialized)
                {
                    throw new InvalidOperationException("Start cannot be called until the camera is in the Initialized state");
                }

                State = CameraState.Starting;
            }

            Resolution = streamDesc.Resolution;
            CameraType = streamDesc.CameraType;

            StreamDescriptionInternal desc = streamDesc as StreamDescriptionInternal;

            MediaCaptureInitializationSettings initSettings = new MediaCaptureInitializationSettings()
            {
                SourceGroup          = desc.FrameSourceGroup,
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            };

            // initialize the media device
            mediaCapture = new MediaCapture();

            try
            {
                await mediaCapture.InitializeAsync(initSettings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"MediaCapture initialization failed: {ex.Message}");
                mediaCapture.Dispose();
                mediaCapture = null;
            }

            if (mediaCapture != null)
            {
                // get access to the video device controller for property settings
                videoDeviceController = mediaCapture.VideoDeviceController;

                // choose media source
                MediaFrameSource frameSource     = mediaCapture.FrameSources[desc.FrameSourceInfo.Id];
                MediaFrameFormat preferredFormat = null;

                foreach (MediaFrameFormat format in frameSource.SupportedFormats)
                {
                    if (format.VideoFormat.Width == desc.Resolution.Width && format.VideoFormat.Height == desc.Resolution.Height && Math.Abs((double)format.FrameRate.Numerator / (double)format.FrameRate.Denominator - desc.Resolution.Framerate) < epsilon)
                    {
                        preferredFormat = format;
                        break;
                    }
                }

                if (preferredFormat != null && preferredFormat != frameSource.CurrentFormat)
                {
                    await frameSource.SetFormatAsync(preferredFormat);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine($"failed to set desired frame format");
                }

                // set up frame readercapture frame data
                frameReader = await mediaCapture.CreateFrameReaderAsync(frameSource);

                frameReader.FrameArrived += OnMediaFrameArrived;
                await frameReader.StartAsync();

                lock (stateLock)
                {
                    State = CameraState.Ready;
                    OnCameraStarted?.Invoke(this, true);
                }
            }
            else
            {
                lock (stateLock)
                {
                    // drop back to initialized when the camera doesn't initialize
                    State = CameraState.Initialized;
                    OnCameraStarted?.Invoke(this, false);
                }
            }
#else
            await Task.CompletedTask;
#endif
        }