We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Use zxing barcode/QR library with video capture
Page Index Toggle Pages: 1
Use zxing barcode/QR library with video capture (Read 2719 times)
Use zxing barcode/QR library with video capture
Apr 9th, 2010, 6:38am
 
Hi.  I'm trying to use the Google  zxing library in a sketch and I'm hoping for a last bit of advice to get it working.

So far, I've:

* built it using ant, as described here: http://code.google.com/p/zxing/wiki/GettingStarted

*  Added both the core.jar and the javase.jar to the project.  

* Checked out the "Developer Notes" page, which is here: http://code.google.com/p/zxing/wiki/DeveloperNotes

This is where it gets a bit hazy.  First, there were a few ambiguities in finding the various classes, which I was able to fix by explicitly placing the xzing paths in the code.  (You'll see this in the code example, which I've included at the end.)  

Once I did this, the sketch compiled and ran, and even takes a picture from the screen when you press a key (hooray!) and tries to pass it to xzing.  But, then it bombs out with the error:

java.lang.IllegalArgumentException: stream == null!

I think this is something simple, like I'm not passing the image correctly from the cam.read() into the xzing libraries, which is what I'm hoping someone can explain, here.  

So, with this long preamble, here's the code I'm using.  Any help GREATLY appreciate:

import processing.video.*;
import com.google.zxing.*;
import com.google.zxing.client.j2se.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

Capture cam;
com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();

void setup() {
 size(640, 480);
 cam = new Capture(this, 320, 240);
}


void keyPressed() {
 if (cam.available() == true) {
   cam.read();    
   image(cam, 160, 100);
   try {
      BufferedImage myImage = ImageIO.read(ImageIO.createImageInputStream(cam));
      LuminanceSource source = new BufferedImageLuminanceSource(myImage);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));    
      Result result = reader.decode(bitmap);
      println(result.getText());
   } catch (Exception e) {
     println(e.toString());
   }
 }
}  

void draw() {
}


Thanks for any help or insights.

Andrew


Re: Use zxing barcode/QR library with video capture
Reply #1 - Apr 9th, 2010, 7:54am
 
Maybe try:
BufferedImage myImage = cam.getImage();
instead of
BufferedImage myImage = ImageIO.read(ImageIO.createImageInputStream(cam));
(ImageIO doesn't know what a PImage is).
Re: Use zxing barcode/QR library with video capture
Reply #2 - Apr 9th, 2010, 8:38am
 
Thanks, PhiLHo -- that gave me just the info I needed.  What you suggested was close, but there was one more small step.  I needed to create a buffered image, grab its graphics context, and then draw the image there.  Once I did that, it works great.

Here's the revised code, which eliminates a few of the unnecessary lines (for example, you don't need to import j2se.  Also, it turns out that zxing is smoking fast, so you can stick it right in the draw() method for "real time" identification.  (Which actually turns out to be good, because it can take several times before one of these codes will register because of blur, angle, weird light, etc).  

Anyway, here's the code (and , I'd love to see other improvements or alternatives!):

import processing.video.*;
import com.google.zxing.*;
import java.awt.image.BufferedImage;

Capture cam;
com.google.zxing.Reader reader = new com.google.zxing.MultiFormatReader();

void setup() {
 size(640, 480);
 cam = new Capture(this, 320, 240);
}


void draw() {
 if (cam.available() == true) {
   cam.read();    
   image(cam, 160, 100);
   try {
      //Create a bufferedimage
      BufferedImage buf = new BufferedImage(320,240, 1); // last arg (1) is the same as TYPE_INT_RGB
      buf.getGraphics().drawImage(cam.getImage(),0,0,null);
      // Now test to see if it has a QR code embedded in it
      LuminanceSource source = new BufferedImageLuminanceSource(buf);
      BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));    
      Result result = reader.decode(bitmap);
      println(result.getText());
   } catch (Exception e) {
     println(e.toString());
   }
 }
}

Page Index Toggle Pages: 1