i did a test, i can’t explain how zxing work but i can tell you how i came to a working testcode (may be not the best):
i googled zxing read bar code and i found this code :
void readBar(BufferedImage barCodeBufferedImage){
try{
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
System.out.println("Barcode text is " + result.getText());
}catch(Exception e){println(e);}
}
it looks doing what we want , taking an image and print barcode
in processing you get few errors like “The class “BufferedImage” does not exist”
google BufferedImage and i see it need :
import java.awt.image.BufferedImage;
to avoid all errors you need:
import com.google.zxing.*;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
awt is already installed with java, but you need to add zxing, googled zxing.jar and i found it here
added this jar in a directory named “code” inside my sketch directory
no import error anymore, but readBar need a BufferedImage (not processing PImage)
in processing’s forum i searched pimage to BufferedImage and found:
BufferedImage getNative(PImage img) { // ignore
loadPixels();
int type = 1;//(format == RGB) ?
//BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage image = new BufferedImage(img.width, img.height, type);
WritableRaster wr = image.getRaster();
wr.setDataElements(0, 0, img.width, img.height, img.pixels);
return image;
}
so we have everything needed
i changed :
void readBar(BufferedImage barCodeBufferedImage){
try{
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
in
void readBar(PImage video){
try{BufferedImage barCodeBufferedImage = getNative(video);
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
and it works
(well… my webcam is so bad, image is too blur for a real bar code, but showing a big one , it read it well)