-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode.py
executable file
·144 lines (134 loc) · 4.83 KB
/
barcode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# barcode.py
#
# Copyright 2021 Thomas Castleman <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""Barcode Reader for PyLibMan"""
import cv2
import json
import time
import pyzbar.pyzbar as zbar
import numpy as np
import qrcode
from PIL import Image as img
import os
import subprocess
import common
def get_frame(webcam):
"""Get a frame from the Webcam"""
return cv2.cvtColor(np.array(webcam.read()[1]), cv2.COLOR_BGR2GRAY)
def get_barcode(frame):
"""Get barcodes in frame."""
return zbar.decode(frame)
def generate_barcode(type, uid):
"""Generate a QR Code"""
# Generate QR Code
path = "/tmp/qr_codes"
data = {"type": type, "uid": uid}
qr = qrcode.QRCode()
qr.add_data(json.dumps(data))
qr.make(fit=True)
image = qr.make_image(fill_color="black", back_color="white")
if not os.path.isdir(path):
print("MAKING DIR")
os.mkdir(path)
image.save(f"{path}/{type}_{uid}.png")
print(f"Saved file to {path}/{type}_{uid}.png")
return f"{path}/{type}_{uid}.png"
def print_barcode(paths):
"""Print QR codes"""
images = []
recursion_point = None
for each in paths:
images.append(img.open(each))
canvas = img.new("RGB", (2550, 3300), color=(255, 255, 255))
# we can user canvas.paste() to paste images into the canvas, then just save the canvas as a file
attach_point = [0, 0]
canvas.paste(images[0], tuple(attach_point))
attach_point = [5, 0]
for each in images[1:]:
size = each.size
attach_point[0] = size[0] + attach_point[0] + 5
if ((size[0] + attach_point[0] + 5) >= 2550): # if the image goes over the right edge, wrap
attach_point[0] = 0
attach_point[1] = attach_point[1] + size[1] + 5
print(attach_point)
if attach_point[1] >= 3300: # if the image goes over the bottom edge, stop. Mark recursion point
recursion_point = image.index(each)
break
canvas.paste(each, tuple(attach_point))
attach_point[0] = attach_point[0] + 5
output = f"{int(time.time())}.png"
canvas.save(output)
if recursion_point == None:
return output
add = print_barcode(paths[recursion_point:])
if isinstance(add, list):
return [output] + add
return [output, add]
# this is just a basic barcode scanner that reads in JSON data
# it has little sanitization, and works in black and white in order to cut
# down on memory usage.
def barcode_scanner(pipe, webcam):
"""Barcode scanner process"""
common.set_procname("PLM-barcode")
job = False
while True:
while True:
loop = False
try:
data = get_frame(webcam)
except cv2.error:
print("Camera disabled. Sleeping...")
time.sleep(1)
loop = True
if pipe.poll():
break
if loop:
continue
detected_barcodes = get_barcode(data)
if detected_barcodes != []:
break
if pipe.poll():
job = pipe.recv()
if job == "get_barcode":
for barcode in detected_barcodes:
data = barcode.data.decode()
try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
continue
# basic sanitation
if isinstance(data, dict):
keys = data.keys()
if (("type" in keys) and ("uid" in keys)):
output = {"type": data["type"], "uid": data["uid"]}
elif isinstance(job, dict):
print("Abnormal job!")
if job["cmd_type"] == "make_qr":
# generate QR Code
print("Making QR!")
output = generate_barcode(job["type"], job["uid"])
elif job["cmd_type"] == "print_qr":
print("Generating images for printing!")
output = print_barcode(job["paths"])
pipe.send(output)
job = None