Files
pallectrum/electrum/qrscanner.py

113 lines
4.0 KiB
Python
Raw Normal View History

2016-02-23 11:36:42 +01:00
#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# 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.
#
# A QR scanner that uses zbar (via ctypes)
# - to access the camera,
# - and to find and decode QR codes (visible in the live feed).
2016-02-23 11:36:42 +01:00
2014-08-23 17:45:47 +02:00
import os
2017-02-17 20:56:38 +01:00
import sys
2017-08-25 11:23:11 +02:00
import ctypes
from typing import Optional, Mapping
2014-08-23 17:45:47 +02:00
2021-02-23 22:38:53 +01:00
from .util import UserFacingException
from .i18n import _
from .logging import get_logger
_logger = get_logger(__name__)
2017-02-17 20:56:38 +01:00
if sys.platform == 'darwin':
name = 'libzbar.0.dylib'
2018-02-08 05:56:54 +01:00
elif sys.platform in ('windows', 'win32'):
name = 'libzbar-0.dll'
2017-02-17 20:56:38 +01:00
else:
name = 'libzbar.so.0'
2017-02-17 20:56:38 +01:00
try:
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
qt: new qrreader using QtMultimedia; drop CalinsQRReader(mac) This commit ports the work of EchterAgo and cculianu from Electron-Cash, to implement a new toolchain to scan qr codes. Previously, on Linux and Win, we have been using zbar to access the camera and read qrcodes; and on macOS we used CalinsQRReader (an objective-C project by cculianu). The new toolchain added here can use QtMultimedia to access the camera, and then feed that image into zbar. When used this way, zbar needs fewer dependencies and is easier to compile, in particular it can be compiled for macOS. The new toolchain works on all three platforms, with some caveats (see code comments in related commits) -- so we also keep the end-to-end zbar toolchain; but at least we can drop CalinsQRReader. The related changes in Electron-Cash are spread over 50+ commits (several PRs and direct pushes to master), but see in particular: https://github.com/Electron-Cash/Electron-Cash/pull/1376 some other interesting links: https://github.com/Electron-Cash/Electron-Cash/commit/b2b737001c8cc41a38fa580ea252a6d24e08f5d5 https://github.com/Electron-Cash/Electron-Cash/commit/163224cf1fad3af63f2d3cbe68a34fb8ff279af6 https://github.com/Electron-Cash/Electron-Cash/commit/3b31e0fcb13f67646228ff42c0dd39d2a0912291 https://github.com/Electron-Cash/Electron-Cash/commit/eda015908e9d6ea9a0adfbda9db55b929c0926ba https://github.com/Electron-Cash/Electron-Cash/pull/1545 https://github.com/Electron-Cash/Electron-Cash/commit/052aa06c23b939adcea07c701f70ae28ebcf9e0a
2021-06-25 15:48:22 +02:00
_logger.error(f"failed to load zbar. exceptions: {[e1,e2]!r}")
2015-03-05 07:49:07 +01:00
2014-10-24 17:11:05 +02:00
qt: new qrreader using QtMultimedia; drop CalinsQRReader(mac) This commit ports the work of EchterAgo and cculianu from Electron-Cash, to implement a new toolchain to scan qr codes. Previously, on Linux and Win, we have been using zbar to access the camera and read qrcodes; and on macOS we used CalinsQRReader (an objective-C project by cculianu). The new toolchain added here can use QtMultimedia to access the camera, and then feed that image into zbar. When used this way, zbar needs fewer dependencies and is easier to compile, in particular it can be compiled for macOS. The new toolchain works on all three platforms, with some caveats (see code comments in related commits) -- so we also keep the end-to-end zbar toolchain; but at least we can drop CalinsQRReader. The related changes in Electron-Cash are spread over 50+ commits (several PRs and direct pushes to master), but see in particular: https://github.com/Electron-Cash/Electron-Cash/pull/1376 some other interesting links: https://github.com/Electron-Cash/Electron-Cash/commit/b2b737001c8cc41a38fa580ea252a6d24e08f5d5 https://github.com/Electron-Cash/Electron-Cash/commit/163224cf1fad3af63f2d3cbe68a34fb8ff279af6 https://github.com/Electron-Cash/Electron-Cash/commit/3b31e0fcb13f67646228ff42c0dd39d2a0912291 https://github.com/Electron-Cash/Electron-Cash/commit/eda015908e9d6ea9a0adfbda9db55b929c0926ba https://github.com/Electron-Cash/Electron-Cash/pull/1545 https://github.com/Electron-Cash/Electron-Cash/commit/052aa06c23b939adcea07c701f70ae28ebcf9e0a
2021-06-25 15:48:22 +02:00
def scan_barcode(device='', timeout=-1, display=True, threaded=False) -> Optional[str]:
2017-02-17 20:56:38 +01:00
if libzbar is None:
raise UserFacingException(_('Cannot start QR scanner: zbar not available.'))
2017-08-25 11:23:11 +02: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)
# libzbar.zbar_set_verbosity(100) # verbose logs for debugging
2017-02-17 20:56:38 +01: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:
2021-02-23 22:38:53 +01:00
raise UserFacingException(
_("Cannot start QR scanner: initialization failed.") + "\n" +
_("Make sure you have a camera connected and enabled."))
2017-02-17 20:56:38 +01: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')
def find_system_cameras() -> Mapping[str, str]:
2014-08-23 17:45:47 +02:00
device_root = "/sys/class/video4linux"
devices = {} # Name -> device
if os.path.exists(device_root):
for device in os.listdir(device_root):
2018-09-04 16:42:08 +02:00
path = os.path.join(device_root, device, 'name')
2017-07-13 11:28:37 +02:00
try:
2018-09-04 16:42:08 +02:00
with open(path, encoding='utf-8') as f:
2017-11-12 14:33:46 +01:00
name = f.read()
2018-09-04 16:42:08 +02:00
except Exception:
2017-07-13 11:28:37 +02:00
continue
2014-08-23 17:45:47 +02:00
name = name.strip('\n')
2017-07-13 11:28:37 +02:00
devices[name] = os.path.join("/dev", device)
2014-08-23 17:45:47 +02:00
return devices
2017-02-17 20:56:38 +01:00
def version_info() -> Mapping[str, Optional[str]]:
return {
"libzbar.path": libzbar._name if libzbar else None,
}
2017-02-17 20:56:38 +01:00
if __name__ == "__main__":
print(scan_barcode())