diff --git a/__init__.py b/__init__.py index 95c1f41..2ead901 100644 --- a/__init__.py +++ b/__init__.py @@ -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 * diff --git a/backends/errors.py b/backends/errors.py deleted file mode 100644 index 299d1e8..0000000 --- a/backends/errors.py +++ /dev/null @@ -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 - diff --git a/backends/sane.py b/backends/sane_backend.py similarity index 80% rename from backends/sane.py rename to backends/sane_backend.py index ee30554..3b8c8c4 100644 --- a/backends/sane.py +++ b/backends/sane_backend.py @@ -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 \ No newline at end of file diff --git a/backends/twain.pyd b/backends/twain.pyd new file mode 100644 index 0000000..917e18b Binary files /dev/null and b/backends/twain.pyd differ diff --git a/backends/twain_backend.py b/backends/twain_backend.py new file mode 100644 index 0000000..3f99f93 --- /dev/null +++ b/backends/twain_backend.py @@ -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 \ No newline at end of file diff --git a/scanner.py b/scanner.py index 124c70f..8482cd6 100644 --- a/scanner.py +++ b/scanner.py @@ -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 *