Skip to main content

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..

Oops!, No Image to display.

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_CODE128 for CODE128 Barcode format.
        • SI_BARCODE_CODE39 for CODE39 Barcode format,
        • SI_BARCODE_EAN8 for EAN8 Barcode format,
        • SI_BARCODE_EAN13 for EAN13 Barcode format,
        • SI_BARCODE_UPCA for UPCA Barcode format,
        • SI_BARCODE_UPCE for UPCE Barcode format,
        • SI_BARCODE_ISBN10 for ISBN10 Barcode format,
        • SI_BARCODE_ISBN13 for ISBN13 Barcode format,
    • Output :

      • on Sucess: Returns Barcode data in string (const char *) format.
      • on Failure: Returns Error data in string (const char *) format.

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

  1. EAN8
  2. EAN13
  3. UPCA
  4. UPCE
  5. ISBN10
  6. ISBN13
  7. CODE39
  8. CODE128

For more info on barcode types, read here.

Using Barcode Shunya stack

Requirements to use Barcode Shunya stack

  1. Shunya OS installed (supported Arm devices) or Shunya OS docker container (X86 based windows/linux devices)
  2. Camera for Reading Barcode from live feed. (optional)

Steps to use Barcode Shunya stack

  1. 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 :

  1. Start with an ready to use template,

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/simple-examples/barcode-reader-image
  2. Open the barcode-reader.cpp in an text editor and modify as per your use case.

  3. 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");
  4. 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;
    }
  5. More examples of Barcode code Reader in shunya stack

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

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 :

  1. 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.
  2. Start with an ready to use template,

    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/full-examples/barcode
  3. Open the barcode-reader.cpp in an text editor and modify as per your use case.

  4. 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 */
  5. 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;
    }

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

  7. At the end close video input,

    closeCapture(&src);    
  8. 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;
    }
  9. Once you are done editing, save and compile the code , by running

    mkdir build && cd build
    cmake ../
    make
  10. Run the code

    sudo ./barcode-reader

Facing errors with the component?