public async Task StartPreviewAsync(QR_Code_Scanner.Business.ComboboxItem comboboxItem)
        {
            FrameSourceInformation frameSourceInformation = new FrameSourceInformation();

            try
            {
                mediaCapture = new MediaCapture();


                var settings = new MediaCaptureInitializationSettings()
                {
                    StreamingCaptureMode = StreamingCaptureMode.Video
                };
                if (comboboxItem != null)
                {
                    settings.VideoDeviceId = comboboxItem.ID;
                    frameSourceInformation = comboboxItem.MediaFrameSourceInformation;
                }
                else
                {
                    if (availableColorCameras == null)
                    {
                        var frameSourceInformations = await GetFrameSourceInformationAsync();

                        frameSourceInformation = frameSourceInformations.First();
                        availableColorCameras  = await GetFrameSourceGroupsAsync(frameSourceInformation);
                    }
                    settings.VideoDeviceId = availableColorCameras.First().Id;
                }

                qrAnalyzerCancellationTokenSource = new CancellationTokenSource();
                try
                {
                    await mediaCapture.InitializeAsync(settings);
                }
                catch (Exception ex)
                {
                    MessageManager.ShowMessageToUserAsync("Tried to initialize a color camera but failed to do so.");
                }
                List <VideoEncodingProperties> availableResolutions = null;
                try
                {
                    availableResolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview).Where(properties => properties is VideoEncodingProperties).Select(properties => (VideoEncodingProperties)properties).ToList();
                }
                catch (Exception ex)
                {
                    MessageManager.ShowMessageToUserAsync("No resolutions could be detected, trying default mode.");
                }

                VideoEncodingProperties bestVideoResolution = this.findBestResolution(availableResolutions);

                if (bestVideoResolution != null)
                {
                    await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, bestVideoResolution);
                }

                displayRequest.RequestActive();
            }
            catch (UnauthorizedAccessException)
            {
                // This will be thrown if the user denied access to the camera in privacy settings
                MessageManager.ShowMessageToUserAsync("The app was denied access to the camera");
                return;
            }

            try
            {
                this.ScanForQRcodes         = true;
                previewWindowElement.Source = mediaCapture;
                await mediaCapture.StartPreviewAsync();

                isPreviewing = true;
                var imgProp = new ImageEncodingProperties
                {
                    Subtype = "BMP",
                    Width   = (uint)imgCaptureWidth,
                    Height  = (uint)imgCaptureHeight
                };
                var bcReader          = new BarcodeReader();
                var qrCaptureInterval = 200;

                var torch = mediaCapture.VideoDeviceController.TorchControl;
                var exposureCompensationControl = mediaCapture.VideoDeviceController.ExposureCompensationControl;

                if (torch.Supported)
                {
                    torch.Enabled = false;
                }
                //if (exposureCompensationControl.Supported) {
                //    var maxSupported = exposureCompensationControl.Max;
                //    var minSupported = exposureCompensationControl.Min;
                //    var middleExposure = (maxSupported + minSupported) / 2;
                //    var quarterExposure = (middleExposure + minSupported) / 2;
                //    await exposureCompensationControl.SetValueAsync(quarterExposure);
                //}

                // Get information about the preview
                var previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

                while (!qrAnalyzerCancellationTokenSource.IsCancellationRequested && qrAnalyzerCancellationTokenSource != null && qrAnalyzerCancellationTokenSource.Token != null)
                {
                    //try capture qr code here
                    if (ScanForQRcodes)
                    {
                        VideoFrame videoFrameFormatPlaceholder = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
                        await mediaCapture.GetPreviewFrameAsync(videoFrameFormatPlaceholder);
                        await findQRinImageAsync(bcReader, videoFrameFormatPlaceholder);

                        videoFrameFormatPlaceholder.Dispose();
                        videoFrameFormatPlaceholder = null;
                    }

                    //await Task.Delay(qrCaptureInterval, qrAnalyzerCancellationTokenSource.Token);
                    var   delayTask        = Task.Delay(qrCaptureInterval, qrAnalyzerCancellationTokenSource.Token);
                    var   continuationTask = delayTask.ContinueWith(task => { });
                    await continuationTask;
                }
            }
            catch (System.IO.FileLoadException)
            {
                mediaCapture.CaptureDeviceExclusiveControlStatusChanged += mediaCapture_CaptureDeviceExclusiveControlStatusChanged;
            }
            catch (System.ObjectDisposedException)
            {
                Debug.WriteLine("object was disposed");
            }
            catch (Exception)
            {
                Debug.WriteLine("another exception occurred.");
            }
        }
 public async Task <IEnumerable <DeviceInformation> > GetFrameSourceGroupsAsync(FrameSourceInformation frameSourceInformation)
 {
     try
     {
         var availableColorCamera = frameSourceInformation.MediaFrameSourceGroup.SourceInfos.Select(y => y.DeviceInformation).Distinct();
         return(availableColorCamera);
     }
     catch (Exception ex)
     {
         MessageManager.ShowMessageToUserAsync("Tried to find all available color cameras but failed to do so.");
         return(null);
     }
 }
Esempio n. 3
0
 public ComboboxItem(string name, string id, FrameSourceInformation mediaFrameSourceInformation)
 {
     Name = name; ID = id; MediaFrameSourceInformation = mediaFrameSourceInformation;
 }