Windows および Linux 用コンソール .NET アプリケーションを使用して画像をスキャン

ブログ カテゴリ: TWAIN.NET

2024/04/08

TWAIN は、プログラムと画像スキャナー間のやり取りを定義する標準プロトコルおよびインターフェイス (API) です。TWAIN 標準は、TWAIN ワーキング グループによって開発されています。TWAIN 標準のバージョン 1 では、Windows 用画像スキャナーとのやり取りが定義されています。TWAIN 標準のバージョン 2 では、Windows、macOS、Linux 用画像スキャナーとのやり取りが定義されています。
TWAIN 標準は、Windows 用画像スキャナーの最も一般的な標準です。ほとんどすべての画像スキャナーには、Windows で使用するための TWAIN ドライバーがあります。
TWAIN規格はLinuxではそれほど普及しておらず、当社の調査(2024年初頭時点)によると、Linuxでイメージスキャナーを使用するためのTWAINドライバーを提供しているのはKodakのみでした。

SANEは、ラスターイメージスキャンデバイス(フラットベッドスキャナー、ハンドヘルドスキャナーなど)への標準化されたアクセスを提供するアプリケーションプログラミングインターフェース(API)です。SA​​NE APIはパブリックドメインであり、一般の議論や開発に開放されています。SANE APIは、Linuxで最も人気のあるイメージスキャナーAPIです。
多くのスキャナーメーカーが、自社のイメージスキャナー用のSANEドライバーを開発しています。SANEコミュニティによって開発されたSANEドライバーもあります。そのため、ほとんどの最新のイメージスキャナーには、Linuxでデバイスを使用できるようにするSANEドライバーが搭載されています。

VintaSoft Twain .NET SDKは、WindowsおよびLinux用の.NETアプリケーションでTWAINまたはSANEスキャナーを制御し、画像を取得できるプロフェッショナルなイメージスキャンライブラリです。取得した画像は前処理してファイルに保存したり、HTTP(S)またはFTPサーバーにアップロードしたりできます。

VintaSoft Twain .NET SDK は、Windows および Linux で TWAIN デバイスを操作するための VintaSoft TWAIN .NET API を提供します。また、VintaSoft Twain .NET SDKは、LinuxでSANEデバイスを操作するためのVintaSoft SANE .NET APIも提供しています。VintaSoft TWAIN .NET APIとVintaSoft SANE .NET APIの詳細については、.NET開発者向けドキュメントをご覧ください:https://www.vintasoft.com/docs/vstwain-dotnet/

Windows および Linux で TWAIN イメージ スキャナーからイメージを取得する方法を示す C# コードは次のとおりです。
/// <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();
}


Linux で SANE イメージ スキャナーからイメージを取得する方法を示す C# コードは次のとおりです。
/// <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();
}