Created
March 4, 2016 06:45
-
-
Save john-e/f0c6ac92675d1ff1c6e9 to your computer and use it in GitHub Desktop.
Display barcode using camera - 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
############################## | |
# | |
# RPI bar code reader | |
# @author github.com/john-e | |
# | |
# @lib | |
# zbar - https://github.com/npinchot/zbar | |
############################## | |
import sys | |
import io | |
import time | |
import picamera | |
from PIL import Image | |
import zbar | |
# Create the in-memory stream | |
stream = io.BytesIO() | |
# create a reader | |
scanner = zbar.ImageScanner() | |
# configure the reader | |
scanner.parse_config('enable') | |
with picamera.PiCamera() as camera: | |
#configure camera | |
camera.video_stabilization = True | |
camera.sharpness = 50 | |
camera.contrast = 30 | |
#start preview window | |
camera.start_preview() | |
#initialize stream reader | |
stream = io.BytesIO() | |
try: | |
for foo in camera.capture_continuous(stream, format='jpeg'): | |
# Truncate the stream to the current position (in case | |
# prior iterations output a longer image) | |
stream.truncate() | |
stream.seek(0) | |
# obtain image data | |
pil = Image.open(stream).convert('L') | |
width, height = pil.size | |
raw = pil.tostring() | |
# wrap image data | |
image = zbar.Image(width, height, 'Y800', raw) | |
# scan the image for barcodes | |
scanner.scan(image) | |
# extract results | |
for symbol in image: | |
# do something useful with results | |
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data | |
# clean up | |
del(image) | |
#sleep to avoid 100% cpu usage | |
time.sleep(0.05) | |
finally: | |
camera.stop_preview() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment