1
0

use ctypes to access the zbar library

This commit is contained in:
ThomasV
2017-02-17 20:56:38 +01:00
parent 90464b6ac1
commit d99855f060
4 changed files with 49 additions and 39 deletions

View File

@@ -24,43 +24,42 @@
# SOFTWARE.
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:
import zbar
except ImportError:
zbar = None
proc = None
libzbar = cdll.LoadLibrary(name)
except OSError:
libzbar = None
def scan_qr(config):
global proc
if not zbar:
raise RuntimeError("\n".join([_("Cannot start QR scanner."),_("The zbar package is not available."),_("On Linux, try 'sudo pip install zbar'")]))
if proc is None:
device = config.get("video_device", "default")
if device == 'default':
device = ''
_proc = zbar.Processor()
_proc.init(video_device=device)
# set global only if init did not raise an exception
proc = _proc
proc.visible = True
while True:
try:
proc.process_one()
except Exception:
# User closed the preview window
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 scan_barcode(device='', timeout=-1, display=True, threaded=False):
if libzbar is None:
raise RuntimeError("Cannot start QR scanner; zbar not available.")
libzbar.zbar_symbol_get_data.restype = c_char_p
proc = libzbar.zbar_processor_create(threaded)
libzbar.zbar_processor_request_size(proc, 640, 480)
libzbar.zbar_processor_init(proc, device, display)
libzbar.zbar_processor_set_visible(proc)
if libzbar.zbar_process_one(proc, timeout):
symbols = libzbar.zbar_processor_get_results(proc)
else:
symbols = None
libzbar.zbar_processor_destroy(proc)
if symbols is None:
return
if not libzbar.zbar_symbol_set_get_size(symbols):
return
symbol = libzbar.zbar_symbol_set_first_symbol(symbols)
data = libzbar.zbar_symbol_get_data(symbol)
return data
def _find_system_cameras():
device_root = "/sys/class/video4linux"
@@ -71,3 +70,7 @@ def _find_system_cameras():
name = name.strip('\n')
devices[name] = os.path.join("/dev",device)
return devices
if __name__ == "__main__":
print(scan_barcode())

View File

@@ -232,6 +232,13 @@ class SimpleConfig(PrintError):
fee_rate = self.get('fee_per_kb', self.max_fee_rate()/2)
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):
"""Parse and return the system config settings in /etc/electrum.conf."""
result = {}