Exemplo n.º 1
0
        private async Task ScanAsync()
        {
            if (_cameraInitialized)
            {
                _result = null;
                while (_result == null)
                {
                    // 对焦
                    await _mediaCapture.VideoDeviceController.FocusControl.FocusAsync();

                    var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
                    if (previewProperties != null)
                    {
                        var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
                        using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
                        {
                            var frameBitmap = currentFrame.SoftwareBitmap;
                            if (frameBitmap != null)
                            {
                                var bitmap = new WriteableBitmap(frameBitmap.PixelWidth, frameBitmap.PixelHeight);
                                frameBitmap.CopyToBuffer(bitmap.PixelBuffer);

                                // 解析二维码
                                BarcodeReader barcodeReader = new BarcodeReader()
                                {
                                    AutoRotate = true,
                                    Options    = new DecodingOptions()
                                    {
                                        TryHarder = true
                                    }
                                };
                                _result = barcodeReader.Decode(bitmap);
                                if (_result != null)
                                {
                                    Debug.WriteLine($"扫描到二维码:{_result.Text}");
                                    QRCodeFound?.Invoke(qrCode, new QRCodeFoundEventArgs()
                                    {
                                        Result = _result
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
 private void QRCodeWatcher_Updated(object sender, QRCodeUpdatedEventArgs e)
 {
     SendProgressMessage($"Found QR code {e.Code.Data}");
     QRCodeFound?.Invoke(this, new QRInfo(e.Code));
 }