示例#1
0
        void barcodeReader_SampleDecoded(VideoBarcodeReader sender, SampleDecodedEventArgs e)
        {
            var ignore = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                if (e.Text == null)
                {
                    BarcodeOutline.Points.Clear();
                    TextLog.Text = "No barcode";
                }
                else
                {
                    TextLog.Text = e.Text;

                    BarcodeOutline.Points.Clear();
                    foreach (var point in e.Points)
                    {
                        BarcodeOutline.Points.Add(new Point(point.X * BarcodeOutline.Width, point.Y * BarcodeOutline.Height));
                    }
                }
            });
        }
示例#2
0
        private async Task InitializeCaptureAsync()
        {
            var settings = new MediaCaptureInitializationSettings
            {
                StreamingCaptureMode = StreamingCaptureMode.Video
            };
            await settings.SelectVideoDeviceAsync(VideoDeviceSelection.BackOrFirst);

            var capture = new MediaCapture();
            await capture.InitializeAsync(settings);

            // Select the capture resolution closest to screen resolution
            var formats = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoPreview);
            var format  = (VideoEncodingProperties)formats.OrderBy((item) =>
            {
                var props = (VideoEncodingProperties)item;
                return(Math.Abs(props.Width - this.ActualWidth) + Math.Abs(props.Height - this.ActualHeight));
            }).First();
            await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, format);

            // Make the SwapChainPanel full screen
            var scale = Math.Min(this.ActualWidth / format.Width, this.ActualHeight / format.Height);

            Preview.Width  = format.Width;
            Preview.Height = format.Height;
            Preview.RenderTransformOrigin = new Point(.5, .5);
            Preview.RenderTransform       = new ScaleTransform {
                ScaleX = scale, ScaleY = scale
            };
            BarcodeOutline.Width  = format.Width;
            BarcodeOutline.Height = format.Height;
            BarcodeOutline.RenderTransformOrigin = new Point(.5, .5);
            BarcodeOutline.RenderTransform       = new ScaleTransform {
                ScaleX = scale, ScaleY = scale
            };

            var reader = await MediaReader.CreateFromMediaCaptureAsync(capture, AudioInitialization.Deselected, VideoInitialization.Bgra8);

            var presenter = ImagePresenter.CreateFromSwapChainPanel(Preview, reader.GraphicsDevice, (int)format.Width, (int)format.Height);

            m_capture = capture;

            // Run preview/detection out of UI thread
            var ignore = Task.Run(async() =>
            {
                using (var barcodeReader = new VideoBarcodeReader())
                {
                    barcodeReader.SampleDecoded += barcodeReader_SampleDecoded;

                    while (true)
                    {
                        Logger.Events.VideoStream_ReadStart();
                        using (var result = await reader.VideoStream.ReadAsync())
                        {
                            Logger.Events.VideoStream_ReadStop();

                            if (result.Error)
                            {
                                break;
                            }

                            var sample = (MediaSample2D)result.Sample;
                            if (sample == null)
                            {
                                continue;
                            }

                            presenter.Present(sample);

                            barcodeReader.QueueSample(sample);
                            result.DetachSample();     // Sample ownership transferred to barcodeReader
                        }
                    }

                    barcodeReader.SampleDecoded -= barcodeReader_SampleDecoded;
                    reader.Dispose();
                    presenter.Dispose();
                }
            });
        }