Barcode Recognition Example

HUSKYLENS 2 barcode mode lets Arduino count, locate and print barcode IDs, content and center coordinates via serial, focusing either on the tag nearest the crosshair or all tags sharing a chosen ID; it’s like a smart camera that filters and reports only the barcodes you care about.

1.Barcode Recognition Code Example

1.1Identify Barcode and Output Relevant Data

Under the barcode recognition feature, HUSKYLENS 2 can detect barcodes present in the image. Through programming, you can count the total number of detected barcodes, retrieve data for the tag closest to the crosshair, and print this information via the serial port. The readable tag data includes: barcode ID, barcode content, barcode width, barcode height, and the X and Y coordinate positions of its center point.You can refer to Barcode Recognition.

The example program:

#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;


// Main program starts
void setup() {
	Serial.begin(9600);
	Wire.begin();
  while (!huskylens.begin(Wire)) {
    delay(100);
  }
	huskylens.switchAlgorithm(ALGORITHM_BARCODE_RECOGNITION);
}
void loop() {
	huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);
	if ((huskylens.available(ALGORITHM_BARCODE_RECOGNITION))) {
		Serial.println((String("Total number of barcodes: ") + String((huskylens.getCachedResultNum(ALGORITHM_BARCODE_RECOGNITION)))));
		Serial.println((String("ID of the barcode near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, ID)))));
		Serial.println((String("Center coordinates of the barcode near the center: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_BARCODE_RECOGNITION), Result, yCenter)))))));
		Serial.println((String("ID of the first detected barcode: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_BARCODE_RECOGNITION, 1-1), Result, ID)))));
	}
	delay(500);
}

When a barcode is recognized, the HUSKYLENS 2 screen will frame the tag and display the parsed barcode content in the top-left corner of the box. Open the serial port in Mind+ to observe the serial port output of the relevant data from the barcode closest to the crosshair.

Running Result: Unlearned barcodes, as shown, will have a default ID of 0, and the barcode content output via the serial port will be consistent with that displayed on the HUSKYLENS 2 screen.

Interface Diagram

1.2 Identify Barcode and Output Relevant Data

When multiple barcodes with the same ID appear in the image (learned tags are assigned IDs in the order of learning, while unlearned tags are uniformly assigned ID 0), you can use the following example program to count the number of barcodes with the same ID in the image and obtain relevant data of a specified barcode with that ID.

Here is the example program:

#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;


// Main program starts
void setup() {
  Serial.begin(9600);
  Wire.begin();
  while (!huskylens.begin(Wire)) {
    delay(100);
  }
  huskylens.switchAlgorithm(ALGORITHM_BARCODE_RECOGNITION);
}
void loop() {
  huskylens.getResult(ALGORITHM_BARCODE_RECOGNITION);
  if ((huskylens.available(ALGORITHM_BARCODE_RECOGNITION))) {
    if (((huskylens.getCachedResultByID(ALGORITHM_BARCODE_RECOGNITION, 1) != NULL))) {
      Serial.println((String("Total number of ID1 barcodes: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_BARCODE_RECOGNITION, 1)))));
      Serial.println((String("Content of ID1 barcode: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_BARCODE_RECOGNITION, 1), Result, content)))));
      Serial.println((String("Center coordinates of the first ID1 barcode: ") + String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 1, 1-1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_BARCODE_RECOGNITION, 1, 1-1), Result, yCenter)))))))));
    }
  }
  delay(500);
}

Running Result: As follows, when multiple barcodes appear in the image, HUSKYLENS 2 can recognize all barcodes (both unlearned and learned), and the total number of barcodes in the current image can be observed through the serial port. At the same time, the serial port output will print the relevant data of the barcode with the specified ID 1.

Interface Diagram

Was this article helpful?

TOP