Android barcode scanner in 6 lines of Python code

June 9, 2009

in Gadgets/Hack

After my last video about using a barcode scanner to add and search books in your library, I was feeling pretty happy. Bar code scanners are pretty cheap–mine cost about $65. But then Google released the Android Scripting Environment (ASE) and it turns out that you don’t even need a bar code scanner. Instead, you can use an Android phone such as the G1.

Just as a proof-of-concept, here’s a barcode scanner written in six lines of Python code:

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

Thanks to fellow Googler Vijayakrishna Griddaluru for sending me this sample code. Visiting the resulting url offers the option to add that book to your library:

Android bar code scanner

Pretty easy, huh? You can read all about the new scripting environment. Not only can you scan bar codes, you can use text-to-speech, make phone calls, send text messages, read sensor data, and find your location–all from easy scripts. One person wrote a script to go into silent mode when the phone is placed screen-down on the table. It took less than 20 lines of code, and that’s including comments!

The Android Scripting Environment should make fun projects even easier. Brad Fitzpatrick wrote about using his Android phone to open his garage door automatically when his motorcycle gets close to home. Now those sorts of projects are even easier to write. :)

{ 27 comments… read them below or add one }

Henry Elliss June 9, 2009 at 2:58 pm

Hmmm, perhaps I should add an iPhone to my shopping list instead of that barcode scanner you recommended, eh Matt? Can I tell my wife you’re in favour of this purchase?! :D

Rudy McCormick June 9, 2009 at 4:07 pm

That is very cool, I can’t wait to try this!!

Dave (original) June 9, 2009 at 7:16 pm

Not only can you scan bar codes, you can use text-to-speech, make phone calls, send text messages, read sensor data, and find your location–all from easy scripts.

“make phone calls” are you mad ;)

Peter (IMC) June 9, 2009 at 9:51 pm

text-to-speech is cool, but I much prefer speech-to-text on cellphones.

Any S60 apps you know of Matt? I mean, Symbian is the Linux of the mobile world. You must be interested in that OS as well.

Bibokz June 10, 2009 at 12:30 am

Android phone becoming more useful compare to iPhone, the applications are more industrialized. I salute the Android developer for this.

Gerry June 10, 2009 at 1:00 am

hate to say admit it – but seriously tempted by an Android, after the #o2fail pricing of the new iPhone, its great but I am a poor broke SEO bloke with a family …

Excellent that I can get my hands ‘mucky’ enough without breaking it …

Chip June 10, 2009 at 8:30 am

6 lines of Python backed by a STRONG built in API!

Without the heavy lifting of having :
droid.scanBarcode()
built in, it definitely would be a much different proof of concept.

I’m with Gerry on the iPhone, absolutely love it, but 1) AT&T (on this side of the pond) really shot themselves in the foot with their $499/$699 pricing and 2) Trying to code for the iPhone is like trying to learn Chinese from a cow.

Rich June 10, 2009 at 9:18 am

Gotta say that I’m looking forward to this: It’s going to be really useful to write quick scripts, rather than having to take on the Android SDK, since testing even very basic applications is ridiculously laborious when you’re only running a Linux netbook.

That said, I only bought a G1 a week or so ago, and aside from the usual battery issues (for which I’ve already ordered a larger battery), I’m noticing that I’m getting multiple issues with freezing and rebooting, sometimes just after loading the OS; I’m fairly sure it’s an app or widget I’m using causing it, since booting into Safe Mode seems to alleviate any problems, but since there seems to be no obvious form of logging or system monitoring apps available through the OS, it’s impossible to tell which might be the cause without individually removing each app, or making a clean start, then reinstalling each app one by one and testing each time.

I don’t suppose you, or any of your readers have any tips for tracking down the cause of my woes? It’d be nice to have a stable phone before I start working on scripts :)

Andrea June 10, 2009 at 9:43 am

When I saw your post about the barcode scanner I thought to myself, there has got to be a way to get my G1 to do this. It has all sorts of other barcode scanning applications.

To those above excited about the iphone pricing, I got my G1 from T-mobile for $99 with a 2 year commitment. I think you may have to be a new customer, but we easily talked the guy at the mall into coming down $150. You just have to be willing to haggle. And if the T-mobile store you go to tells you no, try an independent reseller at a kiosk.

I can’t say enough good things about my G1, though. I’ve played with the iphone, and you couldn’t get me to switch. :)

Morris Rosenthal June 10, 2009 at 10:24 am

Matt,

So when are you going to get to the post where Android writes the books for me and I can retire? That’s what I call displacement technology:-)

Morris

Aaron Shear June 10, 2009 at 1:16 pm

I like the bar code scanner app for the iphone, I wind up using it at best buy.

Dan Thies June 10, 2009 at 2:37 pm

Damn. Cool.

Not as cool as the garage door opening automagically, but… Damn.

Joel Gross June 10, 2009 at 6:39 pm

Any chance that tech will come to the iPhone soon?

IIFA Awards 2009 June 11, 2009 at 12:50 am

seems its time to buy G1 now :) ) and start using bar code applications

Yvo June 11, 2009 at 1:39 am

This only emphasizes my point from a blog article I wrote: In the future everybody develops…

Ralph Corderoy June 11, 2009 at 4:31 am

Does this work with those ISBN barcodes that end in an `X’ rather than a digit? Only I note you’re using `%d’ as a format specifier for the Google URL.

dazza June 11, 2009 at 5:21 am

Some guys have used the Barcode reader built into android (I believe it’s the zxing library) to scan barcodes in to Beep My Stuff (http://www.beepmystuff.com, disclaimer, I coded and run BMS). I don’t think it’s in the app store yet but the code is open source http://code.google.com/p/beepmystuff/

burger June 11, 2009 at 5:43 am

“Symbian is the Linux of the mobile world”

Symbian is not Linux.

Android is closest to Linux.
iPhone has a *nix heart (BSD).

Symbian is it’s own thing. And the hardest of the 3 to program for. But it’s been around a very long time.

Brian June 11, 2009 at 5:50 am

I wouldn’t try to coerce it to an integer — you’re using it as an unmodified string value.


isbn = int(code['result']['SCAN_RESULT'])
url = “http://books.google.com?q=%d” % isbn

If you had an exception handler, I suppose it might make some sense. Anyways, knock that “6 lines” down to 5. :)

Goner Leone June 11, 2009 at 5:22 pm

Yea, go buy an G1 so you can buy more things “more easy”. I would like to see Intel’s OpenCV (Computer Vision) API implemented on the Android. So you could put your face on a Muscle Man or a Seal or something… Sony Camcorder style.

alex borsody June 11, 2009 at 9:36 pm

that code doesn’t tell me much those are all encapsulated functions. Since I renewed my site with Google apps, it has been giving me hell. not showing up in SERP’s then reappearing like nothing. then saying that the site has expired, then going back to normal. now its sending me to a godaddy parked free page.

the site is a solid music blog musicandartsblog.com

Charles June 12, 2009 at 4:17 pm

My friend has one and he loves it. Runs all kinds of crazy stuff on it. I’d love to get one as well. Alex, thats pretty weird. Having the same issue for my main term, there one day gone the next. Perhaps Matt could enlighten us?

Lorne Pike June 13, 2009 at 8:42 am

I’ve been in technology and business for about 20 years now, and I love how new technologies continually appear. There is always something getting faster and better yet cheaper, and there is always someone finding a new way to apply it. Gotta love that!

spamhound June 15, 2009 at 9:24 am

Totally off subject, but I read an article that Google is looking at Twitter and may even display twitter returns in the serps.

If that’s true, all I’ve got to say is “are you kidding me”?

Many people, myself included would not like to search through useless 140 posts by narcissistic people when I’m looking for something. If I did, I would go to Twitter and search.

If Google does this, you will be playing right into the hands of MS. I’ll stop using Google as I’m sure many others who find Twitter useless will.

Martin Sumichrast June 15, 2009 at 3:15 pm

Looking forward to trying this out! Thanks Matt

Sam August 7, 2009 at 1:52 am

Is it possible to access the Speech Recognition engine in a similar manner using ASE? Specifically “RecognizerIntent”?

Nathan Zadoks October 5, 2009 at 2:00 pm

It’s pretty oversized. Let’s start with removing the useless formatting code
import android
droid=android.Android()
code=droid.scanBarcode()
url=”http://books.google.com?q=”+code['result']['SCAN_RESULT']
droid.startActivity(’android.intent.action.VIEW’, url)
We still have some useless assignments
import android
droid.startActivity(’android.intent.action.VIEW’, “http://books.google.com?q=”+android.Android().scanBarcode()['result']['SCAN_RESULT'])
Well, we still have an import. Let’s make it into a nice oneliner.Nathan
droid.startActivity(’android.intent.action.VIEW’, “http://books.google.com?q=”+__import__(’android’).Android().scanBarcode()['result']['SCAN_RESULT'])

Leave a Comment

If you have a question about your site specifically or a general question about search, your best bet is to post in our Webmaster Help Forum linked from http://google.com/webmasters

If you comment, please use your personal name, not your business name. Business names can sound salesy or spammy, and I would like to try people leaving their actual name instead.

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Search your bookshelf with a $65 barcode scanner

Next post: Add Custom Search to any site in two minutes