Drone_Nerd
Posts: 2
Joined: Tue Dec 12, 2017 2:36 pm

QR code scanner problem

Fri Dec 07, 2018 3:50 pm

Hey guys,
I am working on a project where I use a raspberry pi zero w to detect and decode a QR code. I have used the library ZBar for this task. I have also used the picamera to scan the QR code. I don’t have any experience on the raspberry pi. This is the code downloaded from the internet:

Code: Select all

import io
import time
import picamera
from PIL import Image
import zbar


# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(2)
    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.tobytes()

# wrap image data
image = zbar.Image(width, height, 'Y800', raw)

# scan the image for barcodes
scanner.scan(image)

# extract results
for symbol in image:
    print ('decoded', symbol.type, 'symbol', '"%s"' % symbol.data)
    y=str(symbol.data)
# clean up
del(image)
print (y)
import io
import time
import picamera
from PIL import Image
import zbar


# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
    camera.start_preview()
    time.sleep(2)
    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.tobytes()

# wrap image data
image = zbar.Image(width, height, 'Y800', raw)

# scan the image for barcodes
scanner.scan(image)

# extract results
for symbol in image:
    print ('decoded', symbol.type, 'symbol', '"%s"' % symbol.data)
    y=str(symbol.data)
# clean up
del(image)
print (y)

Now what I want to do is when the camera sees the QR code it immediately decodes it and sends the result to a computer wirelessly. Can someone tell me what I have to change in the code to decode the image exactly when the camera sees it. On the code above, even if the camera sees the image as soon as the project is run, it waits for 2 seconds before printing the result.
Any suggestions will be appreciated :)
Thank you

Andyroo

Re: QR code scanner problem

Fri Dec 07, 2018 5:51 pm

It’s best to use the code tags to show code on the site as indentation is vital for Python.

The simplest way to save the two seconds is the change the line:

Code: Select all

time.sleep(2)
To read

Code: Select all

# time.sleep(2)
This leaves the program as was but the delay as a comment. Not sure if this is needed to stabilise the camera or internal software to start.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12322
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: QR code scanner problem

Fri Dec 07, 2018 6:31 pm

added code tags, moved to python section.

Return to “Python”