[macOS] Added QR scanner facility using platform-native helper app.
This commit is contained in:
@@ -40,7 +40,7 @@ except BaseException:
|
||||
libzbar = None
|
||||
|
||||
|
||||
def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again=True):
|
||||
def scan_barcode_ctypes(device='', timeout=-1, display=True, threaded=False, try_again=True):
|
||||
if libzbar is None:
|
||||
raise RuntimeError("Cannot start QR scanner; zbar not available.")
|
||||
libzbar.zbar_symbol_get_data.restype = ctypes.c_char_p
|
||||
@@ -69,6 +69,29 @@ def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_again=
|
||||
data = libzbar.zbar_symbol_get_data(symbol)
|
||||
return data.decode('utf8')
|
||||
|
||||
def scan_barcode_osx(*args_ignored, **kwargs_ignored):
|
||||
import subprocess
|
||||
# NOTE: This code needs to be modified if the positions of this file changes with respect to the helper app!
|
||||
# This assumes the built macOS .app bundle which ends up putting the helper app in
|
||||
# .app/contrib/CalinsQRReader/build/Release/CalinsQRReader.app.
|
||||
root_ec_dir = os.path.abspath(os.path.dirname(__file__) + "/../")
|
||||
prog = root_ec_dir + "/" + "contrib/CalinsQRReader/build/Release/CalinsQRReader.app/Contents/MacOS/CalinsQRReader"
|
||||
if not os.path.exists(prog):
|
||||
raise RuntimeError("Cannot start QR scanner; helper app not found.")
|
||||
data = ''
|
||||
try:
|
||||
# This will run the "CalinsQRReader" helper app (which also gets bundled with the built .app)
|
||||
# Just like the zbar implementation -- the main app will hang until the QR window returns a QR code
|
||||
# (or is closed). Communication with the subprocess is done via stdout.
|
||||
# See contrib/CalinsQRReader for the helper app source code.
|
||||
with subprocess.Popen([prog], stdout=subprocess.PIPE) as p:
|
||||
data = p.stdout.read().decode('utf-8').strip()
|
||||
return data
|
||||
except OSError as e:
|
||||
raise RuntimeError("Cannot start camera helper app; {}".format(e.strerror))
|
||||
|
||||
scan_barcode = scan_barcode_osx if sys.platform == 'darwin' else scan_barcode_ctypes
|
||||
|
||||
def _find_system_cameras():
|
||||
device_root = "/sys/class/video4linux"
|
||||
devices = {} # Name -> device
|
||||
|
||||
Reference in New Issue
Block a user