pyqrcode

 
 


This is about a python extension for encoding and decoding the popular matrix code created by the Japanese corporation Denso-Wave, known as Quick Response Code (qrcode).


Actually, it’s a big hack! The encoder is based on Fukuchi Kentaro's libqrencode, with a small python hack from Stefano Pedemonte's PyQrCodec.


On the other hand, the decoder is based on the best free and open source qrcode decoder written by Yusuke Yanbe, in java. It's built with Andi Vajda's JCC.

qrcode for python


Download

  1. Source Code

  2. Mac OS X (10.5 UB)

  3. Ubuntu Linux (8.10 amd64)

  4. Ubuntu Linux (8.10 i386)



More information

 


introduction


The main goal of this project is to provide a simple interface for encoding and decoding qrcode images from the python programming language. At this moment, it’s being accomplished by using an encoder witten in C and a java decoder, but that doesn’t mean we cannot write our own encoder and decoder in pure python or even c/c++ in the near future. What really matters now is that it’s working, and working fine.


The next step is to make it run on mobile devices like Nokia’s Symbian and Apple’s iPhone, since both already have support for python.


The way pyqrcode is built today isn’t compatible with any mobile device, only servers and desktops can make use of it. But don’t worry, we’re working hard to get in there.



building the source code


Basically, you’ll need JCC, a Java Development Kit (JDK) for building the source code, and a Java Runtime Environment (JRE) for using the resulting python extension. Also, you’ll need GNU make, Python Imaging Library, Python setuptools and Python development files.


Instructions for Mac OS X 10.5:

  1. Download and install JCC

  2. Download and install Python Imaging Library

  3. All the others (including java) are already built in


Instructions for Ubuntu Linux 8.10:

  1. Install JCC

  2. Install Java JDK and JRE

  3. Install Python Imaging Library

  4. Install Python setuptools

  5. Install Python development files

  6. Take a look at the “notes” below


    $ sudo apt-get install jcc sun-java6-sdk python-imaging \

                           python-setuptools python-dev



Once the requirements are satisfied, you’re ready to get it to work. The Makefile has a couple of options, but the regular way also works fine.


    $ make && sudo make install


Building binary distribution of the pyqrcode python extension is also available. In order to generate the python egg, run the following command:


    $ make egg


The resulting python egg will be built in the dist sub-directory of pyqrcode’s source tree.



installing binary distributions


Installing python eggs is pretty simple. All you need is the Python setuptools which is already built in on Mac OS X and available as optional package for Debian GNU/Linux and Ubuntu Linux operating systems.


    $ sudo apt-get install python-setuptools


The only issue with the binary distribution is that you must use the same version of Java that was used to build the python extension.


Here’s what we used to build our binary packages for python 2.5:

  1. Mac OS X 10.5.5: native apple java 1.5

  2. Debian GNU/Linux and Ubuntu Linux: sun java 1.6


In order to install the python egg, type the following:


    $ sudo easy_install qrcode-0.2.1-py2.5-platform.egg



using pyqrcode


Make sure you have a Java Runtime Environment (JRE) compatible with the pyqrcode python extension installed on your system.


For encoding, you have a couple of options:

  1. width: integer (default 400)

  2. border: integer (default 10)

  3. version: integer, from 1 to 40 (default 5)

  4. mode: NUMERIC, ALNUM, BINARY or KANJI (default ALNUM)

  5. eclevel: L, M, Q or H (default L)

  6. case_sensitive: True or False (default True)


Encoder example:


#!/usr/bin/env python

# coding: utf-8

#

# pyqrcode sample encoder


import sys, qrcode


e = qrcode.Encoder()

image = e.encode('woah!', version=15, mode=e.mode.BINARY, eclevel=e.eclevel.H)

image.save('out.png')


The resulting image is a PIL image. You can do many things with it.


For decoding, you only need to give a filename. Here’s an example:


#!/usr/bin/env python

# coding: utf-8

#

# pyqrcode sample decoder


import sys, qrcode


d = qrcode.Decoder()

if d.decode('out.png'):

    print 'result: ' + d.result

else:

    print 'error: ' + d.error



notes


Java library path seems to be missing on Ubuntu Linux 8.10. If you get errors regarding libjava.so or libjvm.so, do the following:


export LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun/jre/lib/amd64/:/usr/lib/jvm/java-6-sun/jre/lib/amd64/server/


Don’t forget to replace amd64 with your current arch.



contact


We appreciate comments, suggestions and bug reports.

In order to do so, please be kind and use our forum.