diff --git a/lib/qrscanner.py b/lib/qrscanner.py index 56e0a18a..e7acf2a2 100644 --- a/lib/qrscanner.py +++ b/lib/qrscanner.py @@ -25,6 +25,7 @@ import os import sys +import time import ctypes if sys.platform == 'darwin': @@ -40,7 +41,7 @@ except BaseException: libzbar = None -def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again=True): +def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_cnt=10): if libzbar is None: raise RuntimeError("Cannot start QR scanner; zbar not available.") libzbar.zbar_symbol_get_data.restype = ctypes.c_char_p @@ -50,10 +51,12 @@ def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again= proc = libzbar.zbar_processor_create(threaded) libzbar.zbar_processor_request_size(proc, 640, 480) if libzbar.zbar_processor_init(proc, device.encode('utf-8'), display) != 0: - if try_again: + if try_cnt > 0: + try_cnt -= 1 + time.sleep(0.1) # workaround for a bug in "ZBar for Windows" # libzbar.zbar_processor_init always seem to fail the first time around - return scan_barcode(device, timeout, display, threaded, try_again=False) + return scan_barcode(device, timeout, display, threaded, try_cnt) raise RuntimeError("Can not start QR scanner; initialization failed.") libzbar.zbar_processor_set_visible(proc) if libzbar.zbar_process_one(proc, timeout):