public async Task DetectScanner(string message) { try { _logger.LogInformation(message); var scanners = SystemDevices.GetScannerDevices(); _firstScanner = scanners.FirstOrDefault(); if (_firstScanner == null) { await _hubClient.CallScannerIsNotConnectedError(); return; } _logger.LogInformation($"{_firstScanner}"); await _hubClient.CallGetScannerSettings(_firstScanner); } catch (COMException ex) { var friendlyErrorMessage = ex.GetComErrorMessage(); // How to show a better error message to users _logger.LogError(ex, friendlyErrorMessage); await _hubClient.CallGetErrors(friendlyErrorMessage); } catch (Exception ex) { _logger.LogError(ex, "DetectScanner error."); await _hubClient.CallGetErrors(ex.Message); } }
private void GetAvailableDevices() { SystemDevices.Add("Single Thread"); SystemDevices.Add("Parallel CPU"); foreach (Device device in MandelbrotInstance.ContextInstance.Devices) { if (device.AcceleratorType == AcceleratorType.CPU) { _CPUDevice = device; } } SystemDevices.Add(_CPUDevice.Name); }
private void GetSystemConnectedOrPairedDevices() { try { //heart rate var guid = Guid.Parse("0000180d-0000-1000-8000-00805f9b34fb"); // SystemDevices = Adapter.GetSystemConnectedOrPairedDevices(new[] { guid }).Select(d => new DeviceListItemViewModel(d)).ToList(); // remove the GUID filter for test // Avoid to loose already IDevice with a connection, otherwise you can't close it // Keep the reference of already known devices and drop all not in returned list. var pairedOrConnectedDeviceWithNullGatt = Adapter.GetSystemConnectedOrPairedDevices(); SystemDevices.RemoveAll(sd => !pairedOrConnectedDeviceWithNullGatt.Any(p => p.Id == sd.Id)); SystemDevices.AddRange(pairedOrConnectedDeviceWithNullGatt.Where(d => !SystemDevices.Any(sd => sd.Id == d.Id)).Select(d => new DeviceListItemViewModel(d))); RaisePropertyChanged(() => SystemDevices); } catch (Exception ex) { Trace.Message("Failed to retreive system connected devices. {0}", ex.Message); } }
static void Main(string[] args) { try { // Finding the first connected scanner to the system var scanners = SystemDevices.GetScannerDevices(); var firstScanner = scanners.FirstOrDefault(); if (firstScanner == null) { Console.WriteLine("Please connect your scanner to the system and also make sure its driver is installed."); return; } Console.WriteLine($"Using {firstScanner}"); using (var scannerDevice = new ScannerDevice(firstScanner)) { scannerDevice.ScannerPictureSettings(config => { config.ColorFormat(ColorType.Color) // Optional settings .Resolution(200) .Brightness(1) .Contrast(1) .StartPosition(left: 0, top: 0) //.Extent(width: 1250 * dpi, height: 1700 * dpi) ; }); // If your scanner is a duplex or automatic document feeder, set these options scannerDevice.ScannerDeviceSettings(config => { // config.Source(DocumentSource.DoubleSided); // ... }); scannerDevice.PerformScan(WiaImageFormat.Jpeg); // An optional post processing of scanned images. // At least using its `Compress` method is recommended! scannerDevice.ProcessScannedImages(process => { process.ScaleByPixels(maximumWidth: 1000, maximumHeight: 1000, preserveAspectRatio: true) .CropByPixels(left: 10, top: 10, right: 10, bottom: 10) .RotateFlip(rotationAngle: 90, flipHorizontal: false, flipVertical: false) .Compress(quality: 90); }); var fileName = Path.Combine(Directory.GetCurrentDirectory(), "test.jpg"); foreach (var file in scannerDevice.SaveScannedImageFiles(fileName)) { Console.WriteLine($"Saved image file to: {file}"); } // Or you can access the scanned images bytes foreach (var fileBytes in scannerDevice.ExtractScannedImageFiles()) { // You can convert them to Image objects // var img = Image.FromStream(new MemoryStream(fileBytes)); Console.WriteLine($"fileBytes len: {fileBytes.Length}"); File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "test2.jpg"), fileBytes); } } } catch (COMException ex) { var friendlyErrorMessage = ex.GetComErrorMessage(); // How to show a better error message to users Console.WriteLine(friendlyErrorMessage); Console.WriteLine(ex); } }