fix scan_barcode

This commit is contained in:
zebra-lucky 2018-06-12 20:57:04 +03:00
parent 5fd0264e6b
commit a7f2f98df8
1 changed files with 6 additions and 3 deletions

View File

@ -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):