Barcode
warning
The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.
What are Barcode ?
A barcode is machine readable code used to store data for identification of items etc..

Barcode component is used to read barcode from input image.
readBarcode() - Read Barcode from input image
const char *readBarcode(cv::Mat Input1, symbol_type_t Input2)
- Description : readBarcode() takes input as image in OpenCV Mat format & Barcode format and returns the Barcode data as a (C)string. See Input and Output for details. 
- Parameters : - Input (via Arguments): - Input1: Image in OpenCV Mat format.
- Input2: Barcode format, used to read the type of barcode, - For example:
- SI_BARCODE_CODE128for CODE128 Barcode format.
- SI_BARCODE_CODE39for CODE39 Barcode format,
- SI_BARCODE_EAN8for EAN8 Barcode format,
- SI_BARCODE_EAN13for EAN13 Barcode format,
- SI_BARCODE_UPCAfor UPCA Barcode format,
- SI_BARCODE_UPCEfor UPCE Barcode format,
- SI_BARCODE_ISBN10for ISBN10 Barcode format,
- SI_BARCODE_ISBN13for ISBN13 Barcode format,
 
 
- Output : - on Sucess: Returns Barcode data in string (const char *) format.
- on Failure: Returns Error data in string (const char *) format.
 
- on Sucess: Returns Barcode data in string (
 
List of Barcode features implemented in Shunya stack
With Shunya stack you can make IoT device to act as a Barcode Reader.
note
Shunya Stack currently supports reading for these Barcode Types
- EAN8
- EAN13
- UPCA
- UPCE
- ISBN10
- ISBN13
- CODE39
- CODE128
For more info on barcode types, read here.
Using Barcode Shunya stack
Requirements to use Barcode Shunya stack
- Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
- Camera for Reading Barcode from live feed. (optional)
Steps to use Barcode Shunya stack
- Implementation as a Barcode Reader
note
Run the steps given below inside Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices) terminals.
Step 1: Implementation as a Barcode Reader
Let's try to implement Barcode Reader using Shunya stack
Steps are :
- Start with an ready to use template, - git clone https://gitlab.iotiot.in/repo-public/examples.git
 cd examples/simple-examples/barcode-reader-image
- Open the - barcode-reader.cppin an text editor and modify as per your use case.
- For our example, we need to first read the input image, add the code in your - main()function- /* Read Barocde image.
 * NOTE: Image must contain the Barcode of set format.
 */
 cv::Mat image = imread("barcode.png");
- To read the Barcode in the image, modify the code - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type CODE128 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_CODE128);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }
- More examples of Barcode code Reader in shunya stack - Read barcode of type EAN8
- Read barcode of type EAN13
- Read barcode of type UPCA
- Read barcode of type UPCE
- Read barcode of type ISBN10
- Read barcode of type ISBN13
- Read barcode of type CODE39
 - For reading Barcode code of Type EAN8, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type EAN8 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_EAN8);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type EAN13, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type EAN13 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_EAN13);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type UPCA, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type UPCA */
 std::string barcodeData = readBarcode(image, SI_BARCODE_UPCA);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type UPCE, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type UPCA */
 std::string barcodeData = readBarcode(image, SI_BARCODE_UPCE);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type ISBN10, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type ISBN10 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_ISBN10);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type ISBN13, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type ISBN13 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_ISBN13);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }- For reading Barcode code of Type CODE39, change code in step 4 with the example given below. - /* Read Barcode of set format in the image */
 /* Format set to detect Barcode of type CODE39 */
 std::string barcodeData = readBarcode(image, SI_BARCODE_CODE39);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }
Understand this component with an example (ready to use code)
Let's take an example use case: Say we need to
- Read barcode from live video feed.
- We will be using video component for capturing live camera feed.
Steps are :
- For our example, we need to first set input video to camera, - Please check here, how to setup the video source path in video component.
 
- Start with an ready to use template, - git clone https://gitlab.iotiot.in/repo-public/examples.git
 cd examples/full-examples/barcode
- Open the - barcode-reader.cppin an text editor and modify as per your use case.
- Load the video input settings, add the code in your - main()function- captureObj src;
 src = newCaptureDevice("video-source"); /* Argument = JSON Title, Load settings from JSON file */
- Capture the camera feed frame by frame, add the code in your - main()function- cv::Mat frame; /* Variable to store frame */
 while (1) {
 frame = captureFrameToMem(&src); /* Capture one frame at a time in a loop*/
 if (frame.empty()) {
 fprintf(stderr, "End of video file!.");
 closeCapture(&src);
 return EXIT_SUCCESS;
 }
 }
- Read Barcode in all the frames, add the code in your - while(1)loop- /* Find and read Barcodes of type CODE128 only */
 std::string barcodeData = readBarcode(image, SI_BARCODE_CODE128);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }
- At the end close video input, - closeCapture(&src);
- Finally your code should look like this, - #include <iostream>
 #include <cstdlib>
 #include <string>
 #include <errno.h>
 #include <opencv2/opencv.hpp>
 #include "si/video.h"
 #include "si/scanner.h"
 using namespace std;
 using namespace cv;
 using namespace zbar;
 int main(int argc, char *argv[])
 {
 captureObj src;
 src = newCaptureDevice("video-source"); /* Create capture Instance */
 Mat frame; /* Variable to store frame */
 while (1) {
 frame = captureFrameToMem(&src); /* Capture one frame at a time in a loop*/
 if (frame.empty()) {
 fprintf(stderr, "End of video file!.");
 closeCapture(&src);
 return EXIT_SUCCESS;
 }
 /* Find and read Barcodes of type CODE128 only */
 std::string barcodeData = readBarcode(image, SI_BARCODE_CODE128);
 if (barcodeData.compare(0, strlen("ERROR"), "ERROR") == 0) {
 std::cout<<"Error: " << barcodeData <<std::endl;
 } else {
 std::cout<<"Barcode Data: " << barcodeData <<std::endl;
 }
 }
 closeCapture(&src);
 return EXIT_SUCCESS;
 }
- Once you are done editing, save and compile the code , by running - mkdir build && cd build
 cmake ../
 make
- Run the code - sudo ./barcode-reader