예제 #1
0
        public void Scanner_OnScanResult(Result result)
        {
            this.result = result.Text;

            if (!(string.IsNullOrEmpty(result?.Text)))
            {
                if (this.modal)
                {
                    try
                    {
                        CodeScanned?.Invoke(this, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        Log.Critical(ex);
                    }

                    this.BackClicked();
                }
                else
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        this.Link.Text            = result.Text;
                        this.ScanGrid.IsVisible   = false;
                        this.ManualGrid.IsVisible = true;
                        this.ModeButton.Text      = "Scan Code";
                        this.OpenButton.Focus();
                    });
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Запустить сканирование
        /// </summary>
        public void StartScanning()
        {
            IsStarted = true;

            // Get our preview properties
            var previewProperties = _capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

            _scanningTimer = new Timer(async(state) =>
            {
                if (_isProcessingFrame || _capture == null || _capture.CameraStreamState != CameraStreamState.Streaming)
                {
                    _scanningTimer?.Change(DelayBetweenAnalyzingFrames, Timeout.Infinite);
                    return;
                }

                var token = _scanCancellationToken.Token;

                var delay = DelayBetweenAnalyzingFrames;

                _isProcessingFrame = true;

                VideoFrame destFrame = null;
                VideoFrame frame     = null;

                try
                {
                    // Setup a frame to use as the input settings
                    destFrame = new VideoFrame(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

                    // Get preview
                    frame = await _capture.GetPreviewFrameAsync(destFrame);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("GetPreviewFrame Failed: {0}", ex);
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }

                Result result = null;

                // Try decoding the image
                try
                {
                    if (frame != null)
                    {
                        await _dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            if (_bitmap == null)
                            {
                                _bitmap = new WriteableBitmap(frame.SoftwareBitmap.PixelWidth, frame.SoftwareBitmap.PixelHeight);
                            }

                            frame.SoftwareBitmap.CopyToBuffer(_bitmap.PixelBuffer);

                            result = _barcodeReader.Decode(_bitmap);

                            if (destFrame != null)
                            {
                                destFrame.Dispose();
                                destFrame = null;
                            }

                            frame.Dispose();
                            frame = null;
                        });
                    }
                }
                catch (Exception ex)
                {
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }

                if (result != null)
                {
                    CodeScanned?.Invoke(this, result);

                    delay = DelayBetweenContinuousScans;
                }

                _isProcessingFrame = false;

                _scanningTimer?.Change(delay, Timeout.Infinite);
            }, null, InitialDelayBeforeAnalyzingFrames, Timeout.Infinite);
        }