예제 #1
0
        static async Task <BarcodeScanner> FindBarcodeScannerAsync()
        {
            BarcodeScanner locatedScanner = null;

            if (CachedBarcodeDeviceId == null)
            {
                // NB: changed to 'local' here in the hope of speeding up this call as
                // it seems to take a long time.
                var deviceSelector = BarcodeScanner.GetDeviceSelector(
                    PosConnectionTypes.Local);

                var devices = await DeviceInformation.FindAllAsync(deviceSelector);

                if (devices != null)
                {
                    foreach (var device in devices)
                    {
                        var scanner = await BarcodeScanner.FromIdAsync(device.Id);

                        if (scanner.VideoDeviceId != null)
                        {
                            CachedBarcodeDeviceId = device.Id;
                            break;
                        }
                    }
                }
            }
            if (CachedBarcodeDeviceId != null)
            {
                locatedScanner = await BarcodeScanner.FromIdAsync(CachedBarcodeDeviceId);
            }
            return(locatedScanner);
        }
 async Task <bool> CreateDeviceList_OneTime(Type[] deviceClasses)
 {
     foreach (Type ty in deviceClasses)
     {
         DeviceInformationCollection deviceList = null;
         if (ty == typeof(BarcodeScanner))
         {
             deviceList = await DeviceInformation.FindAllAsync(BarcodeScanner.GetDeviceSelector());
         }
         else if (ty == typeof(PosPrinter))
         {
             deviceList = await DeviceInformation.FindAllAsync(PosPrinter.GetDeviceSelector());
         }
         else if (ty == typeof(CashDrawer))
         {
             deviceList = await DeviceInformation.FindAllAsync(CashDrawer.GetDeviceSelector());
         }
         else if (ty == typeof(MagneticStripeReader))
         {
             deviceList = await DeviceInformation.FindAllAsync(MagneticStripeReader.GetDeviceSelector());
         }
         else if (ty == typeof(LineDisplay))
         {
             deviceList = await DeviceInformation.FindAllAsync(LineDisplay.GetDeviceSelector());
         }
         AddToSelectionList(ty, deviceList);
     }
     return(true);
 }
예제 #3
0
        private async void OnInitializeScannerAsync()
        {
            string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.Local);
            DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

            foreach (var device in deviceCollection)
            {
                BarcodeScanner scanner = await BarcodeScanner.FromIdAsync(device.Id);

                if (scanner != null && scanner.VideoDeviceId != null)
                {
                    _claimedScanner = await scanner.ClaimScannerAsync();

                    if (_claimedScanner != null)
                    {
                        await _claimedScanner.EnableAsync();

                        _claimedScanner.DataReceived += OnBarcodeDetected;

                        await _videoPreview.StartStreamingAsync(scanner.VideoDeviceId);

                        await _claimedScanner.StartSoftwareTriggerAsync();

                        break;
                    }
                }
            }
        }
예제 #4
0
 /// <summary>
 /// Start device watcher.
 /// </summary>
 public void StartWatcher(CoreDispatcher dispatcher)
 {
     if (posDeviceWatcher == null)
     {
         posDeviceWatcher = new PosDeviceWatcher(BarcodeScanner.GetDeviceSelector(), dispatcher);
         posDeviceWatcher.Start();
     }
 }
 /// <summary>
 /// Start device watcher.
 /// </summary>
 private void StartWatcher()
 {
     if (posDeviceWatcher == null)
     {
         posDeviceWatcher = new PosDeviceWatcher(BarcodeScanner.GetDeviceSelector(), Dispatcher);
         posDeviceWatcher.Start();
     }
 }
예제 #6
0
 internal async Task StartScannerAsync()
 {
     capturePreview.Visibility = Visibility.Visible;
     capturePreview.Width = 400; capturePreview.Height = 300;
     scanner = await DeviceHelpers.GetFirstDeviceAsync(BarcodeScanner.GetDeviceSelector(), async (id) => await BarcodeScanner.FromIdAsync(id));
     if (scanner != null)
     {
         claimedScanner = await scanner.ClaimScannerAsync();
         if (claimedScanner != null)
         {
             claimedScanner.ReleaseDeviceRequested += ClaimedScanner_ReleaseDeviceRequested;
             claimedScanner.DataReceived += ClaimedScanner_DataReceived;
             claimedScanner.IsDecodeDataEnabled = true;
             await claimedScanner.EnableAsync();
             try
             {
                 bool haveAssociatedCamera = !string.IsNullOrEmpty(scanner.VideoDeviceId);
                 if (haveAssociatedCamera)
                 {
                     captureManager = new MediaCapture();
                     captureManager.Failed += CaptureManager_Failed;
                     MediaCaptureInitializationSettings _captureInitSettings = new MediaCaptureInitializationSettings
                     {
                         VideoDeviceId = scanner.VideoDeviceId,
                         SharingMode = MediaCaptureSharingMode.SharedReadOnly, // share
                         StreamingCaptureMode = StreamingCaptureMode.Video     // just video
                     };
                     await captureManager.InitializeAsync(_captureInitSettings);
                     capturePreview.Source = captureManager;
                 }
                 UpdateMessage("waiting..." + (!haveAssociatedCamera ? "But scanner not camera type" : ""));
                 if (captureManager != null) await captureManager.StartPreviewAsync();
                 await claimedScanner.StartSoftwareTriggerAsync();
             }
             catch (Exception e)
             {
                 UpdateMessage(e.Message);
                 Debug.WriteLine("EXCEPTION: " + e.Message);
             }
         }
         else
         {
             UpdateMessage("Could not claim barcode scanner");
         }
     }
     else
     {
         UpdateMessage("No barcode scanners found");
     }
 }
예제 #7
0
        public Scenario5_DisplayingBarcodePreview()
        {
            this.InitializeComponent();

            ScannerListSource.Source = barcodeScanners;

            watcher          = DeviceInformation.CreateWatcher(BarcodeScanner.GetDeviceSelector());
            watcher.Added   += Watcher_Added;
            watcher.Removed += Watcher_Removed;
            watcher.Updated += Watcher_Updated;
            watcher.Start();

            DataContext = this;
        }
예제 #8
0
 private static string GetPosSelector(Type ty)
 {
     if (ty == typeof(BarcodeScanner))
     {
         return(BarcodeScanner.GetDeviceSelector());
     }
     if (ty == typeof(PosPrinter))
     {
         return(PosPrinter.GetDeviceSelector());
     }
     if (ty == typeof(CashDrawer))
     {
         return(CashDrawer.GetDeviceSelector());
     }
     if (ty == typeof(MagneticStripeReader))
     {
         return(MagneticStripeReader.GetDeviceSelector());
     }
     if (ty == typeof(LineDisplay))
     {
         return(LineDisplay.GetDeviceSelector());
     }
     return(null);
 }
예제 #9
0
        private async void CodeScanner_Loaded(object sender, RoutedEventArgs e)
        {
            BarcodeList.ItemsSource = BarcodeHistory = new ObservableCollection <BarcodeItem>();
            ExitLocker = new AutoResetEvent(false);

            string Selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.Local);
            DeviceInformationCollection DeviceCollection = await DeviceInformation.FindAllAsync(Selector);

            if (DeviceCollection == null || DeviceCollection.Count == 0)
            {
                Pro.Visibility = Visibility.Collapsed;
                ProText.Text   = "无摄像头可用";
                return;
            }

            if (ApplicationData.Current.LocalSettings.Values["LastSelectedCameraSource"] is string StorageCameraSource)
            {
                foreach (var DeviceID in from Device in DeviceCollection
                         where Device.Name.Contains(StorageCameraSource)
                         select Device.Id)
                {
                    using (BarcodeScanner Scanner = await BarcodeScanner.FromIdAsync(DeviceID))
                    {
                        Capture = new MediaCapture();
                        var InitializeSettings = new MediaCaptureInitializationSettings
                        {
                            VideoDeviceId        = Scanner.VideoDeviceId,
                            StreamingCaptureMode = StreamingCaptureMode.Video,
                            PhotoCaptureSource   = PhotoCaptureSource.VideoPreview
                        };
                        await Capture.InitializeAsync(InitializeSettings);

                        var CameraFocusControl = Capture.VideoDeviceController.FocusControl;
                        if (CameraFocusControl.Supported)
                        {
                            await CameraFocusControl.UnlockAsync();

                            CameraFocusControl.Configure(new FocusSettings {
                                Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange
                            });
                            await CameraFocusControl.FocusAsync();
                        }
                        PreviewControl.Source = Capture;

                        ClaimedScanner = await Scanner.ClaimScannerAsync();

                        ClaimedScanner.IsDisabledOnDataReceived = false;
                        ClaimedScanner.IsDecodeDataEnabled      = true;
                        ClaimedScanner.DataReceived            += ClaimedScanner_DataReceived;
                        await ClaimedScanner.EnableAsync();

                        await ClaimedScanner.StartSoftwareTriggerAsync();

                        await Capture.StartPreviewAsync();
                    }
                    break;
                }

                if (Capture == null)
                {
                    ContentDialog dialog = new ContentDialog
                    {
                        Title           = "提示",
                        Content         = "    设置所指定的摄像头无法应用于条码识别\r\r    已自动选择最合适的摄像头",
                        CloseButtonText = "确定",
                        Background      = Application.Current.Resources["DialogAcrylicBrush"] as Brush
                    };
                    _ = await dialog.ShowAsync();

                    using (BarcodeScanner Scanner = await BarcodeScanner.FromIdAsync(DeviceCollection.FirstOrDefault().Id))
                    {
                        Capture = new MediaCapture();
                        var InitializeSettings = new MediaCaptureInitializationSettings
                        {
                            VideoDeviceId        = Scanner.VideoDeviceId,
                            StreamingCaptureMode = StreamingCaptureMode.Video,
                            PhotoCaptureSource   = PhotoCaptureSource.VideoPreview
                        };
                        await Capture.InitializeAsync(InitializeSettings);

                        var CameraFocusControl = Capture.VideoDeviceController.FocusControl;
                        if (CameraFocusControl.Supported)
                        {
                            await CameraFocusControl.UnlockAsync();

                            CameraFocusControl.Configure(new FocusSettings {
                                Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange
                            });
                            await CameraFocusControl.FocusAsync();
                        }
                        PreviewControl.Source = Capture;

                        ClaimedScanner = await Scanner.ClaimScannerAsync();

                        ClaimedScanner.IsDisabledOnDataReceived = false;
                        ClaimedScanner.IsDecodeDataEnabled      = true;
                        ClaimedScanner.DataReceived            += ClaimedScanner_DataReceived;
                        await ClaimedScanner.EnableAsync();

                        await ClaimedScanner.StartSoftwareTriggerAsync();

                        await Capture.StartPreviewAsync();
                    }
                }
            }
            else
            {
                using (BarcodeScanner Scanner = await BarcodeScanner.FromIdAsync(DeviceCollection.FirstOrDefault().Id))
                {
                    Capture = new MediaCapture();
                    var InitializeSettings = new MediaCaptureInitializationSettings
                    {
                        VideoDeviceId        = Scanner.VideoDeviceId,
                        StreamingCaptureMode = StreamingCaptureMode.Video,
                        PhotoCaptureSource   = PhotoCaptureSource.VideoPreview
                    };
                    await Capture.InitializeAsync(InitializeSettings);

                    var CameraFocusControl = Capture.VideoDeviceController.FocusControl;
                    if (CameraFocusControl.Supported)
                    {
                        await CameraFocusControl.UnlockAsync();

                        CameraFocusControl.Configure(new FocusSettings {
                            Mode = FocusMode.Continuous, AutoFocusRange = AutoFocusRange.FullRange
                        });
                        await CameraFocusControl.FocusAsync();
                    }
                    PreviewControl.Source = Capture;

                    ClaimedScanner = await Scanner.ClaimScannerAsync();

                    ClaimedScanner.IsDisabledOnDataReceived = false;
                    ClaimedScanner.IsDecodeDataEnabled      = true;
                    ClaimedScanner.DataReceived            += ClaimedScanner_DataReceived;
                    await ClaimedScanner.EnableAsync();

                    await ClaimedScanner.StartSoftwareTriggerAsync();

                    await Capture.StartPreviewAsync();
                }
            }
            LoadingControl.IsLoading = false;

            ExitLocker.Set();
        }
 public static async Task <BarcodeScanner> GetFirstBarcodeScannerAsync(PosConnectionTypes connectionTypes = PosConnectionTypes.All)
 {
     return(await DeviceHelpers.GetFirstDeviceAsync(BarcodeScanner.GetDeviceSelector(connectionTypes), async (id) => await BarcodeScanner.FromIdAsync(id)));
 }
예제 #11
0
        /// <summary>
        /// Creates the default barcode scanner.
        /// </summary>
        /// <returns>true if barcode scanner is created. Otherwise returns false</returns>
        private async Task <bool> CreateDefaultScannerObject()
        {
            if (scanner == null)
            {
                rootPage.NotifyUser("Creating barcode scanner object.", NotifyType.StatusMessage);
                DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(BarcodeScanner.GetDeviceSelector());

                if (deviceCollection != null && deviceCollection.Count > 0)
                {
                    scanner = await BarcodeScanner.FromIdAsync(deviceCollection[0].Id);

                    if (scanner == null)
                    {
                        rootPage.NotifyUser("Failed to create barcode scanner object.", NotifyType.ErrorMessage);
                        return(false);
                    }
                }
                else
                {
                    rootPage.NotifyUser("Barcode scanner not found. Please connect a barcode scanner.", NotifyType.ErrorMessage);
                    return(false);
                }
            }

            return(true);
        }
 public static async Task <BarcodeScanner> GetFirstBarcodeScannerAsync()
 {
     return(await DeviceHelpers.GetFirstDeviceAsync(BarcodeScanner.GetDeviceSelector(), async (id) => await BarcodeScanner.FromIdAsync(id)));
 }