【问题标题】:How to Scan Multiple File in Twain如何在 Twain 中扫描多个文件
【发布时间】:2016-04-27 17:09:06
【问题描述】:

我正在用 Python 开发一个应用程序。我已将丢失的文件放入扫描仪。因此,当按下按钮时,我想扫描所有这些文档并保存到程序中。

import twain

sm = twain.SourceManager(0)
ss = sm.OpenSource()

for i in range(3):  //for ex. 3 documents in the scanner device
   ss.RequestAcquire(0,0)
   rv = ss.XferImageNatively()
   if rv:
       (handle, count) = rv
       twain.DIBToBMFile(handle, '{i}.bmp'.format(i))

当按下按钮所有文件扫描但不能保存到程序中。我有一个错误 twain.excTWCC_SEQERROR 。那么我该如何解决呢?

【问题讨论】:

    标签: python twain image-scanner


    【解决方案1】:

    发送请求后,您必须等待图像准备好。因此,需要根据TWAIN module document设置事件回调:

    SourceManager.SetCallback(pfnCallback)
    This method is used to set the callback handler. The callback handler is invoked when the TWAIN source signals our application. It can signal our application to indicate that there is data ready for us or that it wants to shutdown.
    The expected events are:
    MSG_XFERREADY (0x101) - the data source has data ready
    MSG_CLOSEDSREQ (0x0102) - Request for Application. to close DS
    MSG_CLOSEDSOK (0x0103) - Tell the Application. to save the state.
    MSG_DEVICEEVENT (0X0104) - Event specific to Source
    

    可能的代码更改:

    import twain
    
    sm = twain.SourceManager(0)
    sm.SetCallback(onTwainEvent)
    ss = sm.OpenSource()
    index = 0
    
    for i in range(3):  //for ex. 3 documents in the scanner device
       ss.RequestAcquire(0,0)
    
    def onTwainEvent(event):
        if event == twain.MSG_XFERREADY:
            saveImage()
    
    def saveImage():
        rv = ss.XferImageNatively()
        if rv:
            (handle, count) = rv
            twain.DIBToBMFile(handle, '{index}.bmp'.format(index))
            index += 1
    

    您也可以参考pytwain code

    【讨论】:

    • 如何将DIBToBMFile改成JPEG格式?
    • 就我而言,“onTwainEvent”从未被解雇过。
    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2018-03-09
    • 1970-01-01
    • 2010-11-04
    • 2023-03-14
    相关资源
    最近更新 更多