Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Dhruv00710 opened this issue Oct 15, 2023 · 13 comments
Open

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

Dhruv00710 opened this issue Oct 15, 2023 · 13 comments

Comments

@Dhruv00710
Copy link

def test_multiple_images_scan(root_window):
logging.basicConfig(level=logging.DEBUG)
with twain.SourceManager(root_window) as sm:
with sm.open_source() 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
Copy link
Owner

Can you provide full stack trace?

@Dhruv00710
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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

@Dhruv00710
Copy link
Author

Dhruv00710 commented Oct 17, 2023

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
Copy link
Author

Dhruv00710 commented Oct 19, 2023 via email

@denisenkom
Copy link
Owner

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

@Dhruv00710
Copy link
Author

Dhruv00710 commented Oct 21, 2023

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

@denisenkom
Copy link
Owner

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
Copy link
Owner

Were you able to get logs with the above mechanism?

@Dhruv00710
Copy link
Author

yes sir

@denisenkom
Copy link
Owner

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

@Dhruv00710
Copy link
Author

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

ONCE AGAIN SORRY SIR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants