<< return to Pixycam.com

Barcode (image width in pixels)


I want to find the distance of the barcode from the pixy2. Although, I have gone through the posts in the forum, I still have some queries. How can I get the width in pixels of the barcode? I am currently running the line algorithm with the barcode.
I also had a query regarding the box and the number around the barcode. I have tried finding the code to check the lines for the box and the number but I was unable to get it. I have checked the pixy.h code but I am unable to get the entire code. Can someone please help me with this!


Hi, unfortunately the line tracking algorithm does not return pixel width of the barcode.

For the second part of your question, I’m assuming you’re talking about the label that PixyMon gives to each barcode - those can be set in PixyMon, like this: https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:pixymon_index#barcode-labels-tab

The line tracking code returns the number of the barcode, inside the Barcode struct as uint8_t m_code

Hope this helps!



Thank you so much for the reply. Okay, does that mean I need to include the color code block algorithm along with the line tracking algorithm to know the pixel width of the barcode?
Can I include both line and block code algorithm in one?

Yes, I am asking about the label. I checked in PixyMon the barcode is not labelled over there. Are the labels given in the code? According to he link, I need to put the label manually whereas when I run the line tracking algorithm the label for the barcode is displayed automatically. Also, I had to know how is the bounding box around the barcode displayed? Can you please highlight on this!



I’m having trouble understanding this question. Note, that the color-connected-components and line-tracking are separate programs. Pixy2 runs one or the other, but not both. So you wouldn’t be able to get a color codes and barcodes at the same time because they are separate programs. (ccc and line-tracking)

The bounding box is sent over USB to PIxyMon using a rendering object. It’s true that this data isn’t sent across serial to microcontrollers. We may consider adding this. Can you describe your application and why you would like the width/height of the barcode?



Thank you so much for your reply.
Okay. My application is to find the distance of the barcode from the Pixy camera. And to find this, I require the pixel width of the barcode. I’m unable to get the width and height of the barcode.
Can you please suggest if there is any way to find the distance between the Pixy and the barcode?

Can you please elaborate on the bounding box sent over the USB to pixyMon. How is it done? Or please provide me a link to get the information regarding it.
Also can I know the label of the barcode as it is displayed automatically. Is it because of the uint8_t mode? This mode sets the label of the barcode?


As said, the bounding box is sent over USB to PixyMon using a rendering object.
1.The rendering object can be a barcode?
2. Can I label the barcodes manually? As per the line tracking algorithm the barcode is tracked and labelled with a bounding box automatically.
3. Can I please know how is the PixyMon programmed? How is the data sent from the USB to PixyMon?
4. Although, I have tried finding the source code and tried my level best to analyze, can I please know if I can build my own firmware? Is there any other thing apart from the data present here: https://github.com/charmedlabs/pixy2, if I am not missing anything?
5. Can I send the data across serial to Arduino?
6. It would be beneficial if you could provide me a hint/suggest something to find the distance between the Pixy and the barcode.

Thank you in advance.


The barcodes are labeled and given bounding boxes in PixyMon. PixyMon receives this information via USB through special rendering packet sent by Pixy. Some of the information in the rendering packet (bounding box, label) is not available via serial (UART, SPI, and I2C). We will log your issue as a feature request.

Information on building your own firmware can be found here:



Thank you. However, I have tried going in the firmware and analyzing it. It is hard to find how the width of the barcode is sent in pixels.

Can you please help me regarding finding the distance between the Pixy and the barcode? Is there any way I can calculate the distance? It would be more easier if I could the width of the barcode in pixels. It would be beneficial if you could throw some light on it?


I agree that if you knew the width of the code in pixels, this would give you a way to estimate distance. But that is currently not supported and I can’t give you a timeframe for when it will possibly be supported.

Modifying the firmware is a possibility, but it requires some level of experience and grit (unfortunately). The relevant code is in line.cpp, but I’m not an expert with the firmware.

Note, if the barcodes are on the ground you can use simple trigonometry to calculate distance. That is, barcodes higher in the image are farther away and barcodes lower in the image are closer. Could this be used in your application? If not, can you describe your application?



okay.As told by you, I saw the line.cpp file but I am unable to understand the link between the USB to PixyMon and PixyMon to Arduino. Can you please tell me how is the data from the USB sent over to PixyMon? And, which are the files linked with it? I have tried finding all the files and the thing which I understood is: the barcode data can be managed in the ldtmodule.cpp. pixy2line.h is connected to PixyMon through Tpixy.h?

Also, can you please give a clear idea of the data sent from the PixyMon to the Arduino? It would be beneficial if you could tell the files associated with the link between the PixyMon and the Arduino.
Can you please help me with this!

Thank you


I’m sorry, but I haven’t spent much time looking at line.cpp. I’ll tell you what I’ve done in the past when I’m trying to understand source code that’s poorly documented. I look at the code and form some ideas of what certain parts of it do, and then I make little changes and see how my changes affect things.

If you have a specific question you’d like answered about the firmware, I’ll try to track down the answer for you. Asking multiple questions about the firmware. is probably not the best way to get the information you want. (just my $.02)



Thank you for your reply. Yes, the source code is poorly documented.
It would be nice if more information about the firmware would be provided on the link. Anyway, thank you.