-
-
Save kumanan-c/68a155b354a34977f2ae 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
Hello
I used a similar code
but it executes the recognition only of qrcode
It does the recognition of the barcode.
You know why?
import io
import time
import picamera
from PIL import Image
import zbar
import RPi.GPIO as GPIO
import time
import subprocess
def blueLed():
command = 'sudo python /home/pi/rpi_ws281x/python/blueLed.py'
subprocess.Popen(command , shell=True,stdout=subprocess.PIPE)
def redLed():
command = 'sudo python /home/pi/rpi_ws281x/python/redLed.py'
subprocess.Popen(command , shell=True,stdout=subprocess.PIPE)
def greenLed():
command = 'sudo python /home/pi/rpi_ws281x/python/greenLed.py'
subprocess.Popen(command , shell=True,stdout=subprocess.PIPE)
def noneLed():
command = 'sudo python /home/pi/rpi_ws281x/python/noneLed.py'
subprocess.Popen(command , shell=True,stdout=subprocess.PIPE)
greenLed()
GPIO.setmode(GPIO.BCM)
buzzer
GPIO.setup(23,GPIO.OUT)
led
GPIO.setup(25,GPIO.OUT)
GPIO.output(25,True)
Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.video_stabilization = True
camera.resolution = (1280, 720)
camera.framerate = 30
camera.saturation = -100
camera.sharpness = -100
camera.contrast = 100
camera.start_preview()
"Rewind" the stream to the beginning so we can read its content
stream.seek(0)
pil = Image.open(stream)
create a reader
scanner = zbar.ImageScanner()
configure the reader
scanner.parse_config('enable')
pil = pil.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)
GPIO.output(23,True)
time.sleep(0.25)
GPIO.output(23,False)
GPIO.output(25,False)
dato = "";
type = "";
extract results
for symbol in image:
#print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
dato = symbol.data
type = symbol.type
print 'decoded', type, 'symbol', '"%s"' % dato
del(image)
if dato=="":
redLed()
else:
greenLed()
dato = ""
time.sleep(1)
noneLed()