Revert "Commit windows merge with twain backend working."

This reverts commit 1326a5cf02.
This commit is contained in:
Albert Cervera i Areny 2008-05-24 01:09:54 +02:00
parent 1326a5cf02
commit 44618101d5
6 changed files with 119 additions and 41 deletions

View File

@ -16,9 +16,4 @@
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from ocr import *
from barcode import *
from document import *
from template import *
from recognizer import *

View File

@ -1,24 +0,0 @@
# Copyright (C) 2008 by Albert Cervera i Areny
# albert@nan-tic.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
class ScannerError:
NoDeviceFound = 1
CouldNotOpenDevice = 2
AcquisitionError = 3
UnknownError = 4

View File

@ -19,31 +19,36 @@
import sane
# PIL Module to convert PIL Image Object to QImage
import ImageQt
from errors import *
from common import *
class Scanner(QObject):
class SynchronousScanner(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
sane.init()
self.resolution = 300
# Member of SynchronousScanner Interface
def listDevices(self):
# sane.get_devices() returns an structure like the following
# [('epson:libusb:001:004', 'Epson', 'GT-8300', 'flatbed scanner')]
[x[0] for x in sane.get_devices()]
# Member of SynchronousScanner Interface
def setResolution(self, value):
self.resolution = value
def startScan(self):
devices = self.listDevices()
if not devices:
self.emit( SIGNAL('error(int)'), ScannerError.NoDeviceFound )
return
# Member of SynchronousScanner Interface
def scan(self, name=None):
if not name:
devices = self.listDevices()
if not devices:
self.emit( SIGNAL('error(int)'), ScannerError.NoDeviceFound )
return
name = devices[0]
try:
source = sane.open( devices[0] )
source = sane.open( name )
except:
self.emit( SIGNAL('error(int)'), ScannerError.CouldNotOpenDevice )
return
@ -65,4 +70,4 @@ class Scanner(QObject):
self.emit( SIGNAL('error(int)'), ScannerError.AcquisitionError )
ScannerBackend = SynchronousScanner

BIN
backends/twain.pyd Normal file

Binary file not shown.

101
backends/twain_backend.py Normal file
View File

@ -0,0 +1,101 @@
import twain, struct, string
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from common import *
import common
class SynchronousScanner(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.manager = None
self.source = None
self.resolution = 300
def stripNull(self, s):
offset = string.find(s, '\0')
if s != -1:
s= s[:offset]
return s
# Member of SynchronousScanner Interface
def setResolution(self, value):
self.resolution = value
# Member of SynchronousScanner Interface
def listDevices(self):
manager = twain.SourceManager(0L)
fmtString = "L42sHH4s34s34s34s"
slen = struct.calcsize(fmtString)
self.identity = struct.pack("%ds" % slen, "")
rv = manager.DSM_Entry(twain.DG_CONTROL, twain.DAT_IDENTITY, twain.MSG_GETFIRST, self.identity)
l = []
while rv == twain.TWRC_SUCCESS:
l.append( self.stripNull( self.identity[122:] ) )
rv = manager.DSM_Entry(twain.DG_CONTROL, twain.DAT_IDENTITY, twain.MSG_GETNEXT, self.identity)
return l
def open(self, name):
self.manager = twain.SourceManager( 0, ProductName=name )
if not self.manager:
return
if self.source:
self.source.destroy()
self.source=None
self.source = self.manager.OpenSource()
if self.source:
print "%s: %s" % ( name, self.source.GetSourceName() )
# Member of SynchronousScanner Interface
def scan(self, name=None):
if not name:
l = self.listDevices()
if not l:
print "No device found"
return common.ScannerError.NoDeviceFound
name = l[0]
try:
self.open(name)
except:
return common.ScannerError.CouldNotOpenDevice
if not self.source:
return common.ScannerError.CouldNotOpenDevice
try:
self.source.SetCapability( twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, float(self.resolution) )
self.source.SetCapability( twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, float(self.resolution) )
except:
print "Could not set resolution to '%s'" % self.resolution
pass
try:
self.source.RequestAcquire(0, 0)
except:
return common.ScannerError.AcquisitionError
while self.next():
image = self.capture()
self.emit( SIGNAL('scanned(QImage)'), image )
self.source = None
def next(self):
try:
self.source.GetImageInfo()
return True
except:
return False
def capture(self):
fileName = "tmp.tmp"
(handle, more_to_come) = self.source.XferImageNatively()
twain.DIBToBMFile(handle, fileName)
twain.GlobalHandleFree(handle)
return QImage( fileName )
def close(self):
del self.manager
common.ScannerBackend = SynchronousScanner

View File

@ -16,9 +16,10 @@
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
from backends.common import *
if os.name == 'nt':
from backends.twain import *
else.
from backends.sane import *
from backends.twain_backend import *
else:
from backends.sane_backend import *