Barcode Recognition Example

HuskyLens 2 barcode mode reads IDs, content and positions, then UNIHIKER K10 shows counts and details on screen. It’s like a smart camera that scans product barcodes and instantly lists how many were seen and where they are in the image.

1. Barcode Recognition Blocks

Before You Start:

1.1 Recognize Barcode and Output Related Data

The HUSKYLENS 2 can recognize barcodes in the image and, through programming, obtain the relevant detected barcode data from the image. The readable barcode data includes: the total number of detected barcodes, and data of a specified barcode, which includes barcode ID, content, width, height, and the X and Y coordinates of the barcode's center point.

The example program as follows:

#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: barcode recognition; UNIHIKER K10: screen)
HuskylensV2  huskylens;
UNIHIKER_K10 k10;
uint8_t      screen_dir = 2;  // Screen display direction


// Main program initialization
void setup() {
  k10.begin();          // Initialize UNIHIKER K10
  Wire.begin();         // Initialize I2C communication

  // Wait for Huskylens connection (retry every 100ms)
  while (!huskylens.begin(Wire)) {
    delay(100);
  }

  k10.initScreen(screen_dir);       // Init screen with set direction
  k10.creatCanvas();                // Create drawing canvas
  huskylens.switchAlgorithm(ALGORITHM_BARCODE_RECOGNITION);  // Set to barcode recognition mode
}

void loop() {
  huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);  // Get latest barcode recognition data

  if (huskylens.available(ALGORITHM_BARCODE_RECOGNITION)) {
    // Simplify index: 1-1 → 0 (more intuitive, same meaning)
    k10.canvas->canvasText(String("Center barcode ID: ") + String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, ID)), 1, 0x0000FF);
    k10.canvas->canvasText(String("Total barcodes: ") + String(huskylens.getCachedResultNum(ALGORITHM_BARCODE_RECOGNITION)), 3, 0x0000FF);
    k10.canvas->canvasText(String("First barcode ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_BARCODE_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
  }

  k10.canvas->updateCanvas();  // Refresh screen
  delay(50);                   // 50ms delay for stable data reading
}

Upload the program, wait until it finishes uploading, then align the HuskyLens 2 camera with the barcode in the image to start learning. For detailed steps on learning barcodes, see the Barcode Recognition.

Align the HuskyLens 2 camera with the barcode and observe the result displayed on the UNIHIKER K10 screen.

Running result:As shown in the figure, it can output the number of detected barcodes (whether the barcode has been learned or not) and the specified barcode ID. For unlearned barcodes, the ID will be 0.

Interface Diagram

1.2 Acquiring Relevant Data of the Specified Barcode

After HuskyLens 2 recognizes a barcode, it can obtain relevant data about the specified barcode in the image. For example, check if a specified ID barcode is present in the image, count the number of barcodes with the same ID in the image, and when multiple barcodes with the same ID appear, specify to obtain the relevant parameters of one of the barcodes, including name, content, X/Y coordinates, width, and height.

The example program is provided below:

#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: barcode recognition; UNIHIKER K10: screen)
HuskylensV2  huskylens;
UNIHIKER_K10 k10;
uint8_t      screen_dir = 2;  // Screen display direction


// Main program initialization
void setup() {
  k10.begin();          // Initialize UNIHIKER K10
  Wire.begin();         // Initialize I2C communication

  // Wait for Huskylens connection (retry every 100ms)
  while (!huskylens.begin(Wire)) {
    delay(100);
  }

  k10.initScreen(screen_dir);       // Init screen with set direction
  k10.creatCanvas();                // Create drawing canvas
  huskylens.switchAlgorithm(ALGORITHM_BARCODE_RECOGNITION);  // Set to barcode recognition mode
}

void loop() {
  huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);  // Get latest barcode recognition data

  if (huskylens.available(ALGORITHM_BARCODE_RECOGNITION)) {
    // Check if ID0 barcode exists and display its info
    if (huskylens.getCachedResultByID(ALGORITHM_BARCODE_RECOGNITION, 0) != NULL) {
      // Simplify index: 1-1→0 (1st barcode), 2-1→1 (2nd barcode)
      k10.canvas->canvasText(String("ID0 barcode count: ") , 1, 0x0000FF);
       k10.canvas->canvasText(String(huskylens.getCachedResultNumByID(ALGORITHM_BARCODE_RECOGNITION, 0)), 2, 0x0000FF);
      k10.canvas->canvasText("1st ID0 barcode", 3, 0x0000FF);
      k10.canvas->canvasText(String("Content: ") + String(RET_ITEM_STR(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 0), Result, content)), 4, 0x0000FF);
      k10.canvas->canvasText("2nd ID0 barcode", 6, 0x0000FF);
      k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 0, 1), Result, yCenter)), 7, 0x0000FF);
    }
  }

  k10.canvas->updateCanvas();  // Refresh screen
  delay(50);                   // 50ms delay for stable data reading
}

Running result:As shown in the image, there are two unlearned barcodes (ID 0). The first ID0 barcode (on the right) has content "world"; the second ID0 barcode (on the right) has coordinates (151, 224).

Interface Diagram

Was this article helpful?

TOP