/// <summary> /// Creates the default bar code _BarcodeScanner /// </summary> /// <returns>true if bar code _BarcodeScanner is returned</returns> private async System.Threading.Tasks.Task <bool> CreateDefaultScannerObject() { try { if (_BarcodeScanner == null) { _BarcodeScanner = await BarcodeScanner.FromIdAsync(@"\\?\ACPI#SCN00001#0#{c243ffbd-3afc-45e9-b3d3-2ba18bc7ebc5}\POSBarcodeScanner"); //DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(BarcodeScanner.GetDeviceSelector()); //if (deviceCollection != null && deviceCollection.Count > 0) //{ // foreach(DeviceInformation di in deviceCollection) // { // Class1.doLog("found: " + di.Name +", id="+di.Id); // await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // //UI code here // textBox_Scan.Text = di.Id; // }); // if (di.Id.Contains("POSBarcodeScanner")) // { // _BarcodeScanner = await BarcodeScanner.FromIdAsync(di.Id); // break; // } // } //} //else //{ // Class1.doLog("no POSBarcodeScanner found."); //} } else { await System.Threading.Tasks.Task.Delay(0); } }catch (Exception ex) { await showMessage("CreateDefaultScannerObject " + ex.Message); } if (_BarcodeScanner == null) { Class1.doLog("no POSBarcodeScanner found."); return(false); } else { Class1.doLog("POSBarcodeScanner found."); return(true); } }
/// <summary> /// Select the scanner specified by its device ID. /// </summary> /// <param name="scannerDeviceId"></param> private async Task SelectScannerAsync(string scannerDeviceId) { isSelectionChanging = true; await CloseScannerResourcesAsync(); selectedScanner = await BarcodeScanner.FromIdAsync(scannerDeviceId); if (selectedScanner != null) { claimedScanner = await selectedScanner.ClaimScannerAsync(); if (claimedScanner != null) { await claimedScanner.EnableAsync(); claimedScanner.Closed += ClaimedScanner_Closed; ScannerSupportsPreview = !String.IsNullOrEmpty(selectedScanner.VideoDeviceId); RaisePropertyChanged(nameof(ScannerSupportsPreview)); claimedScanner.DataReceived += ClaimedScanner_DataReceived; if (ScannerSupportsPreview) { await StartMediaCaptureAsync(selectedScanner.VideoDeviceId); } } else { rootPage.NotifyUser("Failed to claim the selected barcode scanner", NotifyType.ErrorMessage); } } else { rootPage.NotifyUser("Failed to create a barcode scanner object", NotifyType.ErrorMessage); } IsScannerClaimed = claimedScanner != null; RaisePropertyChanged(nameof(IsScannerClaimed)); isSelectionChanging = false; }
private async Task <bool> CreateScannerObjectFromVideo() { var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); // Return the first device found var device = (allVideoDevices.Count == 1) ? allVideoDevices[0] : null; if (allVideoDevices.Count > 1) { // try to get the back panel first device = allVideoDevices.First(w => w.EnclosureLocation.Panel == Panel.Back); if (device == null) { device = allVideoDevices[0]; } } if (device != null) { var isExternal = (device.EnclosureLocation == null || device.EnclosureLocation.Panel == Panel.Unknown || device.EnclosureLocation.InDock || device.EnclosureLocation.InLid); if (!isExternal) { if (device.EnclosureLocation.RotationAngleInDegreesClockwise > 0) { //await SetPreviewRotationAsync(); //_rotationHelper = new CameraRotationHelper(device.EnclosureLocation); //_rotationHelper.OrientationChanged += RotationHelper_OrientationChanged; } } } scanner = await BarcodeScanner.FromIdAsync(device.Id); if (scanner == null) { //rootPage.NotifyUser("Failed to create barcode scanner object.", NotifyType.ErrorMessage); return(false); } return(true); }
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))); }
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"); } }
public async Task <BarcodeScanner> GetBarcodeScannerSync() { return(await BarcodeScanner.FromIdAsync(DeviceId)); }
public static async Task <BarcodeScanner> GetFirstBarcodeScannerAsync() { return(await DeviceHelpers.GetFirstDeviceAsync(BarcodeScanner.GetDeviceSelector(), async (id) => await BarcodeScanner.FromIdAsync(id))); }