-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbarcode.py
More file actions
56 lines (44 loc) · 1.27 KB
/
barcode.py
File metadata and controls
56 lines (44 loc) · 1.27 KB
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
import io
from threading import Thread
import picamera
from PIL import Image
import zbar
class BarcodeScanner(Thread):
def __init__(self, resolutionX=800, resolutionY=600, callback=None):
self.callback = callback
self.scanner = zbar.ImageScanner()
self.scanner.parse_config("enable")
self.stream = io.BytesIO()
self.camera = picamera.PiCamera()
self.camera.resolution = (resolutionX, resolutionY)
self.quit = False
Thread.__init__(self)
def setCallback(self, callback):
self.callback = callback
def run(self):
self.quit = False
if self.camera.closed:
self.camera.open()
self.scan()
def terminate(self):
self.quit = True
if not self.camera.closed:
self.camera.close()
def scan(self):
while not self.quit and not self.camera.closed:
self.stream = io.BytesIO()
self.camera.capture(self.stream, format="jpeg")
# "Rewind" the stream to the beginning so we can read its content
self.stream.seek(0)
pil = Image.open(self.stream)
# create a reader
pil = pil.convert("L")
width, height = pil.size
raw = pil.tobytes()
# wrap image data
image = zbar.Image(width, height, "Y800", raw)
# scan the image for barcodes
self.scanner.scan(image)
if any(True for _ in image):
self.callback(image)
self.quit = True