Skip to content

yushulx/python-twain-wia-sane-scanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Document Scanner for TWAIN, WIA, SANE, ICA, and eSCL

The package provides methods for calling Dynamsoft Service REST APIs. This allows developers to build Python applications for digitizing documents from TWAIN (32-bit/64-bit), WIA, SANE, ICA and eSCL scanners.

Prerequisites

  1. Install Dynamsoft Service.

  2. Request a free trial license.

Dynamsoft Service REST API

By default, the REST API's host address is set to http://127.0.0.1:18622.

Method Endpoint Description Parameters Response
GET /DWTAPI/Scanners Get a list of scanners None 200 OK with scanner list
POST /DWTAPI/ScanJobs Creates a scan job license, device, config 201 Created with job ID
GET /DWTAPI/ScanJobs/:id/NextDocument Retrieves a document image id: Job ID 200 OK with image stream
DELETE /DWTAPI/ScanJobs/:id Deletes a scan job id: Job ID 200 OK

You can navigate to http://127.0.0.1:18625/ to access the service. To make it accessible from desktop, mobile, and web applications on the same network, you can change the host address to a LAN IP address. For example, you might use http://192.168.8.72.

dynamsoft-service-config

The scanner parameter configuration is based on Dynamsoft Web TWAIN documentation.

Quick Start

Replace the license key in the code below with a valid one and run the code.

from dynamsoftservice import ScannerController, ScannerType

scannerController = ScannerController()
devices = []
host = "http://127.0.0.1:18622"
license_key = "LICENSE-KEY"

questions = """
Please select an operation:
1. Get scanners
2. Acquire documents by scanner index
3. Quit
"""


def ask_question():
    while True:
        print(".............................................")
        answer = input(questions)

        if answer == '3':
            break
        elif answer == '1':
            scanners = scannerController.getDevices(
                host, ScannerType.TWAINSCANNER | ScannerType.TWAINX64SCANNER)
            devices.clear()
            for i, scanner in enumerate(scanners):
                devices.append(scanner)
                print(f"\nIndex: {i}, Name: {scanner['name']}")
        elif answer == '2':
            if len(devices) == 0:
                print("Please get scanners first!\n")
                continue

            index = input(f"\nSelect an index (<= {len(devices) - 1}): ")
            index = int(index)  

            if index < 0 or index >= len(devices):
                print("It is out of range.")
                continue

            parameters = {
                "license": license_key,
                "device": devices[index]["device"],
            }

            parameters["config"] = {
                "IfShowUI": False,
                "PixelType": 2,
                "Resolution": 200,
                "IfFeederEnabled": False,
                "IfDuplexEnabled": False,
            }

            job_id = scannerController.scanDocument(host, parameters)

            if job_id != "":
                images = scannerController.getImageFiles(host, job_id, "./")
                for i, image in enumerate(images):
                    print(f"Image {i}: {image}")

                scannerController.deleteJob(host, job_id)
        else:
            continue


if __name__ == "__main__":
    ask_question()

Example

DynamsoftService API

The DynamsoftService class provides methods to interact with the Dynamsoft service.

  • getDevices(self, host: str, scannerType: int = None) -> List[Any]: Get a list of available devices.
  • scanDocument(self, host: str, parameters: Dict[str, Any]) -> str: Scan a document.
  • deleteJob(self, host: str, jobId: str) -> None: Delete a job.
  • getImageFile(self, host, job_id, directory): Get an image file.
  • getImageFiles(self, host: str, jobId: str, directory: str) -> List[str]: Get a list of image files.
  • getImageStreams(self, host: str, jobId: str) -> List[bytes]: Get a list of image streams.

How to Build the Package

  • Source distribution:

    python setup.py sdist
  • Wheel:

    pip wheel . --verbose
    # Or
    python setup.py bdist_wheel

About

A Python package for programming with Dynamsoft Service, supporting scanning documents from TWAIN, WIA, ICA, SANE and eSCL scanners.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages