libqrencode.qrencode – QR Encoding

Python ctypes binding to libqrencode.

This module when ran as a script attempts to mimic the c sample program qrenc.c otherwise known as qrencode

This file is part of libqrencode python ctypes bindings.

Copyright (C) 2012 Matthew Baker <mu.beta.06@gmail.com>

This is free software: you can redistribute it and/or modify it under the terms of the LGNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the LGNU Lesser General Public License along with this software. If not, see <http://www.gnu.org/licenses/>.

Installation

  1. Install libqrencode.

  2. Unpack archive onto local machine.

  3. Run

    $ python setup.py install
  4. Test

    $ python -m libqrencode.qrencode 'Hello World'
    XXXXXXXXXXXXXX    XX  XXXX  XXXXXXXXXXXXXX
    XX          XX    XXXXXX    XX          XX
    XX  XXXXXX  XX  XXXX  XXXX  XX  XXXXXX  XX
    XX  XXXXXX  XX    XX  XX    XX  XXXXXX  XX
    XX  XXXXXX  XX      XX  XX  XX  XXXXXX  XX
    XX          XX          XX  XX          XX
    XXXXXXXXXXXXXX  XX  XX  XX  XXXXXXXXXXXXXX
                    XXXX  XXXX
    XXXXXX  XXXXXXXXXXXXXXXX  XXXX      XX
      XXXX  XXXX    XXXXXX      XXXXXX    XXXX
    XX  XX  XX  XX  XX  XX  XXXX  XXXXXXXXXXXX
        XX          XXXXXX          XX    XX
    XXXX    XX  XXXX  XX    XXXX  XXXX
                    XX  XXXX  XX        XXXX
    XXXXXXXXXXXXXX  XX  XXXX      XXXX  XXXXXX
    XX          XX  XXXX  XXXX    XX        XX
    XX  XXXXXX  XX  XX    XX        XX
    XX  XXXXXX  XX    XXXXXX    XXXXXX  XXXX
    XX  XXXXXX  XX  XX  XX  XX  XX  XX  XX  XX
    XX          XX  XX    XX        XX    XX
    XXXXXXXXXXXXXX  XX  XXXXXX    XX      XXXX

Usage

Scripted Usage

  1. Run the following for usage

    $ python -m libqrencode.qrencode -h
    Usage: qrencode [OPTION]... [STRING]
    
    libqrencode ctypes python binding version 1.0, libqrencode version 3.2.1
    
    Options:
      -h, --help            show this help message and exit
      -o FILENAME, --filename=FILENAME
                            Encoded image filename (.png, .pdf, .jpg, .tif...)
      -s SIZE, --size=SIZE  Specify module size in dots (pixels). (default=3)
      -l LEVEL, --level=LEVEL
                            specify error correction level from L (lowest) to H
                            (highest). (default=L)
      -v SYMVERSION, --symversion=SYMVERSION
                            specify the version of the symbol. (default=auto)
      -m MARGIN, --margin=MARGIN
                            specify the width of the margins. (default=4)
                            (2 for Micro)))
      -d DPI, --dpi=DPI     specify the DPI of the generated PNG. (default=72)
      -S, --structured      make structured symbols. Version must be
                            specified.
      -k, --kanji           assume that the input text contains kanji
                            (shift-jis).
      -c, --casesensitive   encode lower-case alphabet characters in 8-bit mode.
                            (default)
      -8, --eightbit        encode entire data in 8-bit mode. -k and -c will be
                            ignored.
      -M, --micro           encode in a Micro QR Code. (experimental).

Module Usage

  1. Import libqrencode bindings:

    >>> from libqrencode import qrencode
    
  2. Create a qrencode.QREncoder instantiation:

    >>> encoder = qrencode.QREncoder()
    
  3. Encode a string using the aforementioned instantiation:

    >>> encoder.encode('Hello World')
    
  4. Select encoded output format from one of the following output formats:

    1. ASCII:

      >>> encoder.asciipreview()
      XXXXXXXXXXXXXX    XX  XXXX  XXXXXXXXXXXXXX
      XX          XX    XXXXXX    XX          XX
      XX  XXXXXX  XX  XXXX  XXXX  XX  XXXXXX  XX
      XX  XXXXXX  XX    XX  XX    XX  XXXXXX  XX
      XX  XXXXXX  XX      XX  XX  XX  XXXXXX  XX
      XX          XX          XX  XX          XX
      XXXXXXXXXXXXXX  XX  XX  XX  XXXXXXXXXXXXXX
                      XXXX  XXXX
      XXXXXX  XXXXXXXXXXXXXXXX  XXXX      XX
        XXXX  XXXX    XXXXXX      XXXXXX    XXXX
      XX  XX  XX  XX  XX  XX  XXXX  XXXXXXXXXXXX
          XX          XXXXXX          XX    XX
      XXXX    XX  XXXX  XX    XXXX  XXXX
                      XX  XXXX  XX        XXXX
      XXXXXXXXXXXXXX  XX  XXXX      XXXX  XXXXXX
      XX          XX  XXXX  XXXX    XX        XX
      XX  XXXXXX  XX  XX    XX        XX
      XX  XXXXXX  XX    XXXXXX    XXXXXX  XXXX
      XX  XXXXXX  XX  XX  XX  XX  XX  XX  XX  XX
      XX          XX  XX    XX        XX    XX
      XXXXXXXXXXXXXX  XX  XXXXXX    XX      XXXX
      
    2. Boolean list of lists:

      >>> qr_list = encoder.as2dlist()
      
    3. Postscript string:

      >>> qr_ps = encoder.asps()
      

See the individual classes, methods, attributes and functions below for further details.

Classes

class libqrencode.qrencode.QREncode(p)[source]

Base class for representing QREncode c structures. Allows for struct member read access as class attributes.

class libqrencode.qrencode.QRcode(p, cleanup=True)[source]

Python representation of QRcode c structure

class libqrencode.qrencode.QRcode_List(p)[source]

Python representation of QRcode_List c structure.

class libqrencode.qrencode.QREncoder[source]

Base class for representing QREncoder.

Largely used to maintain the state that the globals in qrenc.c represent.

as2dlist()[source]

Return a list of lists describing the encoded data.

asciipreview()[source]

Print an ascii representation of the Encoder output to stdout

asps()[source]

Return a PostScript representation of the encoded QRCode.

Paints the “black” symbol in the current colour on the assumption that the background has already been painted and appropriate colour. Symbols are assumed to be 1 user coordinate square.

casesensitive

Return the QREncoder’s casesensitive property.

code

Return the QREncode object

eightbit

Return the QREncoder’s eightbit property.

encode(data)[source]

Encode input string represented by data

hint

Return the QREncoder’s hint.

level

Return the QREncoder’s level.

micro

Return the QREncoder’s micro property.

version

Return the QREncoder’s symbol version property.

class libqrencode.qrencode.StructuredQREncoder[source]

Subclassing of QREncoder for structured QR encoding.

as2dlists()[source]

Return a list of list of lists describing the encoded data.

asciipreview()[source]

Print an ascii representation of the Encoder output to stdout

asps()[source]

Return a list of PostScript strings representing the encoded QRCode’s of the StructuredQREncoder.

code_list

Return StructuredQREncoder’s code_list.

encode(data)[source]

Encode input string represented by data

Functions

libqrencode.qrencode.call(func, *args)[source]

Convenience routine to call function func, check for NULL pointer and subsequently check errno to report useful error message.

libqrencode.qrencode.main(argv)[source]

Exceptions

exception libqrencode.qrencode.Error[source]

This is a user-defined exception for errors raised by this module.

Pre-requisites

Python (http://www.python.org)

libqrencode (http://fukuchi.org/works/qrencode/index.html.en)

Python Imaging Library (PIL, http://www.pythonware.com/products/pil/) (optional)

Known Issues

  1. For the moment the bindings have only be constructed for use under linux. I could be tempted to support windows if required.