denisenkom / pytwain

TWAIN library using ctypes bindings
Other
45 stars 23 forks source link

ss.request_acquire(show_ui=True, modal_ui=False) #38

Open Dhruv00710 opened 11 months ago

Dhruv00710 commented 11 months ago

def test_multiple_images_scan(root_window): logging.basicConfig(level=logging.DEBUG) with twain.SourceManager(root_window) as sm: with sm.opensource() as ss: for in range(2): ss.request_acquire(show_ui=False, modal_ui=False) rv = ss.xfer_image_natively() if rv: (handle, count) = rv print(f"number of images remaining: {count}") twain.dib_to_bm_file(handle, 'testscans/{}.bmp'.format(uuid.uuid4())) else: break ss.hide_ui() sir using above code when ss.request_acquire(show_ui=True, modal_ui=False) or ss.request_acquire(show_ui=True, modal_ui=True) in both situation. its give error like INFO:main:Creating source manager INFO:lowlevel:attempting to load dll: C:\Windows\twain_32.dll INFO:lowlevel:DSM initialized INFO:main:Opening source INFO:lowlevel:starting source selection dialog INFO:lowlevel:user selected source with id 827 INFO:lowlevel:opening data source with id 827 INFO:main:Request acquire INFO:lowlevel:starting scan INFO:main:Transferring image INFO:lowlevel:closing data source with id 827 ERROR:main:Error: module 'twain.exceptions' has no attribute 'DSTransferCancelled'

denisenkom commented 11 months ago

Can you provide full stack trace?

Dhruv00710 commented 11 months ago

import sys import os import logging import twain from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit, QVBoxLayout, QWidget, QLabel

class ScannerApp(QMainWindow): def init(self): super().init()

    self.setWindowTitle('Scanner App')
    self.setGeometry(100, 100, 400, 300)

    self.central_widget = QWidget(self)
    self.setCentralWidget(self.central_widget)

    self.layout = QVBoxLayout(self.central_widget)

    self.scan_button = QPushButton('Scan', self)
    self.scan_button.clicked.connect(self.scan)
    self.layout.addWidget(self.scan_button)

    self.log_label = QLabel('Log:')
    self.layout.addWidget(self.log_label)

    self.log_text = QTextEdit(self)
    self.layout.addWidget(self.log_text)

    self.logger = logging.getLogger(__name__)
    logging.basicConfig(level=logging.INFO)

def scan(self):
    parent_window_handle = int(self.winId())
    self.logger.info('Creating source manager')

    try:
        with twain.SourceManager(parent_window_handle) as sm:
            self.logger.info('Opening source')
            ss = sm.open_source()
            if not ss:
                self.logger.error('Failed to open a scanner source')
                return None
            self.logger.info('Request acquire')
            ss.request_acquire(show_ui=True, modal_ui=True)
            handles = []
            more = 1
            try:
                self.logger.info('Transferring image')
                handle, more = ss.xfer_image_natively()
                handles.append(handle)
            except twain.exceptions.DSTransferCancelled:
                self.logger.info('Cancelled')
                pass
            while more != 0:
                try:
                    self.logger.info('Transferring image')
                    handle, more = ss.xfer_image_natively()
                    handles.append(handle)
                except twain.exceptions.DSTransferCancelled:
                    self.logger.info('Cancelled')
                    more = 0
            self.logger.info('Transfer complete')
            index = 0
            curr_folder = os.path.dirname(__file__)  # Use __file__ to get the current script's directory
            for handle in handles:
                twain.dib_to_bm_file(handle, os.path.join(curr_folder, "testscans/{}.bmp".format(index)))
                twain.global_handle_free(handle)
                index += 1
                self.logger.info('Saved image {}'.format(index))
    except Exception as e:
        self.logger.error(f'Error: {str(e)}')

if name == 'main': app = QApplication(sys.argv) window = ScannerApp() window.show() sys.exit(app.exec_())

Dhruv00710 commented 11 months ago

stack trace is below:

Traceback (most recent call last): File "e:\GKIQ-2023\Student Entry\pytwain-master\1.py", line 42, in scan rv = ss.xfer_image_natively() ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain__init.py", line 593, in xfer_image_natively rv, handle = self._get_native_image() ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain__init__.py", line 516, in _get_native_image rv = self._call(DG_IMAGE, ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain__init__.py", line 175, in _call return self._sm._call(self._id, dg, dat, msg, buf, expected_returns) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain\init__.py", line 968, in _call raise exc twain.exceptions.excTWCC_SEQERROR

denisenkom commented 11 months ago

Looks like you are using old version, try upgrading to the latest version 2.2.1

Dhruv00710 commented 11 months ago

i upgrade my twain version but also get error

NFO:twain:attempting to load dll: C:\Windows\twain_32.dll
INFO:twain:DSM initialized
INFO:twain:starting source selection dialog
INFO:twain:user selected source with id 904
INFO:twain:opening data source with id 904
INFO:twain:starting scan
INFO:twain:closing data source with id 904
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "e:\GKIQ-2023\Student Entry\pytwain-master\tests\2.py", line 13, in button_click
    rv = ss.xfer_image_natively()
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain\__init__.py", line 597, in xfer_image_natively
    rv, handle = self._get_native_image()
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain\__init__.py", line 520, in _get_native_image
    rv = self._call(DG_IMAGE,
         ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain\__init__.py", line 179, in _call
    return self._sm._call(self._id, dg, dat, msg, buf, expected_returns)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python311-32\Lib\site-packages\twain\__init__.py", line 973, in _call
    raise exc
twain.exceptions.SequenceError

and my code is

import tkinter as tk
import twain
import uuid
import logging
app = tk.Tk()
app.title("Simple Form")
def button_click():
    logging.basicConfig(level=logging.DEBUG)
    with twain.SourceManager(app) as sm:
        with sm.open_source() as ss:
            for _ in range(2):
                ss.request_acquire(show_ui=True, modal_ui=True)
                rv = ss.xfer_image_natively()
                if rv:
                    (handle, count) = rv
                    print(f"number of images remaining: {count}")
                    twain.dib_to_bm_file(handle, 'testscans/{}.bmp'.format(uuid.uuid4()))
                else:
                    break
                ss.hide_ui()
button = tk.Button(app, text="Click Me", command=button_click)
button.pack()

app.mainloop()
Dhruv00710 commented 11 months ago

sir please solve my query.

On Tue, Oct 17, 2023 at 3:20 AM denisenkom @.***> wrote:

Looks like you are using old version, try upgrading to the latest version 2.2.1

— Reply to this email directly, view it on GitHub https://github.com/denisenkom/pytwain/issues/38#issuecomment-1765332149, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARCUBTGMXMQLZOBJWJSDZWLX7WTY7AVCNFSM6AAAAAA6AZ4EEWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRVGMZTEMJUHE . You are receiving this because you authored the thread.Message ID: @.***>

denisenkom commented 11 months ago

I tried running this program, and it worked for me. So I could not reproduce your issue.

Dhruv00710 commented 11 months ago

sir can you solve my query. i give you anydesk. because i tried but not success. so please i request to you.

denisenkom commented 10 months ago

Try enabling debug logging for twain DSM using environment variable TWAINDSM_LOG, e.g. do:

set TWAINDSM_LOG=c:/temp/twainlog.txt

Before you start your program, and then run your program from the same terminal window.

Logging only works with Twain DSM (can be downloaded here https://github.com/twain/twain-dsm/releases). Logging does not work with twain_32 which is bundled with Windows. You would need to pass path to DSM via dsm_name paramter in SourceManager constructor.

denisenkom commented 10 months ago

Were you able to get logs with the above mechanism?

Dhruv00710 commented 10 months ago

yes sir

denisenkom commented 10 months ago

Can you share logs here? Or have you already figured out your issue?

Dhruv00710 commented 10 months ago

SORRY SIR. BECAUSE AT THAT TIME I DON'T UNDERSTAND WHAT IS LOGS WITH MECHANISM. SO I REPLAY WITH YES.

ONCE AGAIN SORRY SIR.