Сканирование изображений с помощью .NET приложения для Linux
2 марта 2023
dotnet new console --framework net7.0
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <RootNamespace>ConsoleApp1</RootNamespace> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="Vintasoft.Shared" Version="3.4.0.1" /> <PackageReference Include="Vintasoft.Shared" Version="14.0.1.1" /> </ItemGroup> </Project>
using System; using System.IO; using Vintasoft.Twain; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Vintasoft.Twain.TwainGlobalSettings.Register("VintaSoft", "support@vintasoft.com", "2023-04-02", "..."); try { // create TWAIN device manager using (DeviceManager deviceManager = new DeviceManager()) { // open TWAIN device manager if (!OpenDeviceManager(deviceManager)) return; // select TWAIN device Device device = SelectDevice(deviceManager); // if device is not selected if (device == null) return; // specify that device UI should not be shown device.ShowUI = false; // specify that image scanning progess UI should not be shown device.ShowIndicators = false; // specify that device must be disabled after image scan device.DisableAfterAcquire = true; // open the device device.Open(); // set device parameters device.TransferMode = TransferMode.Native; device.PixelType = PixelType.BW; device.XferCount = 1; // create directory for TIFF file string directoryForImages = Path.GetDirectoryName(Directory.GetCurrentDirectory()); directoryForImages = Path.Combine(directoryForImages, "Images"); if (!Directory.Exists(directoryForImages)) Directory.CreateDirectory(directoryForImages); string multipageTiffFilename = Path.Combine(directoryForImages, "multipage.tif"); // acquire image(s) from the device int imageIndex = 0; AcquireModalState acquireModalState = AcquireModalState.None; do { acquireModalState = device.AcquireModal(); switch (acquireModalState) { case AcquireModalState.ImageAcquired: // save acquired image to a file device.AcquiredImage.Save(multipageTiffFilename); // dispose an acquired image device.AcquiredImage.Dispose(); Console.WriteLine(string.Format("Image{0} is saved.", imageIndex++)); break; case AcquireModalState.ScanCompleted: Console.WriteLine("Scan is completed."); break; case AcquireModalState.ScanCanceled: Console.WriteLine("Scan is canceled."); break; case AcquireModalState.ScanFailed: Console.WriteLine(string.Format("Scan is failed: {0}", device.ErrorString)); break; case AcquireModalState.UserInterfaceClosed: Console.WriteLine("User interface is closed."); break; } } while (acquireModalState != AcquireModalState.None); // close the device device.Close(); // close the device manager deviceManager.Close(); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } /// <summary> /// Opens the TWAIN device manager. /// </summary> /// <param name="deviceManager">Device manager.</param> /// <returns><b>True</b> - device manager is opened successfully; otherwise, <b>false</b>.</returns> private static bool OpenDeviceManager(DeviceManager deviceManager) { // try to use TWAIN device manager 2.x deviceManager.IsTwain2Compatible = true; // if TWAIN device manager 2.x is not available if (!deviceManager.IsTwainAvailable) { Console.WriteLine("TWAIN device manager is not available."); return false; } // open the device manager deviceManager.Open(); return true; } /// <summary> /// Selects the TWAIN device. /// </summary> /// <param name="deviceManager">TWAIN device manager.</param> /// <returns>TWAIN device if device is selected; otherwiase, <b>null</b>.</returns> private static Device SelectDevice(DeviceManager deviceManager) { int deviceCount = deviceManager.Devices.Count; // if no devices are found in the system if (deviceCount == 0) { Console.WriteLine("Devices are not found."); return null; } Console.WriteLine("Device list:"); for (int i = 0; i < deviceCount; i++) { Console.WriteLine(string.Format("{0}. {1}", i + 1, deviceManager.Devices[i].Info.ProductName)); } int deviceIndex = -1; while (deviceIndex < 0 || deviceIndex > deviceCount) { Console.Write(string.Format("Please select device by entering the device number from '1' to '{0}' or press '0' to cancel: ", deviceCount)); deviceIndex = Console.ReadKey().KeyChar - '0'; Console.WriteLine(); } Console.WriteLine(); if (deviceIndex == 0) return null; return deviceManager.Devices[deviceIndex - 1]; } } }
dotnet build Scan_Images.csproj
dotnet ./Scan_Images.dll