About‎ > ‎Announcements‎ > ‎

Linux - Create QR code images

posted 23 Feb 2011 11:59 by James Holgate   [ updated 25 Feb 2012 23:33 ]
QR Code uses square patterns to encode characters in an image. Readers and writers are available for many mobile phone platforms. For casual use, or to investigate different uses, you can preview and download QR Code images with an on-line QR Code creator like Kaywa, Mobile-BarcodesQuickMark or Zxing. On-line QR Code generators may have restrictions on how it can be used. For example, the generator may be restricted to non-commercial use. Linux qrencode has no such restrictions. It is quick and reliable. You can easily import the PNG images it produces into documents or post them on the web.

To decode images, you can use an open source toolkit called zbar. Zbar can decode QR code images (zbarimg) and scan QR codes with a webcam (zbarcam). You can use QR code images to facilitate inventory, track shipments, record web addresses or make electronic business cards (vcards). If you are using a Linux or Java powered mobile phone, look in your app repository for a reader that uses zxing. If you are using an iPod or iPhone, Barcodes Scanner by Romain Pechayre and Semacode are free applications that can read QR codes. Blackberry devices have a built in QR code reader. Windows Mobile users should look for i-nigma or ScanLife. Android users should look for i-nigma or Quickmark QR code reader.

QR code of extension site
QR Code for http://aoo-extensions.sourceforge.net/project/Read_Text

Tip: For images to be used by consumer cameras, webcams or phones, keep the length of the selection as small as possible. Use less than 800 characters.

With Linux, you can easily create QR code image files to embed strings of text in an image using a tool called qrencode. Install qrencode using a package manager or build it from source.

To use qrencode with Read Text extension, save the following python code as create_qr_label.py

Using Read Text's main dialog, you can choose /usr/bin/python as the Command Line Program, and "path-to/create_qr_label.py" --output "(HOME)(NOW).png" "(TMP)" as the Command Line options. 

The python script will create a PNG image file in your home directory.


#!/usr/bin/env python
# -*- coding: UTF-8-*-
'''
To encode the text in a QR Code image file (png).

QR Code uses square patterns to encode characters in an image.
Readers are available in many mobile phone platforms. For Linux, 
use zbar. Zbar is an open source toolkit that can decode QR code
images (zbarimg) and scan QR codes with a webcam (zbarcam).
You can use QR code images to facilitate inventory, track shipments,
record web addresses or make electronic business cards (vcards).

Tip: For most consumer cameras or phones, keep the length
of the selection as small as possible. Use less than 800 characters.

Install qrencode using a package manager or build it from source.
  
Read Text Extension for OpenOffice.org (for Linux) :
  See: http://sites.google.com/site/readtextextension/
  Tools > Add-Ons > Read Selection... Dialog setup:
  External program: /usr/bin/python
  Command line: "(CREATE_QR_LABEL_PY)" --output "(HOME)(NOW).png" "(TMP)"
  Option (huge): "(CREATE_QR_LABEL_PY)" --size 12 --output "(HOME)(NOW).png" "(TMP)"
  Option (tiny): "(CREATE_QR_LABEL_PY)" --size 1 --output "(HOME)(NOW).png" "(TMP)"
Copyright (c) 2011 James Holgate
'''
import getopt, sys, codecs, os

def usage():
  print ("")
  print ("Usage")
  print ('  '+ sys.argv[0] + ' [--size 3] --output "output.png" "input.txt"')
  print ("")
# enddef ############################

def qrencode(sTXT,sPNG,sSIZE):
  if sSIZE == "":
    sSIZE = "3"
  #endif
  #Use cat to send literal text to qrencode...
  try: 
    s1 = 'cat "' + sTXT + '" | qrencode -s ' + sSIZE + ' -o "' + sPNG + '" '
    print s1
    os.system(s1)
  except IOError:
    print 'I was unable to use cat with qrencode!'
    usage()
    sys.exit(2)
  #endtry
# enddef ############################

def main():
  sSIZE=""
  try:
    opts, args = getopt.getopt(sys.argv[1:], "hos", ["help", "output=", "size="])
  except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
  #endtry
  for o, a in opts:
    if o in ("-h", "--help"):
      usage()
      sys.exit(0)
    elif o in ("-o", "--output"):
      sPNG=a
    elif o in ("-s", "--size"):
      sSIZE=a
    else:
      assert False, "unhandled option"
    #endif
  #endfor
  sTXT=sys.argv[-1]
  qrencode(sTXT,sPNG,sSIZE)
  sys.exit(0)
# enddef ############################

if __name__ == "__main__":
  main()
#endif

“QR Code” is registered trademark of DENSO WAVE INCORPORATED in the following countries: Japan, United States of America, Australia and Europe.


Č
ċ
ď
create_qr_label.py
(2k)
James Holgate,
9 Mar 2011 07:53
Comments