查看文章
 
Documentation:PyQrCodec(PyQrCodec文档)
2008-10-09 17:20

What is PyQrCodec

PyQrCodec is a Python module for encoding and decoding QrCode images. PyQrCodec wraps encoding library libqrencode developed by Fukuchi Kentaro and decoding library libdecodeqr developed by Nishi Takao, the former being a C library and the latter a C++ library. The implementation of the libraries in native code makes both the encoding and decoding very quick.


Dependencies

PyQrCodec requires the Python Imaging Library (PIL).


Linux version vs Windows version

There is a substantial difference between the two versions for Linux and for Windows, in fact the Linux version compiles as a Python extension module while in the Windows version the codec comes as a precompiled dynamic link library which is then interfaced through Ctypes.

The reason why I created a different version for Windows which uses Ctypes is that Python extension modules must be compiled with the same compiler which was used for compiling your Python environment; under Linux it's gcc and everything works fine, but Python 2.5 for Windows was compiled with Visual Studio 2005 which I don't have and is not distributed free of charge, so I decided to compile a DLL which incapsulates encode and decode functions and interface it from Python through Ctypes, this way the compile process is decoupled and you can use any compiler to build the DLL.

If you have Visual Studio 2005 you can actually download the version for Linux and compile it under Windows without any modification by typing

python setup.py build install.

Install under Windows

Download the archive and unzip the content into the main Python folder. If you have Python 2.5 the folder is usually C:\Python25\

Download for Windows

Download .DLL sources for Windows


Install under Linux

Download the archive and unzip the content into a temporary folder, for example ~/pyqrcodec/, then cd to that folder

Download for Linux

mkdir ~/pyqrcodec
tar xvzf PyQrcodec.tar.gz ~/pyqrcodec/
cd ~/pyqrcodec

Make sure you have installed g++, Python development files and the OpenCv library and development files. You can download OpenCv from SourceForge and install by hand or use Debian or Ubuntu repositories:

sudo aptitude install g++
sudo aptitude install python-dev
sudo aptitude install libcv1 libcvaux1 libcv-dev libcvaux-dev

(if you install from repository everything should work out of the box, if you install by hand you have to compile OpenCv and modify /usr/include/opencv/ in setup.py to your OpenCv lib directory).

Now build the module:

python setup.py build

And install:

python setup.py install

Usage

The module includes one class named QrCodec which has just two methods: encode() and decode().

encode() takes a string as only required parameter. It also takes some additional optional parameters to specify the Qr encoding type, Reed-Solomon error correction level, force Qr version. Edit QrCode.py, some comments explain the meaning of the parameters. encode() returns a tuple where the first value is an integer which tells the number of squares the resulting code is made of and the second value is a PIL image containing the QrCode.

decode() takes a filename as only parameter, file type can be .jpg, .jpeg, .bmp, .png. decode() returns a tuple where the first value is a boolean informing about successful/unsuccessful decode and the second value is a string representing the string encoded in the Qr code in case of successful decode and a comment about the reason of failure in case of unsuccessful decode.


Encoder example

>>> import PyQrcodec
>>> codec = PyQrcodec.QrCodec()
>>> size, image = codec.encode('www.example.com')
>>> image.save('C:\\example.jpg')

Decoder example

>>> import PyQrcodec
>>> codec = PyQrcodec.QrCodec()
>>> status, string = codec.decode('C:\\example.jpg')
>>> print string
www.example.com

类别:Python||添加到搜藏 |分享到i贴吧|浏览(2567)|评论 (0)
 
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu