Reading barcodes

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)

Hello @th75,
after some workaround I wrote the following code - starting from your:

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

Capture img;

void setup() {
  size(640, 360);
  img = new Capture(this, 320,240);
  img.start();
}

void captureEvent(Capture img) {
 readBar(img); 
}

void draw() {
  if (img.available()) {
    img.read();
  }
  
  image(img, 0, 0);
}

void readBar(PImage video) {
  try {
    BufferedImage barCodeBufferedImage = getNative(video);
    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());
    exit();
  }
  catch(Exception e) {
    println("");
  }
}

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;
}

Unfortunately, it doesn’t read correctly the codes - I printed the code I posted earlier and it works, but other “real” codes are not correctly read

There are probably different types/ kinds of definitions for the codes. Did you check the specifications?

hi

could this run on android ?

well done,
as Chrisir said, i will suspect something about encoding…

by “are not correctly read”, do you mean it print wrong data or is not able to read them at all?

looking the image , do you think , image is more difficult than printed one?
for my fast try, i had a too bad camera for small real ones, so, to test, i searched barcode with google image and, filming the screen, all was read correctly…

in doc for MultiFormatReader , there is a “hint” option, where you can help by settings some normalised encoding,
list of hints here
(there is even a hint “try_harder” :face_with_monocle: )
may be set a key in your sketch to save img, to show us or to try it there: https://online-barcode-reader.inliteresearch.com/ and see what encoding is that
otherwise there is preprocessing before decode(bitmap), i will try to show bitmap on screen or save it, may be this preprocessing too have options (auto white balance, autocontrast…i don’t really know this library)
and last, if output is different than what is written,i will check if read is consistent in time?
:thinking:

something else in you code, i m not sure but i thing there is a timing problem:

capture Event reference
img is updated on img.read(); you update it in draw but you read bar code in capture event
i thing you need to do img.read(); first then readBar(img); and to do that sequence in capture event

(i think) capture event is an interruption triggered by the camera saying “hey i ve a new picture for you”
and draw is triggered by processing event “i need to draw something new to achieve expected framerate”, so those timing can be different

I didn’t; please consider that I’m a freshman coder and all seems so humongous that I can hardly imagine what you mean by “specifications” :slightly_smiling_face:

Well, it has correctly read the “giant” barcode printed on paper (A5 format), while

  • gives back the wrong number reading a shoes box barcode
  • doesn’t read at all the barcode printed on the cover of Learning Processing

I tried changing the light conditions especially for the book that has a shiny cover which probably makes it hard to be read; also tried to putting the objects closer of farther from the camera with no success.

I used my FaceTime camera - discovering that doesn’t autofocus the image; to overcome this issue, I tried to export the sketch to Android to test it with another camera, but the export failed - anyway, this is the chapter for another book

1 Like

see Barcode - Wikipedia