Created
April 15, 2019 23:31
-
-
Save ja-ko/a54706bb02a8c7e8121eb52ecbe2e965 to your computer and use it in GitHub Desktop.
Strichliste.org Barcodescanner for raspberry pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------------- | |
# "THE BEER-WARE LICENSE" (Revision 42): | |
# <github@jko.dev> wrote this file. As long as you retain this notice you | |
# can do whatever you want with this stuff. If we meet some day, and you think | |
# this stuff is worth it, you can buy me a beer in return. Jannik Kolodziej | |
# ---------------------------------------------------------------------------- | |
import time | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
import os | |
import re | |
from pyzbar import pyzbar | |
import picamera | |
from io import BytesIO | |
from PIL import Image | |
def search_barcode(): | |
stream = BytesIO() | |
camera.capture(stream, format='png') | |
stream.seek(0) | |
frame = Image.open(stream) | |
barcodes = pyzbar.decode(frame) | |
for barcode in barcodes: | |
type = barcode.type | |
data = barcode.data.decode("utf8") | |
if type != "EAN13": | |
continue | |
if (len(data) != 13) or (not data.isdigit()): | |
continue | |
print("[INFO] found barcode of type {}, content is: {}".format(type, data)) | |
return data | |
return None | |
def enter_barcode(data): | |
driver.find_element_by_xpath('//body').send_keys(data) | |
driver.find_element_by_xpath('//body').send_keys(Keys.ENTER) | |
user_url_pattern = re.compile('.*#!/user/\d+') | |
os.environ["LANG"] = "en_US.UTF-8" | |
os.environ["DISPLAY"] = ":0" | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument("--kiosk") | |
chrome_options.add_argument("--start-maximized") | |
chrome_options.add_argument("--disable-infobars") | |
chrome_options.add_argument("--user-data-dir=~/.selenium") | |
chrome_options.add_argument("--app https://demo.strichliste.org") | |
chrome_options.add_argument("--google-base-url=https://demo.strichliste.org") | |
print("[INFO] starting webbrowser") | |
driver = webdriver.Chrome(chrome_options=chrome_options) | |
driver.get("https://demo.strichliste.org") | |
print("[INFO] starting video stream") | |
with picamera.PiCamera(resolution=(800, 450), framerate=30) as camera: | |
time.sleep(2.0) | |
print("[INFO] entering main loop") | |
last_valid_barcode = None | |
while True: | |
if user_url_pattern.match(driver.current_url): | |
barcode = search_barcode() | |
if barcode is not None: | |
if last_valid_barcode == barcode: | |
print("[INFO] ignoring barcode to avoid double payment") | |
continue | |
last_valid_barcode = barcode | |
time.sleep(1.0) | |
enter_barcode(barcode) | |
else: | |
last_valid_barcode = None | |
continue | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment