Odewahn
YaBB Newbies
Offline
Posts: 5
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()); } } }