home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Freelog 116
/
FreelogNo116-JuilletSeptembre2013.iso
/
Bureautique
/
gImageReader
/
gimagereader_0.9-1_win32.exe
/
bin
/
gimagereader
/
acquire.pyc
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2011-03-24
|
11KB
|
326 lines
# Source Generated with Decompyle++
# File: in.pyc (Python 2.7)
import os
import sys
import gtk
import gobject
import subprocess
import json
import time
import gettext
_ = gettext.gettext
import dialogs
import threadtools
class SaneAPI:
def __init__(self):
sane.init()
self.devices = None
self.device = None
self.img = None
def get_devices(self):
''' Returns a list of devices. '''
self.devices = sane.get_devices()
return [ device[2] for device in self.devices ]
def open_device(self, idx):
''' Attempts to load the specified device. '''
try:
self.device = sane.open(self.devices[idx][0])
return (True, None)
except sane._sane.error:
e = None
return (False, '%s:\n%s' % (_('Failed to open the selected device'), e))
def close_device(self):
''' Closes the current device. '''
if self.device != None:
self.device.close()
self.device = None
def scan(self, output, resolution, mode):
''' Acquires an image from the current device with specified resolution and mode. '''
self.device.resolution = resolution
self.device.mode = mode
try:
img = self.device.scan()
except sane._sane.error:
e = None
return (False, '%s:\n%s' % (_('Failed to retreive data'), e))
try:
img.save(output)
except Exception:
e = None
return (False, '%s:\n%s' % (_('Failed to save image'), e))
return (True, None)
def cancel_scan(self):
''' Cancels the scan. '''
self.device.cancel()
class TwainAPI:
def __init__(self):
self.device = None
self.devices = None
def get_devices(self):
''' Returns a list of devices. '''
try:
self.sm.destroy()
except:
pass
self.sm = twain.SourceManager(0)
self.devices = self.sm.GetSourceList()
return self.devices
def open_device(self, idx):
''' Attempts to load the specified device. '''
try:
self.device = self.sm.OpenSource(self.devices[idx])
return (True, None)
except Exception:
e = None
return (False, '%s:\n%s' % (_('Failed to open the selected device'), e))
def close_device(self):
''' Closes the current device. '''
try:
self.device.destroy()
except Exception:
e = None
def scan(self, output, resolution, mode):
''' Acquires an image from the current device with specified resolution and mode. '''
ext = os.path.splitext(output)[1].lower()
try:
if ext == '.jpg' or ext == '.jpeg':
self.device.SetXferFileName(output, twain.TWFF_JFIF)
elif ext == '.tif' or ext == '.tiff':
self.device.SetXferFileName(output, twain.TWFF_TIFF)
else:
self.device.SetXferFileName(output, twain.TWFF_PNG)
except Exception:
e = None
return (False, '%s:\n%s' % (_('Failed to set output file'), e))
try:
self.device.SetCapability(twain.ICAP_XRESOLUTION, twain.TWTY_FIX32, resolution)
self.device.SetCapability(twain.ICAP_YRESOLUTION, twain.TWTY_FIX32, resolution)
except:
pass
try:
self.device.RequestAcquire(0, 1)
while not self.device.GetImageInfo():
time.sleep(0.5)
self.device.XferImageByFile()
except Exception:
e = None
return (False, '%s:\n%s' % (_('Failed to retreive data'), e))
return (True, None)
def cancel_scan(self):
''' Cancels the scan. '''
try:
self.device.CancelAllPendingXfers()
except Exception:
e = None
try:
import sane
scan_api = SaneAPI()
except:
try:
import twain
scan_api = TwainAPI()
scan_api = None
print >>sys.stderr, 'Could not load sane or twain, scanning will not work.'
class Acquire:
def __init__(self, builder, main):
if scan_api == None:
builder.get_object('tb_acquire').hide()
builder.get_object('menu_acquire').hide()
builder.get_object('table_acquire').hide()
return None
self.main = None
self.devices = []
self.resolutions = (75, 100, 200, 300, 600, 1200)
self.output = os.path.join(os.path.expanduser('~'), 'scan1.png')
self.dialog_output = dialogs.image_save_dialog(main.window)
self.scan_count = 1
while os.path.exists(self.output):
self.scan_count += 1
self.output = os.path.join(os.path.expanduser('~'), 'scan' + str(self.scan_count) + '.png')
self.notebook_sources = builder.get_object('notebook_sources')
self.menu_toggle_sources = builder.get_object('menu_toggle_sources')
self.button_scan = builder.get_object('button_acquire_scan')
self.label_messages = builder.get_object('label_acquire_messages')
self.label_output = builder.get_object('label_acquire_outputfile')
self.button_output = builder.get_object('button_acquire_output')
self.hbox_source = builder.get_object('hbox_acquire_source')
self.button_refresh = builder.get_object('button_acquire_refresh')
self.spinner = threadtools.Spinner()
self.hbuttonbox = builder.get_object('hbuttonbox_acquire')
self.button_cancel = gtk.Button(stock = gtk.STOCK_CANCEL)
self.button_cancel.show()
self._Acquire__set_output(None, self.output)
self.combo_source = gtk.combo_box_new_text()
builder.get_object('hbox_acquire_source').pack_start(self.combo_source)
self.combo_source.show()
self.combo_resolution = gtk.combo_box_new_text()
builder.get_object('table_acquire').attach(self.combo_resolution, 1, 2, 2, 3, xoptions = gtk.EXPAND | gtk.FILL, yoptions = gtk.FILL)
self.combo_resolution.show()
for resolution in self.resolutions:
self.combo_resolution.append_text('%d dpi' % resolution)
self.combo_resolution.set_active(2)
builder.get_object('tb_acquire').connect('clicked', self._Acquire__show)
builder.get_object('menu_acquire').connect('activate', self._Acquire__show)
self.button_refresh.connect('clicked', self._Acquire__load_devices)
self.button_output.connect('clicked', self._Acquire__set_output)
self.button_scan.connect('clicked', self._Acquire__scan)
self.button_cancel.connect('clicked', self._Acquire__scan_cancel)
self.spinner.connect('size-allocate', self._Acquire__set_spinner_size)
self._Acquire__load_devices()
def __set_spinner_size(self, widget, allocation):
''' Called when the spinner needs to be resized. '''
self.spinner.set_size_request(allocation.height - 4, allocation.height - 4)
def __show(self, widget = None):
''' Shows the acquire UI in the left pane. '''
self.menu_toggle_sources.set_active(True)
self.notebook_sources.set_current_page(1)
def __load_devices(self, widget = None):
''' Initiates loading devices. '''
self.label_messages.set_markup('')
self.combo_source.get_model().clear()
self.button_scan.set_sensitive(False)
self.hbox_source.remove(self.button_refresh)
self.hbox_source.pack_end(self.spinner, False)
self.spinner.start()
threadtools.SubprocessThread(self._Acquire__load_devices_do, self._Acquire__load_devices_end).start()
def __load_devices_do(self):
''' Queries devices. '''
self.devices = scan_api.get_devices()
if not self.devices:
return False
return None
def __load_devices_end(self, result):
''' Called when done loading devices. '''
self.spinner.stop()
self.hbox_source.remove(self.spinner)
self.hbox_source.pack_end(self.button_refresh, False)
if result == False:
self.label_messages.set_markup('<span color="red">%s</span>' % _('No scanners were detected.'))
return None
None.button_scan.set_sensitive(True)
for device in self.devices:
self.combo_source.append_text(device)
self.combo_source.set_active(0)
def __scan(self, widget = None):
''' Starts the scan process. '''
self.hbuttonbox.remove(self.button_scan)
self.button_cancel.set_sensitive(False)
self.hbuttonbox.pack_start(self.button_cancel)
self.label_messages.set_text(_('Opening device...'))
threadtools.SubprocessThread(self._Acquire__scan_do, self._Acquire__scan_done).start()
def __scan_do(self):
''' Performs the scan. '''
ret = scan_api.open_device(self.combo_source.get_active())
if ret[0] == False:
self.label_messages.set_markup('<span color="red">%s</span>' % ret[1])
return False
None.label_messages.set_text(_('Transferring data...'))
self.button_cancel.set_sensitive(True)
ret = scan_api.scan(self.output, self.resolutions[self.combo_resolution.get_active()], 'color')
if ret[0] == False:
self.label_messages.set_markup('<span color="red">%s</span>' % ret[1])
scan_api.close_device()
return ret[0]
def __scan_done(self, result):
''' Called when scan is complete. '''
self.hbuttonbox.remove(self.button_cancel)
self.hbuttonbox.pack_start(self.button_scan)
if result == False:
return None
None.label_messages.set_text('')
self.main.get_filelist().open_images(None, [
self.output])
self.scan_count += 1
self.output = os.path.join(os.path.dirname(self.output), 'scan' + str(self.scan_count) + '.png')
self._Acquire__set_output(None, self.output)
def __scan_cancel(self, widget = None):
''' Callback to cancel scan button. '''
scan_api.cancel_scan()
def __set_output(self, widget = None, path = None):
''' Callback to output file chooser. '''
if path == None:
path = self.dialog_output.run(self.output)
if path != None:
self.output = path
self.label_output.set_text(path)
self.button_output.set_tooltip_text(self.output)