electrum-bitcoinprivate/lib/qrscanner.py

92 lines
3.4 KiB
Python
Raw Normal View History

2016-02-23 02:36:42 -08:00
#!/usr/bin/env python
#
2019-12-24 03:21:35 -08:00
# Electrum - lightweight bitcoinprivate client
2016-02-23 02:36:42 -08:00
# Copyright (C) 2015 Thomas Voegtlin
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
2014-08-23 08:45:47 -07:00
import os
2017-02-17 11:56:38 -08:00
import sys
2018-06-12 10:57:04 -07:00
import time
2017-08-25 02:23:11 -07:00
import ctypes
2014-08-23 08:45:47 -07:00
2017-02-17 11:56:38 -08:00
if sys.platform == 'darwin':
name = 'libzbar.dylib'
elif sys.platform in ('windows', 'win32'):
name = 'libzbar-0.dll'
2017-02-17 11:56:38 -08:00
else:
name = 'libzbar.so.0'
2017-02-17 11:56:38 -08:00
try:
2017-08-25 02:23:11 -07:00
libzbar = ctypes.cdll.LoadLibrary(name)
except BaseException:
2017-02-17 11:56:38 -08:00
libzbar = None
2015-03-04 22:49:07 -08:00
2014-10-24 08:11:05 -07:00
2018-06-12 10:57:04 -07:00
def scan_barcode(device='', timeout=-1, display=True, threaded=False, try_cnt=10):
2017-02-17 11:56:38 -08:00
if libzbar is None:
raise RuntimeError("Cannot start QR scanner; zbar not available.")
2017-08-25 02:23:11 -07:00
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)
2017-02-17 11:56:38 -08:00
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:
2018-06-12 10:57:04 -07:00
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
2018-06-12 10:57:04 -07:00
return scan_barcode(device, timeout, display, threaded, try_cnt)
raise RuntimeError("Can not start QR scanner; initialization failed.")
2017-02-17 11:56:38 -08:00
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.decode('utf8')
2014-08-23 08:45:47 -07:00
def _find_system_cameras():
device_root = "/sys/class/video4linux"
devices = {} # Name -> device
if os.path.exists(device_root):
for device in os.listdir(device_root):
2017-07-13 02:28:37 -07:00
try:
2017-11-12 05:33:46 -08:00
with open(os.path.join(device_root, device, 'name')) as f:
name = f.read()
2017-07-13 02:28:37 -07:00
except IOError:
continue
2014-08-23 08:45:47 -07:00
name = name.strip('\n')
2017-07-13 02:28:37 -07:00
devices[name] = os.path.join("/dev", device)
2014-08-23 08:45:47 -07:00
return devices
2017-02-17 11:56:38 -08:00
if __name__ == "__main__":
print(scan_barcode())