Scan QR code with webcam #78

Open
opened 2025-05-20 00:48:54 +02:00 by Benjamin_Loison · 12 comments

It can be useful to share credentials in a trusted manner.

Using Termux | qrencode -t UTF8 for instance.

QRCodeScanner.py:
from cv2 import VideoCapture, barcode, imshow, waitKey, destroyAllWindows#, QRCodeDetector

cap = VideoCapture(0)
detector = barcode.BarcodeDetector() # QRCodeDetector

while True:
    _, img = cap.read()

    data, b, c = detector.detectAndDecode(img) # returns incorrect results most of time and other variables are still `None`...
    #print(pyzbar.decode(img)) # not better
    if data:
        print(data, b, c)
        a = data
        break
    imshow('QR code scanner', img)
    if waitKey(1) == ord('q'):
        break

print(a)
cap.release()
destroyAllWindows()

was taken on this Internet if I remember correctly.

It is stored on my Linux Mint 22.1 Cinnamon Framework 13 at /home/benjamin_loison/Desktop/bens_folder/dev/python/projects/QRCodeScanner/. Related to /home/benjamin_loison/Desktop/bens_folder/dev/python/projects/QRCodeReader/ taking an image like a screenshot if I remember correctly, see below.

It is quite slow and has not detected my QR code this time but it already happened significantly in the past.

Maybe could investigate with scrcpy/blob/c5ed2cfc28ee7c7b59b11eb4db1258ac1c633bff/doc/v4l2.md to have better video stream.

QRCodeReader.py:
import os
from pyzbar.pyzbar import decode
from PIL import Image

path = 'Desktop/'

os.chdir(path)

print(decode(Image.open('qr.png')))

+7

It can be useful to share credentials in a trusted manner. Using Termux `| qrencode -t UTF8` for instance. <details> <summary><code>QRCodeScanner.py</code>:</summary> ```python from cv2 import VideoCapture, barcode, imshow, waitKey, destroyAllWindows#, QRCodeDetector cap = VideoCapture(0) detector = barcode.BarcodeDetector() # QRCodeDetector while True: _, img = cap.read() data, b, c = detector.detectAndDecode(img) # returns incorrect results most of time and other variables are still `None`... #print(pyzbar.decode(img)) # not better if data: print(data, b, c) a = data break imshow('QR code scanner', img) if waitKey(1) == ord('q'): break print(a) cap.release() destroyAllWindows() ``` </details> was taken on this Internet if I remember correctly. It is stored on my Linux Mint 22.1 Cinnamon Framework 13 at `/home/benjamin_loison/Desktop/bens_folder/dev/python/projects/QRCodeScanner/`. Related to `/home/benjamin_loison/Desktop/bens_folder/dev/python/projects/QRCodeReader/` taking an image like a screenshot if I remember correctly, see below. It is quite slow and has not detected my QR code this time but it already happened significantly in the past. Maybe could investigate with [scrcpy/blob/c5ed2cfc28ee7c7b59b11eb4db1258ac1c633bff/doc/v4l2.md](https://github.com/Genymobile/scrcpy/blob/c5ed2cfc28ee7c7b59b11eb4db1258ac1c633bff/doc/v4l2.md) to have better video stream. <details> <summary><code>QRCodeReader.py</code>:</summary> ```python import os from pyzbar.pyzbar import decode from PIL import Image path = 'Desktop/' os.chdir(path) print(decode(Image.open('qr.png'))) ``` </details> +7
Author
Owner

DuckDuckGo and Google search Linux scan webcam QR code and qrscan GitHub.

DuckDuckGo and Google search *Linux scan webcam QR code* and *qrscan GitHub*.
Author
Owner

DuckDuckGo search "ERROR: no decode delegate for this image format".

DuckDuckGo search `"ERROR: no decode delegate for this image format"`.
Author
Owner

CoBang has 305 stars and last commit was last week.

[CoBang](https://github.com/hongquan/CoBang) has 305 stars and last commit was last week.
Author
Owner
https://www.reddit.com/r/linux/comments/vyshqm/comment/ig46a7t/
Author
Owner

The Ask Ubuntu question 22871 raises this question.

[The Ask Ubuntu question 22871](https://askubuntu.com/q/22871) raises this question.
Author
Owner
Related to [Benjamin_Loison/xclip/issues/1](https://codeberg.org/Benjamin_Loison/xclip/issues/1).
Author
Owner
On my Debian 12 GNOME laptop:
-----BEGIN PGP MESSAGE-----

hF4DTQa9Wom5MBgSAQdAMVQFf77a6oKYevxY2/pj0G/fvKeg9ve/0JQxJPEIJ3gw
KBkFVgszpZ32GuV7EGwfXZN6ITX/S/AY4PHanZvtqHolZ0JbkiUl0Wi6PyA9wmhJ
1GABCQIQ/SGgYP5T1oCQEiX/md2SUjWYdoqOB+O00aQKZkuGoaxKnw3nd24mVh7q
S96PkWFB/oSBvOQ4BQTm+/BGxvzfQD8m/tEPBUdb2XTrOeo3I+B2vvesaL4nPdej
Nto=
=yGfY
-----END PGP MESSAGE-----
ls /dev/video*
/dev/video0  /dev/video1
zbarimg --raw --quiet /dev/video0
Output:
Command 'zbarimg' not found, but can be installed with:
sudo apt install zbar-tools
sudo apt install -y zbar-tools
Output:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  zbarcam-gtk zbarcam-qt
The following NEW packages will be installed:
  zbar-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 36,1 kB of archives.
After this operation, 98,3 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main amd64 zbar-tools amd64 0.23.92-7+deb12u1 [36,1 kB]
Fetched 36,1 kB in 0s (123 kB/s)      
Selecting previously unselected package zbar-tools.
(Reading database ... 276854 files and directories currently installed.)
Preparing to unpack .../zbar-tools_0.23.92-7+deb12u1_amd64.deb ...
Unpacking zbar-tools (0.23.92-7+deb12u1) ...
Setting up zbar-tools (0.23.92-7+deb12u1) ...
Processing triggers for dbus (1.14.10-1~deb12u1) ...
Processing triggers for man-db (2.11.2-2) ...
zbarimg --raw --quiet /dev/video0
ERROR: no decode delegate for this image format `' @ error/constitute.c/ReadImage/580

Same with /dev/video1 and without --raw and --quiet

zbarcam

shows webcam video stream in realtime and when present a QR code, then get in the console:

QR-Code:CENSORED
<details> <summary>On my Debian 12 GNOME laptop:</summary> ``` -----BEGIN PGP MESSAGE----- hF4DTQa9Wom5MBgSAQdAMVQFf77a6oKYevxY2/pj0G/fvKeg9ve/0JQxJPEIJ3gw KBkFVgszpZ32GuV7EGwfXZN6ITX/S/AY4PHanZvtqHolZ0JbkiUl0Wi6PyA9wmhJ 1GABCQIQ/SGgYP5T1oCQEiX/md2SUjWYdoqOB+O00aQKZkuGoaxKnw3nd24mVh7q S96PkWFB/oSBvOQ4BQTm+/BGxvzfQD8m/tEPBUdb2XTrOeo3I+B2vvesaL4nPdej Nto= =yGfY -----END PGP MESSAGE----- ``` </details> ``` ls /dev/video* ``` ``` /dev/video0 /dev/video1 ``` ``` zbarimg --raw --quiet /dev/video0 ``` <details> <summary>Output:</summary> ``` Command 'zbarimg' not found, but can be installed with: sudo apt install zbar-tools ``` </details> ``` sudo apt install -y zbar-tools ``` <details> <summary>Output:</summary> ``` Reading package lists... Done Building dependency tree... Done Reading state information... Done Suggested packages: zbarcam-gtk zbarcam-qt The following NEW packages will be installed: zbar-tools 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 36,1 kB of archives. After this operation, 98,3 kB of additional disk space will be used. Get:1 http://deb.debian.org/debian bookworm/main amd64 zbar-tools amd64 0.23.92-7+deb12u1 [36,1 kB] Fetched 36,1 kB in 0s (123 kB/s) Selecting previously unselected package zbar-tools. (Reading database ... 276854 files and directories currently installed.) Preparing to unpack .../zbar-tools_0.23.92-7+deb12u1_amd64.deb ... Unpacking zbar-tools (0.23.92-7+deb12u1) ... Setting up zbar-tools (0.23.92-7+deb12u1) ... Processing triggers for dbus (1.14.10-1~deb12u1) ... Processing triggers for man-db (2.11.2-2) ... ``` </details> ``` zbarimg --raw --quiet /dev/video0 ``` ``` ERROR: no decode delegate for this image format `' @ error/constitute.c/ReadImage/580 ``` Same with `/dev/video1` and without `--raw` and `--quiet` ``` zbarcam ``` shows webcam video stream in realtime and when present a QR code, then get in the console: ``` QR-Code:CENSORED ```
Author
Owner
Related to [Benjamin_Loison/gnome-control-center/issues/48](https://gitlab.gnome.org/Benjamin_Loison/gnome-control-center/-/issues/48).
Author
Owner

Could even make a tool to propose actions like Android when scan an arbitrary QR code, for instance to also just open a URL.

Could even make a tool to propose actions like Android when scan an arbitrary QR code, for instance to also just open a URL.
Author
Owner
zbarcam --help
Output:
...
    -1, --oneshot   exit after scanning one bar code
    --nodisplay     disable video display window
...

may help.

``` zbarcam --help ``` <details> <summary>Output:</summary> ``` ... -1, --oneshot exit after scanning one bar code --nodisplay disable video display window ... ``` </details> may help.
Author
Owner
zbarcam --oneshot | sed 's/QR-Code://' | head -c -1 | clip

works as wanted to put QR code scanner content without displaying it to the clipboard that way up to someone seeing the QR code which is quite easy due to screen video feedback, it is secret privacy friendly supporting.

```bash zbarcam --oneshot | sed 's/QR-Code://' | head -c -1 | clip ``` works as wanted to put QR code scanner content without displaying it to the clipboard that way up to someone seeing the QR code which is quite easy due to screen video feedback, it is secret privacy friendly supporting.
Author
Owner
Related to [Benjamin_Loison/Debian/issues/50](https://codeberg.org/Benjamin_Loison/Debian/issues/50).
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/linux#78
No description provided.