win binary: build zbar ourselves (#6593)
This allows bundling much newer zbar that includes many fixes.
related: #6018
This is largely based on
https://github.com/Electron-Cash/Electron-Cash/pull/1362
https://github.com/Electron-Cash/Electron-Cash/pull/1363
https://github.com/Electron-Cash/Electron-Cash/pull/1365
eda015908e
This commit is contained in:
@@ -27,6 +27,12 @@ import os
|
||||
import sys
|
||||
import ctypes
|
||||
|
||||
from .logging import get_logger
|
||||
|
||||
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
|
||||
if sys.platform == 'darwin':
|
||||
name = 'libzbar.dylib'
|
||||
elif sys.platform in ('windows', 'win32'):
|
||||
@@ -35,25 +41,27 @@ else:
|
||||
name = 'libzbar.so.0'
|
||||
|
||||
try:
|
||||
libzbar = ctypes.cdll.LoadLibrary(name)
|
||||
except BaseException:
|
||||
libzbar = None
|
||||
libzbar = ctypes.cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), name))
|
||||
except BaseException as e1:
|
||||
try:
|
||||
libzbar = ctypes.cdll.LoadLibrary(name)
|
||||
except BaseException as e2:
|
||||
libzbar = None
|
||||
if sys.platform != 'darwin':
|
||||
_logger.error(f"failed to load zbar. exceptions: {[e1,e2]!r}")
|
||||
|
||||
|
||||
def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False, try_again=True):
|
||||
def scan_barcode_ctypes(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 = ctypes.c_char_p
|
||||
libzbar.zbar_processor_create.restype = ctypes.POINTER(ctypes.c_int)
|
||||
libzbar.zbar_processor_get_results.restype = ctypes.POINTER(ctypes.c_int)
|
||||
libzbar.zbar_symbol_set_first_symbol.restype = ctypes.POINTER(ctypes.c_int)
|
||||
# libzbar.zbar_set_verbosity(100) # verbose logs for debugging
|
||||
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:
|
||||
# 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)
|
||||
raise RuntimeError("Can not start QR scanner; initialization failed.")
|
||||
libzbar.zbar_processor_set_visible(proc)
|
||||
if libzbar.zbar_process_one(proc, timeout):
|
||||
|
||||
Reference in New Issue
Block a user