use ctypes to access the zbar library
This commit is contained in:
parent
90464b6ac1
commit
d99855f060
|
@ -2001,7 +2001,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
||||||
def read_tx_from_qrcode(self):
|
def read_tx_from_qrcode(self):
|
||||||
from electrum import qrscanner
|
from electrum import qrscanner
|
||||||
try:
|
try:
|
||||||
data = qrscanner.scan_qr(self.config)
|
data = qrscanner.scan_barcode(self.config.get_video_device())
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.show_error(str(e))
|
self.show_error(str(e))
|
||||||
return
|
return
|
||||||
|
@ -2517,9 +2517,9 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, PrintError):
|
||||||
#combo.addItem("Manually specify a device", config.get("video_device"))
|
#combo.addItem("Manually specify a device", config.get("video_device"))
|
||||||
index = qr_combo.findData(self.config.get("video_device"))
|
index = qr_combo.findData(self.config.get("video_device"))
|
||||||
qr_combo.setCurrentIndex(index)
|
qr_combo.setCurrentIndex(index)
|
||||||
msg = _("Install the zbar package to enable this.\nOn linux, type: 'apt-get install python-zbar'")
|
msg = _("Install the zbar package to enable this.")
|
||||||
qr_label = HelpLabel(_('Video Device') + ':', msg)
|
qr_label = HelpLabel(_('Video Device') + ':', msg)
|
||||||
qr_combo.setEnabled(qrscanner.zbar is not None)
|
qr_combo.setEnabled(qrscanner.libzbar is not None)
|
||||||
on_video_device = lambda x: self.config.set_key("video_device", str(qr_combo.itemData(x).toString()), True)
|
on_video_device = lambda x: self.config.set_key("video_device", str(qr_combo.itemData(x).toString()), True)
|
||||||
qr_combo.currentIndexChanged.connect(on_video_device)
|
qr_combo.currentIndexChanged.connect(on_video_device)
|
||||||
gui_widgets.append((qr_label, qr_combo))
|
gui_widgets.append((qr_label, qr_combo))
|
||||||
|
|
|
@ -49,12 +49,12 @@ class ScanQRTextEdit(ButtonsTextEdit, MessageBoxMixin):
|
||||||
def qr_input(self):
|
def qr_input(self):
|
||||||
from electrum import qrscanner, get_config
|
from electrum import qrscanner, get_config
|
||||||
try:
|
try:
|
||||||
data = qrscanner.scan_qr(get_config())
|
data = qrscanner.scan_barcode(get_config().get_video_device())
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.show_error(str(e))
|
self.show_error(str(e))
|
||||||
return ""
|
data = ''
|
||||||
if type(data) != str:
|
if type(data) != str:
|
||||||
return
|
data = ''
|
||||||
self.setText(data)
|
self.setText(data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
|
@ -24,43 +24,42 @@
|
||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from i18n import _
|
import sys
|
||||||
|
from ctypes import cdll, c_char_p
|
||||||
|
|
||||||
|
if sys.platform == 'darwin':
|
||||||
|
name = 'libzbar.dylib'
|
||||||
|
elif sys.platform == 'windows':
|
||||||
|
name = 'libzbar.dll'
|
||||||
|
else:
|
||||||
|
name = 'libzbar.so.0'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import zbar
|
libzbar = cdll.LoadLibrary(name)
|
||||||
except ImportError:
|
except OSError:
|
||||||
zbar = None
|
libzbar = None
|
||||||
|
|
||||||
proc = None
|
|
||||||
|
|
||||||
|
|
||||||
def scan_qr(config):
|
def scan_barcode(device='', timeout=-1, display=True, threaded=False):
|
||||||
global proc
|
if libzbar is None:
|
||||||
if not zbar:
|
raise RuntimeError("Cannot start QR scanner; zbar not available.")
|
||||||
raise RuntimeError("\n".join([_("Cannot start QR scanner."),_("The zbar package is not available."),_("On Linux, try 'sudo pip install zbar'")]))
|
libzbar.zbar_symbol_get_data.restype = c_char_p
|
||||||
if proc is None:
|
proc = libzbar.zbar_processor_create(threaded)
|
||||||
device = config.get("video_device", "default")
|
libzbar.zbar_processor_request_size(proc, 640, 480)
|
||||||
if device == 'default':
|
libzbar.zbar_processor_init(proc, device, display)
|
||||||
device = ''
|
libzbar.zbar_processor_set_visible(proc)
|
||||||
_proc = zbar.Processor()
|
if libzbar.zbar_process_one(proc, timeout):
|
||||||
_proc.init(video_device=device)
|
symbols = libzbar.zbar_processor_get_results(proc)
|
||||||
# set global only if init did not raise an exception
|
else:
|
||||||
proc = _proc
|
symbols = None
|
||||||
|
libzbar.zbar_processor_destroy(proc)
|
||||||
|
if symbols is None:
|
||||||
proc.visible = True
|
return
|
||||||
while True:
|
if not libzbar.zbar_symbol_set_get_size(symbols):
|
||||||
try:
|
return
|
||||||
proc.process_one()
|
symbol = libzbar.zbar_symbol_set_first_symbol(symbols)
|
||||||
except Exception:
|
data = libzbar.zbar_symbol_get_data(symbol)
|
||||||
# User closed the preview window
|
return data
|
||||||
return ""
|
|
||||||
for r in proc.results:
|
|
||||||
if str(r.type) != 'QRCODE':
|
|
||||||
continue
|
|
||||||
# hiding the preview window stops the camera
|
|
||||||
proc.visible = False
|
|
||||||
return r.data
|
|
||||||
|
|
||||||
def _find_system_cameras():
|
def _find_system_cameras():
|
||||||
device_root = "/sys/class/video4linux"
|
device_root = "/sys/class/video4linux"
|
||||||
|
@ -71,3 +70,7 @@ def _find_system_cameras():
|
||||||
name = name.strip('\n')
|
name = name.strip('\n')
|
||||||
devices[name] = os.path.join("/dev",device)
|
devices[name] = os.path.join("/dev",device)
|
||||||
return devices
|
return devices
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(scan_barcode())
|
||||||
|
|
|
@ -232,6 +232,13 @@ class SimpleConfig(PrintError):
|
||||||
fee_rate = self.get('fee_per_kb', self.max_fee_rate()/2)
|
fee_rate = self.get('fee_per_kb', self.max_fee_rate()/2)
|
||||||
return fee_rate
|
return fee_rate
|
||||||
|
|
||||||
|
def get_video_device(self):
|
||||||
|
device = self.get("video_device", "default")
|
||||||
|
if device == 'default':
|
||||||
|
device = ''
|
||||||
|
return device
|
||||||
|
|
||||||
|
|
||||||
def read_system_config(path=SYSTEM_CONFIG_PATH):
|
def read_system_config(path=SYSTEM_CONFIG_PATH):
|
||||||
"""Parse and return the system config settings in /etc/electrum.conf."""
|
"""Parse and return the system config settings in /etc/electrum.conf."""
|
||||||
result = {}
|
result = {}
|
||||||
|
|
Loading…
Reference in New Issue