RASPBERRY PI BARCODE SCANNER

Hi guys,

In this blogpost I’ll show you how to make the raspberry pi scan bar codes. So without any delay lets begin .

Parts you will need

  • Raspberry pi (running raspbian OS)
  • camera module
  • Internet connection to the pi

Hoping you have all the parts. Let us first start by connecting the camera module

Connecting The Camera module

raspberry pi camera.JPG

It is pretty straight forward to connecting the camera module to the pi. Here are the steps.

  • Lift up the camera connectors lock

raspberry pi camera lock lifted.JPG

  • Insert the camera module ribbon cable inside

raspberry pi camera cable attached

  • Push down the lock

Enabling the camera on the pi

  • Boot up the pi
  • Open up terminal software
  • In terminal type the following command sudo raspi-config

sudo raspi-config

A settings dialog box will pop up with a set of options to modify various settings for the pi

enable camera option

  • Go to the Camera setting option and enable the camera

enable camera

  • Reboot by typing sudo reboot to make the camera work

Testing the Camera module

If you are using the camera for the first time and want to test it check out the following links

SimpleCV

SimpleCV is an image processing library. It is an open source software that runs on all the major platforms and it is quite a powerful piece of image processing software. for more info regarding SimpleCV visit http://simplecv.org/ . I shall cover more of the features of SimpleCV in the upcoming projects. But first we need to install SimpleCV on the Pi

Installing SimpleCV on Raspberry pi

  • Open up the terminal software
  • Type the following command

sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip

these were the dependencies or prerequisites for the SimpleCV library

  • Now to download SimpleCV type

sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master

  • It will take some time for the software to download
  • After it is done downloading open up python and type import SimpleCV 
  • If you get no errors then you have successfully installed SimpleCV

Getting the raspberry pi camera recognised by SimpleCV software

The raspberry pi camera is not recognised by the SimpleCv module. This was the most difficult problem that i encountered. Luckily I found help online in the form of this post by Xfirm that will install a uv4l driver for the camera. Many thanks!!. just go through that link and get the camera working in SimpleCV.

Zbar

Zbar is the module that will process the barcodes. There is another library you can use called the ZXing library. It can also process barcodes but currently I’m using the Zbar one.

Installing the Zbar library

  • Open up the lx terminal software in raspbian OS
  • First type sudo apt-get update (if not updated recently)
  • After type the following command sudo apt-get install python-zbar
  • Open up python and type import zbar, you should get no errors

The final part

  • You will need this following program
from SimpleCV import Color,Camera,Display

cam = Camera()  #starts the camera
display = Display() 

while(display.isNotDone()):
 
 img = cam.getImage() #gets image from the camera

 barcode = img.findBarcode() #finds barcode data from image
 if(barcode is not None): #if there is some data processed
   barcode = barcode[0] 
   result = str(barcode.data)
   print result #prints result of barcode in python shell
   barcode = [] #reset barcode data to empty set

 img.save(display) #shows the image on the screen
  • Run the program and wait for a few seconds for the program to run i.e. led on the camera to light up
  • Now direct the camera towards the Barcode/QR code you want to scan wait for few seconds and youll get the barcode data printed in the python shell

2016-02-01-105607_1360x768_scrot.png

 

The uv4l driver has a resolution of 640X480. therefore the barcodes need to be of a higher resolution to get them scanned properly. If any of you guys have figured out how to incease resolution of the uv4l driver please let me know in the comments down below.

Also like and share this post so that it reaches out to more people. See you guys next time.

32 thoughts on “RASPBERRY PI BARCODE SCANNER

  1. Hey man I really appreciate your tutorial. I will be building upon it for my project. But my only concern is if it will run on a rasberry pi zero w with it’s camera

    Like

  2. Hi, has anybody a solution for the zxing library. I want to decode aztec code. Any help would be very appreciated.

    Like

  3. Hi, has anybody a solution for the zxing library. I want to decode aztec code. Any help would be very appreciated

    Like

  4. hi,

    Thanks for the post. The program looks elegant. I am able to use my camera in SimpleCV via different program but when I use the program listed here, I get the error on

    while(display.isNotDone()):

    isNOTDone is not a valid attribute of Display is the syntax error that I get.

    can you pls let me know how to correct this error?

    Thanks,

    Mike

    Like

  5. I can see with the camera, but it isn’t scanning the barcode or QR code. I opened python shell , import SimpleCV , import zbar, no errors. I then open bash and run the program, barcode.py.

    Like

  6. All working well, except when I try to scan a Book Barcode it only returns the ISBN10 instead of the ISBN13 version.
    I’ve tried the windows installer of ZBAR and it correctly decodes to ISBN13.
    Odd.
    It’s a shame there isn’t more to go on in the python wrapper. 😦

    Like

  7. I am trying to implement a solution using the new RPi camera to scan regular barcodes. I tried your code but while it can successfully scan QR codes, it cant seem to pick up these regular barcodes.

    Can you help me at all here? Thanks

    Like

    1. Barcodes do get scanned. Same method. But i found qr codes getting scanned much easily. I think the problem can be solved by using a better resolution camera

      Like

  8. When I run the coding its give an error saying that “No module named SimpleCV”. I’ve downloaded the SimpleCV using the links given, can you explain why I get such error? Tq 🙂

    Like

    1. Try installing simplecv again if that helps. Also check if you have the prerequisites installed. Try ‘import numpy’ etc.. In python. I would like to know Which os are you using?

      Like

      1. I’ve try to install the SimpleCV and the sgvwrite and its gives no error when I’m trying the import SimpleCV command. And then when I’m trying to run the code, it gives an error said

        ERROR:
        Traceback (most recent call last):
        File “QR.py”, line 8, in
        img = cam.getImage() #gets image from the camera
        File “/usr/local/lib/python2.7/dist-packages/SimpleCV/Camera.py”, line 618, in getImage
        cv.GrabFrame(self.capture)
        TypeError: Expected CvCapture for argument ‘capture’

        what does it mean? And I’m using the Noobs wheezy.

        Like

      2. So the simplecv library is installed. But I have no idea why you’re getting that error. Did you try some simple programs like getting camera live feed etc using simplecv. I’m also using the noobs wheezy ver on the pi.

        Like

      3. I also have the same problem. When i try to run the code and when i try to import SimpleCV in python shell. Even when i try to import zbar it shows “No module named zbar.” I know for sure the libraries are installed because i can see the physical folder in the explorer and because i tied “import numpy” and “import opencv” and it doesn’t return any errors. Did you find a solution for that?

        Like

      1. Hey, when I run the given program after doing all the necessary steps, I get the error message that says so :
        lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system /run/user/109/gvfs
        Output information may be incomplete.
        Fatal Python error: (pygame parachute) Segmentation Fault
        Aborted
        Any idea how it can be fixed? Thank you in advance!

        Like

Leave a comment