private void setupScanner()
 {
     device = WiaDevice.GetFirstScannerDevice().AsScannerDevice();
     // Not checking the device's capabilities: for now, we're working on the assumption that we
     // control the exact model of scanner.
     device.DeviceSettings.DocumentHandlingSelect = this.twoSided ?
         DocumentHandlingSelect.Duplex : DocumentHandlingSelect.Feeder;
     device.DeviceSettings.Pages = numPages * (twoSided ? 2 : 1);
 }
 private void setupScanner()
 {
     device = WiaDevice.GetFirstScannerDevice().AsScannerDevice();
     // Not checking the device's capabilities: for now, we're working on the assumption that we
     // control the exact model of scanner.
     device.DeviceSettings.DocumentHandlingSelect = this.twoSided ?
                                                    DocumentHandlingSelect.Duplex : DocumentHandlingSelect.Feeder;
     device.DeviceSettings.Pages = numPages * (twoSided ? 2 : 1);
 }
Beispiel #3
0
        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);
            }
        }
Beispiel #4
0
        public async Task DoScan(NewScanConfig newScanConfig)
        {
            try
            {
                if (_firstScanner == null || newScanConfig == null)
                {
                    await _hubClient.CallScannerIsNotConnectedError();

                    return;
                }

                _logger.LogInformation($"Using {_firstScanner}");
                _logger.LogInformation($"{newScanConfig.Source}, {newScanConfig.ColorFormat}, DPI:{newScanConfig.Resolution}, {newScanConfig.FileType}");

                using (var scannerDevice = new ScannerDevice(_firstScanner))
                {
                    ColorType colorType;
                    switch (newScanConfig.ColorFormat)
                    {
                    case ColorFormatType.Color:
                        colorType = ColorType.Color;
                        break;

                    case ColorFormatType.Greyscale:
                        colorType = ColorType.Greyscale;
                        break;

                    case ColorFormatType.Text:
                        colorType = ColorType.BlackAndWhite;
                        break;

                    default:
                        colorType = ColorType.Color;
                        break;
                    }
                    scannerDevice.ScannerPictureSettings(config =>
                    {
                        config.ColorFormat(colorType)
                        .Resolution(newScanConfig.Resolution)
                        .Brightness(newScanConfig.Brightness)
                        .Contrast(newScanConfig.Contrast);
                    });

                    scannerDevice.ScannerDeviceSettings(config =>
                    {
                        if (newScanConfig.Source == SourceType.Duplex)
                        {
                            config.Source(DocumentSource.DoubleSided);
                        }
                        else if (newScanConfig.Source == SourceType.AutomaticDocumentFeeder)
                        {
                            config.Source(DocumentSource.SingleSided);
                        }
                    });

                    WiaImageFormat format;
                    switch (newScanConfig.FileType)
                    {
                    case FileType.Jpeg:
                        format = WiaImageFormat.Jpeg;
                        break;

                    case FileType.Bmp:
                        format = WiaImageFormat.Bmp;
                        break;

                    case FileType.Png:
                        format = WiaImageFormat.Png;
                        break;

                    case FileType.Gif:
                        format = WiaImageFormat.Gif;
                        break;

                    case FileType.Tiff:
                        format = WiaImageFormat.Tiff;
                        break;

                    default:
                        format = WiaImageFormat.Jpeg;
                        break;
                    }
                    scannerDevice.PerformScan(format);

                    scannerDevice.ProcessScannedImages(process =>
                    {
                        process.Compress(quality: 90);
                        // ...
                    });

                    var filesBytes = scannerDevice.ExtractScannedImageFiles().ToList();
                    await _uploadApiClient.PostImagesAsync(
                        _appConfig.UploadImagesApiPath,
                        filesBytes,
                        _appConfig.UploadImagesApiParamName,
                        $".{newScanConfig.FileType.ToString().ToLowerInvariant()}");
                }
            }
            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, "Scan error.");
                await _hubClient.CallGetErrors(ex.Message);
            }
        }