|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -import mss |
16 | 15 | import zxingcpp
|
17 | 16 | import base64
|
18 | 17 | import io
|
19 | 18 | import os
|
20 | 19 | import sys
|
21 | 20 | import subprocess # nosec
|
22 | 21 | import tempfile
|
23 |
| -from mss.exception import ScreenShotError |
| 22 | +import pyscreenshot as ImageGrab |
24 | 23 | from PIL import Image
|
25 |
| -import numpy.core.multiarray # noqa |
26 |
| - |
27 |
| - |
28 |
| -def _capture_screen(): |
29 |
| - try: |
30 |
| - with mss.mss() as sct: |
31 |
| - monitor = sct.monitors[0] # 0 is the special "all monitors" value. |
32 |
| - sct_img = sct.grab(monitor) # mss format |
33 |
| - return Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") |
34 |
| - except ScreenShotError: |
35 |
| - # One common error is that mss doesn't work with Wayland |
36 |
| - if sys.platform.startswith("linux"): |
37 |
| - # Try calling screenshot tools, with original library path |
38 |
| - env = dict(os.environ) |
39 |
| - lp = env.get("LD_LIBRARY_PATH_ORIG") |
40 |
| - if lp is not None: |
41 |
| - env["LD_LIBRARY_PATH"] = lp |
42 |
| - else: |
43 |
| - env.pop("LD_LIBRARY_PATH", None) |
44 |
| - fd, fname = tempfile.mkstemp(suffix=".png") |
45 |
| - |
46 |
| - try: |
47 |
| - # Try using gnome-screenshot |
48 |
| - rc = subprocess.call( # nosec |
49 |
| - ["gnome-screenshot", "-f", fname], env=env |
50 |
| - ) |
51 |
| - if rc == 0: |
52 |
| - return Image.open(fname) |
53 |
| - except FileNotFoundError: |
54 |
| - # Try using spectacle (KDE) |
55 |
| - try: |
56 |
| - rc = subprocess.call( # nosec |
57 |
| - ["spectacle", "-b", "-n", "-o", fname], env=env |
58 |
| - ) |
59 |
| - if rc == 0: |
60 |
| - return Image.open(fname) |
61 |
| - except FileNotFoundError: |
62 |
| - pass # Fall through to ValueError |
63 |
| - finally: |
64 |
| - os.unlink(fname) |
65 |
| - raise ValueError("Unable to capture screenshot") |
66 | 24 |
|
| 25 | +import numpy.core.multiarray # noqa |
67 | 26 |
|
68 | 27 | def scan_qr(image_data=None):
|
69 | 28 | if image_data:
|
70 | 29 | msg = base64.b64decode(image_data)
|
71 | 30 | buf = io.BytesIO(msg)
|
72 | 31 | img = Image.open(buf)
|
73 | 32 | else:
|
74 |
| - img = _capture_screen() |
| 33 | + img = ImageGrab.grab() |
75 | 34 |
|
76 | 35 | result = zxingcpp.read_barcode(img)
|
77 | 36 | if result and result.valid:
|
|
0 commit comments