查看文章 |
What is PyQrCodecPyQrCodec 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. DependenciesPyQrCodec requires the Python Imaging Library (PIL).
Linux version vs Windows versionThere 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 WindowsDownload the archive and unzip the content into the main Python folder. If you have Python 2.5 the folder is usually C:\Python25\ Download .DLL sources for Windows Install under LinuxDownload the archive and unzip the content into a temporary folder, for example ~/pyqrcodec/, then cd to that folder
mkdir ~/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++ (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 UsageThe 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 Decoder example>>> import PyQrcodec |

