QR Code Recognition Example
HuskyLens 2 QR code recognition reads count, IDs and positions in real time, then prints them on the UNIHIKER K10 display through cached results APIs. It’s like a smart camera plus dashboard that scans every visible QR code, reports total number, center ID, and detailed data such as content, width, height and X/Y center for specific IDs, including multiple codes sharing ID0.
1. QR Code Recognition Blocks
Before You Start:
1.1 Recognize QR Codes and Output Related Data
HUSKYLENS 2 can recognize QR codes appearing in the image. You can obtain the related data of the detected QR codes through programming. The QR code data that can be read includes: the total number of detected QR codes, and the data of a specified QR code, which includes QR code ID, content, width, height, as well as the X and Y coordinates of the center point.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: QR code 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_QRCODE_RECOGNITION); // Set to QR code recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION); // Get latest QR code recognition data
if (huskylens.available(ALGORITHM_QRCODE_RECOGNITION)) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("Center QR code ID: "), 1, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_QRCODE_RECOGNITION), Result, ID)), 2, 0x0000FF);
k10.canvas->canvasText(String("Total QR codes: ") + String(huskylens.getCachedResultNum(ALGORITHM_QRCODE_RECOGNITION)), 3, 0x0000FF);
k10.canvas->canvasText(String("First QR code ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_QRCODE_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
After uploading the program, wait for the upload to complete. Point the HUSKYLENS 2 camera at the QR code in the image to learn it. For detailed learning instructions, please refer to: QR Code Recognition Function - Learning QR Codes:QR Code Recognition.
Point the HUSKYLENS 2 camera at the QR code and observe the result displayed on the UNIHIKER K10 screen. As shown in the figure, the output will include: Number of detected QR codes (regardless of whether they have been learned);Specified QR code ID; Unlearned QR codes will be set to 0.
1.2 Get Related Data of Specified QR Code in the Screen
After HuskyLens 2 recognizes a QR code, it can obtain the relevant data of the specified QR code in the screen. For example, determine whether a QR code with a specified ID is present in the screen, count the number of QR codes with the same ID in the screen. When multiple QR codes with the same ID appear in the screen, you can specify to obtain the parameters of one of the QR codes, including name, content, X/Y coordinates, width, and height.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: QR code 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_QRCODE_RECOGNITION); // Set to QR code recognition mode
}
void loop() {
huskylens.getResult(ALGORITHM_QRCODE_RECOGNITION); // Get latest QR code recognition data
if (huskylens.available(ALGORITHM_QRCODE_RECOGNITION)) {
// Check if ID0 QR code exists and display its info
if (huskylens.getCachedResultByID(ALGORITHM_QRCODE_RECOGNITION, 0) != NULL) {
// Simplify index: 1-1→0 (1st QR code), 2-1→1 (2nd QR code)
k10.canvas->canvasText(String("ID0 QR code count: "), 1, 0x0000FF);
k10.canvas->canvasText(String(huskylens.getCachedResultNumByID(ALGORITHM_QRCODE_RECOGNITION, 0)), 2, 0x0000FF);
k10.canvas->canvasText("1st ID0 QR code", 3, 0x0000FF);
k10.canvas->canvasText(String("Content: ") + String(RET_ITEM_STR(huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 0), Result, content)), 4, 0x0000FF);
k10.canvas->canvasText("2nd ID0 QR code", 6, 0x0000FF);
k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_RECOGNITION, 0, 1), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_QRCODE_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 figure, there are two unlearned QR codes (ID 0) in the image. The first ID0 QR code is on the right, which has the content "DFROBOT". The second ID0 QR code is on the left, and its coordinates are (245, 251).
Was this article helpful?
