Skip to content

Instantly share code, notes, and snippets.

@kumanan-c
Forked from john-e/rpi-barcode-reader.py
Created March 4, 2016 07:05
Show Gist options
  • Save kumanan-c/68a155b354a34977f2ae to your computer and use it in GitHub Desktop.
Save kumanan-c/68a155b354a34977f2ae to your computer and use it in GitHub Desktop.
Display barcode using camera - Raspberry PI
##############################
#
# 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()
@robotdomesticiteam
Copy link

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()

img = Image.open('overlay.png')
pad = Image.new('RGB', (
    ((img.size[0] + 31) // 32) * 32,
    ((img.size[1] + 15) // 16) * 16,
    ))

pad.paste(img, (0, 0))

o = camera.add_overlay(pad.tostring(), size=img.size)
o.alpha = 128
o.layer = 3 

time.sleep(3)
camera.capture(stream, format='jpeg')

"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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment