Сканирование изображений с помощью .NET приложения в Windows и Linux
8 апреля 2024
/// <summary> /// Synchronously acquire images images from TWAIN scanner. /// </summary> public void SynchronouslyAcquireImagesFromTwainScanner() { try { using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager()) { // open the device manager deviceManager.Open(); // get reference to the default device Vintasoft.Twain.Device device = deviceManager.DefaultDevice; // open the device device.Open(); // set acquisition parameters device.TransferMode = Vintasoft.Twain.TransferMode.Memory; device.ShowUI = false; device.DisableAfterAcquire = true; device.PixelType = Vintasoft.Twain.PixelType.BW; // create directory for PDF document string directoryForImages = System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()); directoryForImages = System.IO.Path.Combine(directoryForImages, "Images"); if (!System.IO.Directory.Exists(directoryForImages)) System.IO.Directory.CreateDirectory(directoryForImages); string pdfFilename = System.IO.Path.Combine(directoryForImages, "multipage.pdf"); // acquire image(s) from the device Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None; int imageIndex = 0; do { acquireModalState = device.AcquireModal(); switch (acquireModalState) { case Vintasoft.Twain.AcquireModalState.ImageAcquired: // save image to file device.AcquiredImage.Save(pdfFilename); // dispose acquired image device.AcquiredImage.Dispose(); // output current state System.Console.WriteLine(string.Format("Image{0} is saved.", imageIndex++)); break; case Vintasoft.Twain.AcquireModalState.ScanCompleted: // output current state System.Console.WriteLine("Scan completed."); break; case Vintasoft.Twain.AcquireModalState.ScanCanceled: // output current state System.Console.WriteLine("Scan canceled."); break; case Vintasoft.Twain.AcquireModalState.ScanFailed: // output current state System.Console.WriteLine(string.Format("Scan failed: {0}", device.ErrorString)); break; } } while (acquireModalState != Vintasoft.Twain.AcquireModalState.None); // close the device device.Close(); // close the device manager deviceManager.Close(); } } catch (Vintasoft.Twain.TwainException ex) { System.Console.WriteLine("Error: " + ex.Message); } System.Console.ReadLine(); }
/// <summary> /// Synchronously acquire images images from SANE scanner. /// </summary> public void SynchronouslyAcquireImagesFromSaneScanner() { // create SANE device manager using (Vintasoft.Sane.SaneLocalDeviceManager deviceManager = new Vintasoft.Sane.SaneLocalDeviceManager()) { // open SANE device manager deviceManager.Open(); // get count of SANE devices int deviceCount = deviceManager.Devices.Count; if (deviceCount == 0) { System.Console.WriteLine("Devices are not found."); return; } // select the first SANE device Vintasoft.Sane.SaneLocalDevice device = deviceManager.Devices[0]; // open SANE device device.Open(); int imageIndex = 0; Vintasoft.Sane.SaneAcquiredImage acquiredImage; do { try { // acquire image from SANE device acquiredImage = device.AcquireImageSync(); // if image is received if (acquiredImage != null) { imageIndex++; string filename = string.Format("scannedImage-{0}.png", imageIndex); if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); acquiredImage.Save(filename); System.Console.WriteLine(string.Format("Acquired image is saved to a file '{0}'.", filename)); } // if image is not received else { System.Console.WriteLine("Scan is completed."); break; } } catch (System.Exception ex) { System.Console.WriteLine(string.Format("Scan is failed: {0}", ex.Message)); break; } } // while device has more images to scan while (device.HasMoreImagesToScan); // close SANE device device.Close(); // close SANE device manager deviceManager.Close(); } System.Console.ReadLine(); }