Barcode Recognition with Google's ZXing on the Mac with TIFF Support

Posted on 15 January 2009 by Johannes Fahrenkrug. Tags: Programming Java Mac
Have you checked out Google's great open source barcode recognition library called ZXing ("zebra crossing")? It's really powerful, free and (halfway) platform independent since it's written in Java. If you're a Mac user, it might be a little tricky to get the JavaSE version to run, though. The core part of the library has a WTK (wireless toolkit) dependency, but there's no WTK for the Mac. The good news is: you don't need the _wireless_ toolkit if you want to run this thing on your desktop machine. So how do you get it to compile on your Mac? Easy:
  1. Download ZXing
  2. Unpack it.
  3. Read this post
  4. ... or just download my modified build.xml file and put it in core/build.xml
  5. Change into core and run ant
  6. Change into javase and run ant
  7. Change into the zxing root and try running the command line client:
    java -cp javase/javase.jar:core/core.jar com.google.zxing.client.j2se.CommandLineRunner http://www.idautomation.com/ucc_ean_128.jpeg
    The result should look like this:
    Raw result:
    81007123452112345678
    Parsed result:
    81007123452112345678
    
  8. Rejoice.
OK, so this works. But I had to get the barcode from the first page of a multipage TIFF file. That didn't work. But it was easy to fix. I had to modify javase/src/com/google/zxing/client/j2se/CommandLineRunner.java a little bit.
I imported some JAI packages:
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
And I changed one line in the static decode method from
image = ImageIO.read(uri.toURL());
to
PlanarImage pi = JAI.create("URL", uri.toURL());
image = pi.getAsBufferedImage();
(If you're too lazy to type that, get the file here)
Now just save the file, change back into the javase directory and run ant again. Now you should be able to decode barcodes in TIFF files as well. You might have to add the "--try_harder" argument to the CommandLineRunner, though. Without it, it didn't find the barcode in my TIFF file.
I'm not sure if it scans every page of the TIFF for a barcode or just the first. If you need to access a specific page, check out this code and this code to turn a RenderedImage into a BufferedImage.
Now enjoy your Mac-TIFF-barcode-reading awesomeness. 


Comments

sonomos said...

Sun / Oracle released the JME SDK for OS X a short while ago.

March 16, 2010 10:58 PM

Anonymous said...

Excellent info. Just what I needed. Thanks!

Andy

March 11, 2009 09:05 AM

Johannes Fahrenkrug said...

Hey Sean,

Thanks for the feedback. I understand that the WTK dependency makes sense for making sure it'll run on JavaME, but getting it to compile on the Mac out-of-the-box would surely be great :)
Thanks for all the hard work and this truly great library!

January 16, 2009 06:56 AM

srowen said...

Yeah great, thanks much for this. You are right, the WTK dependency from core is actually almost not even there. The only reason it is there is because we want to compile the core -- which does not depend on Javame classes, but cannot depend on non-Javame classes if it is to work in that environment -- against the Javame subset of Java's core APIs.

It would of course compile against desktop Java's classes too. I suppose you risk then changing the code in a way that compiles against Java but not Javame's subset. But that's only an issue for developers I suppose and it is a really small risk, and one we'd catch at release.

I could introduce a change to just use Java's classes when on a mac (which is what I use too). That would address this without need for tweaks. I'll get on it.

January 15, 2009 08:56 PM

Comments

Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus