# Reading barcodes

**URL:** <https://discourse.processing.org/t/reading-barcodes/29868>\
**Category:** Coding Questions\
**Created:** [May 5, 2021, 8:56am UTC](https://discourse.processing.org/t/reading-barcodes/29868 "2021-05-05T08:56:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 8:56am UTC](https://discourse.processing.org/t/reading-barcodes/29868/1 "2021-05-05T08:56:15Z")

</div>

Ciao!  
I’d like to scan some UPC barcodes to get the product number, and after some research I unsuccessfully used the following libraries

- Zxing for Processing (unable to load some plugins for gstreamer 1.0) and
- QRCode (throws some errors in the console and seems dedicated to 2d barcodes)

Now I’m writing my own code starting from this image:

 ![campbell_soup_barcode](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c833004ed41b3d9163a1b140973572c1db0447a8.jpeg)

My approach is based on the image pixels’ brightness - if it’s greater than 200, draw a line:

```auto

PImage img;         

void setup() {
  size(1237, 1080);
  background(255);
  img = loadImage("campbell_soup_barcode.jpg");

}

void draw() {

  image(img, 0, 0);

  img.loadPixels();

  for (int x = 0; x < width; x++ ) {

    int loc = x + height/2 * img.width;
    float pixelBrightness = brightness(img.pixels[loc]);
      
    if (pixelBrightness > 200) {
      stroke(255,0,0);
      line(x, 0, x, height);
    }

  }
  img.updatePixels();

}

```

This is the result, which gives me a pretty good approximation of the code that I’m reading:

 ![barcode_readed](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1195ccdf8e66afa033badf9b51f5ba0141fc63b3.jpeg)

Now comes the hazy part: I need to read the distance between the two outer red stripes and split it by 95 to get the unit width and then getting the binary code.  
How can I do this??  
As far as I know, to use dist() you have to know the point location; is there another way to get the measure?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 9:43am UTC](https://discourse.processing.org/t/reading-barcodes/29868/2 "2021-05-05T09:43:52Z")

</div>

Woe…

You can use ANY java library with processing.

Sure, there is no library that works?

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 10:10am UTC](https://discourse.processing.org/t/reading-barcodes/29868/3 "2021-05-05T10:10:24Z")

</div>

I suppose the point are not the libraries, but their usage… probably there is some piece missing and I’m not able to fix it. On Windows I’m having some troubles with gstreamer, here’s the screenshot while running Zxing for Processing, the BarcodeReaderCam example:

 ![Immagine 2021-05-05 120424](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cf5350e670f9f09ee7e2634f216b0c0c20613c63.jpeg)

@Chrisir I’d appreciate a lot your suggestions on how to fix this…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 10:17am UTC](https://discourse.processing.org/t/reading-barcodes/29868/4 "2021-05-05T10:17:37Z")

</div>

When he doesn’t find the dll:

Do you have the dll?

You can (under windows) add the path to the path statement of windows iirc

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 10:22am UTC](https://discourse.processing.org/t/reading-barcodes/29868/5 "2021-05-05T10:22:31Z")

</div>

Really no expert for QR… but have you installed GStream? What does the documentation tell you?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 5, 2021, 10:36am UTC](https://discourse.processing.org/t/reading-barcodes/29868/6 "2021-05-05T10:36:25Z")

</div>

> [@Webcam Bar code/ QR code Read](https://discourse.processing.org/t/webcam-bar-code-qr-code-read/2911/6):
>

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 11:03am UTC](https://discourse.processing.org/t/reading-barcodes/29868/7 "2021-05-05T11:03:51Z")

</div>

I neither have that dll nor found it reinstalling gstreamer; why does the software seek for a windows32 dll while I’m running a 64-bit OS?

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 11:04am UTC](https://discourse.processing.org/t/reading-barcodes/29868/8 "2021-05-05T11:04:22Z")

</div>

Does it work even for 1D barcodes?

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 5, 2021, 11:07am UTC](https://discourse.processing.org/t/reading-barcodes/29868/9 "2021-05-05T11:07:12Z")

</div>

i have not trying it

but i found out that if any project could achieve without any kind of libraries is more flexible

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 11:07am UTC](https://discourse.processing.org/t/reading-barcodes/29868/10 "2021-05-05T11:07:49Z")

</div>

And, by the way, any suggestion on how to get the distance between two parallel lines?

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 11:10am UTC](https://discourse.processing.org/t/reading-barcodes/29868/11 "2021-05-05T11:10:24Z")

</div>

@jafal thank you for your support.  
Before opening this topic I discarded that solution as seems to import too many libraries and it’s unclear if it’s oriented only to QR codes.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 11:24am UTC](https://discourse.processing.org/t/reading-barcodes/29868/12 "2021-05-05T11:24:16Z")

</div>

Did you see: [Processing QRCode Library | Daniel Shiffman](https://shiffman.net/p5/qrcode-processing/)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 5, 2021, 11:30am UTC](https://discourse.processing.org/t/reading-barcodes/29868/13 "2021-05-05T11:30:28Z")

</div>

take a look to this project

no libraries and its multi task you can modifies it as logic analyses or any other idea by replacing the ir receiver in Arduino

there is much to learn in this sketch despite its easy arrays, drawing lines in pattern, briny conversion, serial communication, spiting, timing and much more … you can play a lot with it

> [@Infrared remote control](https://discourse.processing.org/t/infrared-remote-control/29301):
>
> I found this sketch on a Japanese site and it has several errors i fix some of them but still not working any help thanks everyone import processing.serial.\*; Serial myPort ; int [] recv\_data = new int [200]; float x , pre\_x ; boolean state = true ; boolean requested = false ; boolean received = false ; float t\_margin , b\_margin , r\_margin , l\_margin ; int base\_t = 0 ; void setup () { myPort = new Serial ( this , Serial . list () [0], 115200 );…

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 5, 2021, 11:47am UTC](https://discourse.processing.org/t/reading-barcodes/29868/14 "2021-05-05T11:47:41Z")

</div>

i am on win 7 64 bit i download the library now its working but my pc without camera

```auto
/*
QRcode reader
 Generate images from a QRcode generator such as
 http://qrcode.kaywa.com/ and put them in this sketch's
 data folder.
 Press spacebar to read from the camera, generate an image,
 and scan for barcodes. Press f to read from a file and scan.
 Press s for camera settings.
 Created 9 June 2007
 by Tom Igoe / Daniel Shiffman
 */

import processing.video.*;
import qrcodeprocessing.*;

Capture video; // instance of the video capture library
String statusMsg = "Waiting for an image"; // a string to return messages:

// Decoder object from prdecoder library
Decoder decoder;

void setup() {
  size(400, 320);
  video = new Capture(this, 320, 240);
  video.start();

  // Create a decoder object
  decoder = new Decoder(this);
}

// When the decoder object finishes
// this method will be invoked.
void decoderEvent(Decoder decoder) {
  statusMsg = decoder.getDecodedString();
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  background(0);

  // Display video
  image(video, 0, 0);
  // Display status
  text(statusMsg, 10, height-4);

  // If we are currently decoding
  if (decoder.decoding()) {
    // Display the image being decoded
    PImage show = decoder.getImage();
    image(show, 0, 0, show.width/4, show.height/4); 
    statusMsg = "Decoding image";
    for (int i = 0; i < (frameCount/2) % 10; i++) statusMsg += ".";
  }
}

void keyReleased() {
  // Depending on which key is hit, do different things:
  switch (key) {
  case ' ':        
    // Spacebar takes a picture and tests it:
    // copy it to the PImage savedFrame:
    PImage savedFrame = createImage(video.width, video.height, RGB);
    savedFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
    savedFrame.updatePixels();
    // Decode savedFrame
    decoder.decodeImage(savedFrame);
    break;
  case 'f': // f runs a test on a file
    PImage preservedFrame = loadImage("qrcode.png");
    // Decode file
    decoder.decodeImage(preservedFrame);
    break;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 1:55pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/15 "2021-05-05T13:55:18Z")

</div>

Well yes I tried to rerun the sketch QRCode example from QRCode library and by pressing spacebar or f it acquires the image to be read from the camera or from the file, despite of the error messages in the console.  
However it seems to be working only with QR codes, I need 1D barcodes instead

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 2:52pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/16 "2021-05-05T14:52:39Z")

</div>

I can’t help you with that

As I said it should exist a java library since it is a standard problem

To analyze an image, this is hard- maybe check openCV library

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2021, 2:55pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/17 "2021-05-05T14:55:59Z")

</div>

See [GitHub - zxing/zxing: ZXing ("Zebra Crossing") barcode scanning library for Java, Android](https://github.com/zxing/zxing)

Or [Barcodes recognition: How to create barcode scanner in Java with ABBYY Cloud OCR SDK – Help Center](https://support.abbyy.com/hc/en-us/articles/360017269380-Barcodes-recognition-How-to-create-barcode-scanner-in-Java-with-ABBYY-Cloud-OCR-SDK)

---

<div class="post-metadata">

**Author:** ![th75](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@th75](https://discourse.processing.org/u/th75)\
**Post date:** [May 5, 2021, 5:36pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/18 "2021-05-05T17:36:21Z")

</div>

if you want to process images as the sample you shown, image processing can be a fun project.

but if you want to process images from webcam, i will recommand using Zxing too. dealing with “real life” images, with poor contrast , noises, rotation artefact, color artefact and … it is really hard

and in the other hand, Zxing require only one jar:  
org.apache.servicemix.bundles.zxing-3.4.1\_1.jar

( no extra dll needed or install )

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [May 5, 2021, 8:32pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/19 "2021-05-05T20:32:09Z")

</div>

> [@th75](#):
>
> if you want to process images as the sample you shown, image processing can be a fun project.

I agree. As soon as it’ll be ready I’ll post my solution.

> [@th75](#):
>
> and in the other hand, Zxing require only one jar:  
> org.apache.servicemix.bundles.zxing-3.4.1\_1.jar

Would you be so kind to explain how to use it?  
Thank you for your support!

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [May 5, 2021, 9:22pm UTC](https://discourse.processing.org/t/reading-barcodes/29868/20 "2021-05-05T21:22:32Z")

</div>

Hello Th3cG,

You can find below my take on your problem.

This is the process I did:

1. Extract the middle row of the image
2. Convert the color to 0 or 1
3. Get rid of the sides by finding the first black pixel in each direction
4. Find the size of each band by dividing pixel total width (in pixels) by expected nb of bands
5. sum and average pixel values of each band to determine if 0 or 1.

Of course it implies that the image is straight and the bar code centered but this can be done easily with openCV for example.

> **Here the code:**
>
> ```auto
> import java.util.Arrays;
> 
> final int bitNB = 95;
> PImage barCode;
> int[] singleRow;
> int[] result = new int[bitNB];
> int beg, end;
> float stepSize;
> 
> void setup() {
> barCode = loadImage("barCode.jpeg");
>   
> // Get the middle row of the picture
> int midRow = barCode.height / 2;
>   
> // Extract the color value of the middle row
> barCode.loadPixels();
> beg = midRow * barCode.width;
> end = beg + barCode.width;
> singleRow = Arrays.copyOfRange(barCode.pixels, beg, end);
>   
> // Convert the value to 0 or 1
> for (int i = 0; i < singleRow.length; i++) {
> singleRow[i] = (singleRow[i] & 0xFF) > 125 ? 1 : 0;
> }
>   
> // Find first black pixels
> for (int i = 0; i < singleRow.length; i++) {
> if (singleRow[i] == 0) {
> beg = i;
> break;
> }
> }
>   
> // Find last black pixels
> for (int i = singleRow.length - 1; i >= 0; i--) {
> if (singleRow[i] == 0) {
> end = i;
> break;
> }
> }
>   
> // Find "width" of each bar
> stepSize = (float)(end - beg) / bitNB;
> float left = 0;
> float right = stepSize;
>   
> // Read the bar code
> for (int n = 0; n < bitNB; n++) {
> // Set start point
> beg = floor(left);
> if (left - beg > 0.6) beg++; // the width can be a float number. Consider a pixel if at least 40% is within the range
>     
> // Set end point
> end = ceil(right);
> if(end - right > 0.6) right--; // the width can be a float number. Consider a pixel if at least 40% is within the range
>     
> // Sum and average the value
> float sum = 0;
> for (int i = beg; i < end + 1; i++) {
> sum += singleRow[i];
> }
> sum /= end - beg + 1;
>     
> // Store value in result
> result[n] = sum > 0.5 ? 1 : 0;
>     
> left = right;
> right += stepSize;
> }
>   
> println(singleRow);
> println(beg, end);
> println(result);
> }
> 
> ```

[Next page](https://discourse.processing.org/t/reading-barcodes/29868.md?page=2)
