Skip to content

QR Code Generation

1.1. QR Code Overview

A QR code is also called a two-dimensional barcode (2D code). The commonly used type is QR Code. QR stands for Quick Response. It is an encoding method that has become very popular on mobile devices in recent years. Compared with traditional barcodes (Bar Code), it can store more information and represent more data types. A QR code has a total of 40 sizes, which are officially called versions. Version 1 is a 21 x 21 matrix, Version 2 is a 25 x 25 matrix, and Version 3 is a 29 x 29 matrix. Each time the version increases by 1, the size increases by 4. The calculation formula is (V-1)*4 + 21, where V is the version number. The maximum is Version 40, and (40-1)*4+21 = 177, so the maximum size is a 177 x 177 square. QR codes have a certain error-correction capability. This error-correction function is mainly implemented using the Reed-Solomon error correction algorithm.

QR code structure:

1.1. QR Code Overview Figure 1

1.1. QR Code Overview Figure 1

1.2. Quick Start

1.2.1 Preparing the Development Environment

If you are reading this document for the first time, please first read “Start Guide/Preparing the Development Environment/Preparing and Updating the Easy-Eai Compilation Environment” and follow the steps there to set up the compilation environment.

Run the run script on the Ubuntu system on your PC to enter the EASY-EAI compilation environment. The details are as follows.

Terminal window
cd ~/develop_environment
./run.sh 2204

1.2.2 Downloading the Source Code and Compiling the Sample

In the EASY-EAI compilation environment, create a directory for managing the source code repository.

Terminal window
cd /opt
mkdir EASY-EAI-Toolkit
cd EASY-EAI-Toolkit

https://dl.dragonwake.com/download/rv1126b/embedded/EASY-EAI-Toolkit-1126B/Demos.zip

Download the Demos.zip compressed file from the link above, upload it to the /opt/EASY-EAI-Toolkit directory of the virtual machine, and decompress it. Go to the corresponding sample directory and perform the compilation operation. The specific commands are as follows.

Terminal window
cd EASY-EAI-Toolkit-1126B/Demos/common-qrcode/
./build.sh

💡 Note: Because the dependent libraries are deployed on the board, keep /mnt mounted during cross-compilation.

1.2.2 Downloading the Source Code and Compiling the Sample Figure 3

1.2.2 Downloading the Source Code and Compiling the Sample Figure 3

1.2.3 Running the Sample

Enter the board backend through serial debugging or SSH debugging, and go to the location where the sample is stored, as shown below.

Terminal window
cd /userdata/Demo/common-qrcode

The command for running the sample is as follows.

Terminal window
./test-QRCode

1.2.4 Execution Result

The execution result is as follows.

After running the test program, an image named “QRCode.png” is generated.

1.2.4 Execution Result Figure 4

1.2.4 Execution Result Figure 4

Return to the [Compilation Window] and use the eog command to open the image directly.

Terminal window
eog /mnt/userdata/Demo/common-qrcode/QRCode.png

1.2.4 Execution Result Figure 5

1.2.4 Execution Result Figure 5

If the EASY-EAI compilation environment prompts that the eog command cannot be found, install it using the following operations.

Terminal window
sudo apt-get update
sudo apt-get install eog

Alternatively, you can copy the image from the board to the PC.

Terminal window
cp /mnt/userdata/Demo/common-qrcode/QRCode.png ./Release

For a detailed API description and API calls (the source code of this sample), refer to the following description.

1.3. QR Code Generation API Description

The EASY EAI api encapsulates a QR code generation tool. A QR code image can be generated simply by passing in information, allowing users to easily generate QR code images for their own applications.

1.3.1 Include/Reference Method

The EASY EAI api library is located in the easyeai-api directory of this repository. To allow customers to directly call the EASY EAI api library in their local projects, the libraries, header files, and other items that need to be linked in the project are listed below.

DescriptionCMake EntryMakefile Entry
api.cmake${common_root}/qrcode/api.cmakeNone
Header File Directory${QRCODE_INCLUDE_DIRS}-I ../../easyeai-api/common/qrcode
Source File Directory${QRCODE_SOURCE_DIRS}../../easyeai-api/common/qrcode
Library File DirectoryNoneNone
Library Link Parameters${QRCODE_LIBS}None

The path of the API source code is: EASY-EAI-Toolkit-1126B/easyeai-api/common/qrcode/ Users can view the implementation of the interfaces through the source code and can also directly modify the source code.

1.3.2 QR Code Generation Operation

The prototype of the QR code generation function is as follows.

Terminal window
long StrToQRCode(const char \*file, const char \*pStr);

The specific description is shown in the table below.

Function Name: StrToQRCode()Details
Header Fileeasyeai-api/common/qrencode/qrcode.h
Input Parametersfile: save path of the QR code image to be generated (including the directory)
Example: /userdata/myQRCode.png
pStr: string required for QR code generation
Return ValueReturns 0 on success
Returns -1 on failure
NotesNone

1.4. Usage Example

The code path for the usage example of the QR code operation API is as follows:

EASY-EAI-Toolkit-1126B/Demos/common-qrcode/test-QRCode.c

Terminal window
StrToQRCode(QRCODE_PATH, "hello! my name is EasyEai-api !");