Reading, writing and speaking barcodes.

Barcodes encode data in terms of on and off (light and dark). These can be used to represent binary numbers. These binary numbers can themselves represent letters.

1-D barcodes are often used to encode numbers, for example in supermarket or on the back of books.

Android can use the phone camera to read barcodes and decode them.

2-D barcodes or QR codes are a type of barcode that can be used to store larger amounts of information (up to about 4,000 bytes).

A barcode reader

Create a new script on the PC called cyber-barcode.py. Type the following:

import android
droid = android.Android()

# start the scanner, uses an external program for this
code = droid.scanBarcode()

# get the result and print it on the terminal window
print code.result['extras']['SCAN_RESULT']

There are some new commands here.

Line 3 tells Python that you want to speak scan a barcode.

Line 4 extracts the result and prints it on the terminal.

Finding a Book

Now lets combine this with the browser lookup we used in the last exercise. In this case, Google Book lookup. Modify your program to look like the following:

import android
droid = android.Android()
code = droid.scanBarcode()
isbn = int(code.result['extras']['SCAN_RESULT'])
url = 'http://books.google.com?q=%d' % isbn
droid.startActivity('android.intent.action.VIEW', url)

What do you think this does?

How do you think you could combine it with speech to help someone who is sight impaired?

Extracting the Name of the Book

This is a more sophisticated version of the same idea (based on this code.. It uses some code to find the title (the title is tagged with dc:title). If you aren't sure what I mean go to http://books.google.com/books/feeds/volumes?q=0470093552 in Firefox and view the source. The title will be surrounded by special codes indicating that this is the title. For example:

<dc:title>Concurrency</dc:title>

Indicates that the title of the book if Concurrency.

# Scans book barcodes and looks them up by ISBN in Google Books.
    
import android
import urllib
import xml.dom.minidom
    
droid = android.Android()
    
def nodeValue(doc, tagname):  # get the text inside a node
    return str(doc.getElementsByTagName(tagname)[0].firstChild.nodeValue)
    
code = droid.scanBarcode()
isbn = code.result['extras']['SCAN_RESULT']
url = "http://books.google.com/books/feeds/volumes?q=" + isbn
docval = urllib.urlopen(url).read()
doc = xml.dom.minidom.parseString(docval)
found = int(nodeValue(doc, "openSearch:totalResults"))
if found:
    title = nodeValue(doc, "dc:title")
    print title
else:
    print "Sorry, book not found"

Helping the Sight Impaired

Try changing the program to speak the name of the book or book not found.

Creating Your Own Barcodes

Now you should create some barcodes with which to try out your speaking barcode reader. We want to use 2-D barcodes because we can actually embed text into the barcode.

We are going to use the QR Code Generator.

  1. Open the Contents drop down and choose Text.
  2. Write what you want to be said in Text Content.
  3. Open the Size drop down and choose L.
  4. Click Generate.
  5. Embed or share the resulting barcode image with your friends. Point your barcode reader at the screen.

Speaking Barcodes

Think about creating an application for a sight impaired person. You might want to generate barcodes that you stick to things like food cans so they know what is in them by pointing their phone at the barcode and reading the result out to them. Create a program a bit like the book reader that does this. Make it go in a loop. Think about making the START and STOP commands voice activated, how would you do this? Think about combining the book reader with the barcode, allow the person to choose with mode (using their voice).

This topic: Users/Ian > WebHome > ActivityBarcode
Topic revision: 14 Dec 2012, ian
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 24 Jan 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated