# Design of Cigarette QR Code Decoding System Based on STM32

Authors: Zhu Haoran; Chen Hao; Wei Taicheng
Journal: Security and Communication Networks
 (2022)

---

## Abstract

This paper mainly studies the design method of the hardware and software of the cigarette strip QR code decoding based on STM32 microprocessor. This paper expounds the principle of hardware circuit design with STM32F407 microprocessor as the core, analyzes the steps of analyzing the QR code decoding algorithm based on the modular programming idea of C language, and analyzes the result of cigarette-bar QR code decoding. The results of field application show that the system can meet the design requirements of cigarette QR code decoding and improve the recognition rate of two-dimensional code, which has high application value.


---

## Body

## 1. Introduction

With the proposal of the intelligent construction plan of the cigarette system, the market-oriented reform of the cigarette industry is moving closer to “Internet + Cigarette.” Relying on two-dimensional code technology, the cigarette industry can establish a “one item, one code” information association system and can build a network system for intelligent decision-making and control. It is of great significance for the industry to optimize industrial upgrading and improve management and service levels. Therefore, it has become a trend for cigarette companies to introduce QR code technology into cigarette packaging [1]. The two-dimensional code on cigarettes must be accurate to small boxes and cigarettes, according to the characteristics of cigarette packaging, so that the cigarette Internet of Things can accurately grasp changes in cigarette market information, which raises the bar for decoding and analysis of the two-dimensional code on cigarettes. However, the legitimacy of the QR code cannot be determined manually, and the information contained in the QR code cannot be quickly deciphered. Industrial applications may be realised by analyzing the information of the QR code on the cigarette packet using decoding technology based on the STM32 microcontroller [2-3].This study mainly introduces the hardware construction of two-dimensional code decoding based on STM32 microprocessor, the compilation of software algorithms under the Keil programming environment, and the analysis of two-dimensional code information.


## 2. Design of Hardware

### 2.1. Microprocessor Selection

The core part of hardware is the microprocessor, which adopts ST's STM32F407 ARM chip. Its on-chip DCMI module supports the use of VGA-like timing to obtain image data streams and can accept external multibit data streams, such as data sent by CMOS camera modules such as 8-bit, 10-bit, and 12-bit [4]. It supports the original image data organized by line and frame format, such as YUV and RGB, and also supports receiving JPEG format compressed data stream. The block diagram of the interface is shown in Figure 1.Figure 1
Schematic diagram of the RMII interface.


### 2.2. Design of the Main Circuit

Designed around the microprocessor STM32, the main circuit is composed of a regulated power supply module, a microprocessor, an OV series camera, and a patch RS232 transceiver.The primary power source for peripherals is DC24V. Capacitors, inductors, and rectifier diodes are used for filtering, anti-interference, and anti-reverse connection processing once the input power is connected. SMD rectifier diode 214AC(SMA)-IN4007, SMD differential mode filter inductor HC0850-56uH, SMD TVS tube 214-AA-BI-P6KE56CA, and so on make up the configuration components. Because the CPU and certain chips demand a 5 V or 3.3 V driving voltage, isolation voltage conversion is necessary. Finally, it is provided to the CPU and other circuitry following additional voltage control by the LDO linear power supply module [5-6]. Figure 2 illustrates the power circuit.Figure 2
Schematic diagram of the power supply circuit.In order to meet the needs of the system for cigarette two-dimensional code image acquisition, decoding communication, and synchronisation signal acquisition, hardware is designed around the microprocessor STM32. Among them, the multichannel I/O pins, USART interface, USB interface, and RMII Ethernet communication interface are led out to the docking circuit board port [7-9]. The schematic diagram of the microprocessor circuit is shown in Figure 3.Figure 3
Schematic diagram of the main circuit of the microprocessor.The system acquires the QR code information which is in the form of pictures. The picture acquired by the OV series camera is loaded using a software process, and the image may be called using the scan() method. The function looks for the pixel point as a variable, filters it, applies an edge gradient, verifies the edge, and transforms it to a width stream. Finally, the decoding algorithm extracts the QR code information, which is then sent to the storage system or display tool through RS232 connection [10-11]. Figure 4 shows the schematic design of the RS232 communication interface circuit.Figure 4
The schematic diagram of the RS232 communication interface circuit.


## 2.1. Microprocessor Selection

The core part of hardware is the microprocessor, which adopts ST's STM32F407 ARM chip. Its on-chip DCMI module supports the use of VGA-like timing to obtain image data streams and can accept external multibit data streams, such as data sent by CMOS camera modules such as 8-bit, 10-bit, and 12-bit [4]. It supports the original image data organized by line and frame format, such as YUV and RGB, and also supports receiving JPEG format compressed data stream. The block diagram of the interface is shown in Figure 1.Figure 1
Schematic diagram of the RMII interface.


## 2.2. Design of the Main Circuit

Designed around the microprocessor STM32, the main circuit is composed of a regulated power supply module, a microprocessor, an OV series camera, and a patch RS232 transceiver.The primary power source for peripherals is DC24V. Capacitors, inductors, and rectifier diodes are used for filtering, anti-interference, and anti-reverse connection processing once the input power is connected. SMD rectifier diode 214AC(SMA)-IN4007, SMD differential mode filter inductor HC0850-56uH, SMD TVS tube 214-AA-BI-P6KE56CA, and so on make up the configuration components. Because the CPU and certain chips demand a 5 V or 3.3 V driving voltage, isolation voltage conversion is necessary. Finally, it is provided to the CPU and other circuitry following additional voltage control by the LDO linear power supply module [5-6]. Figure 2 illustrates the power circuit.Figure 2
Schematic diagram of the power supply circuit.In order to meet the needs of the system for cigarette two-dimensional code image acquisition, decoding communication, and synchronisation signal acquisition, hardware is designed around the microprocessor STM32. Among them, the multichannel I/O pins, USART interface, USB interface, and RMII Ethernet communication interface are led out to the docking circuit board port [7-9]. The schematic diagram of the microprocessor circuit is shown in Figure 3.Figure 3
Schematic diagram of the main circuit of the microprocessor.The system acquires the QR code information which is in the form of pictures. The picture acquired by the OV series camera is loaded using a software process, and the image may be called using the scan() method. The function looks for the pixel point as a variable, filters it, applies an edge gradient, verifies the edge, and transforms it to a width stream. Finally, the decoding algorithm extracts the QR code information, which is then sent to the storage system or display tool through RS232 connection [10-11]. Figure 4 shows the schematic design of the RS232 communication interface circuit.Figure 4
The schematic diagram of the RS232 communication interface circuit.


## 3. Design of Software

### 3.1. The Establishment of Keil Software Engineering

This design programming software adopts American Keil software. Software is powerful, well-structured, and highly readable and is suitable for programming in multiple languages. This design applies to C language programming. Before programming, an engineering project needs to be established, and MDK engineering settings are performed, and the Zbar decoding algorithm engineering module is used [12].Before the project is established, the Keil software system is set. First, find the microprocessor model in the Device option and select the STM32F407 chip. The detailed configuration of the chip appears on the right for reference. Second, select the Output option, set the storage path of the compilation result, and name the file. The most important step is to check the C99 Mode [13-15] in the C/C++ option because the Zbar code source is developed based on C99. The checked C99Mode is shown in Figure 5.Figure 5
C99Mode check option diagram.In the project directory, the C language programming text is named and added to the project.


### 3.2. Zbar Algorithm

Zbar is an open source library for scanning and reading QR codes and barcodes such as I25, CODE39, CODE128, and QR codes. The cigarette stick’s two-dimensional code is a QR code, and the code’s information size is small (approximately 1732 bytes). This method may be used for decoding at a high rate. It is compiled in C language, with easy programming and debugging [16-18], and is based on the STM32 development environment in design.(1)
The picture of the QR code is captured with the OV series camera and converted to grayscale. The scanning process of the Zbar image scanner is comparable to the zigzag form, and it scans line by line. The code is as follows:IplImage∗img = cvLoadImage(“C:\\ tobacco 1.jpg”)IplImage∗imgGray = cvCreateImage(cvGetSize(img), 8, 1)cvCvtColor(img, imgGray, CV_RGB2GRAY)intn = scanner.scan(image);(2)
Zbar decoding needs to preview the coordinates of the upper left vertex of the QR code image to obtain the following parameters:Height: height of the QR code imageWidth: width of the QR code imagePheight: height of the preview image, which is the resolution height of the cameraPwidth: width of the preview image, that is, the resolution width of the camerast: upper left vertex coordinatesSheight: height of scan frameSwidth: width of scan frameCheight: height of the preview controlCwidth: width of the preview controlSubstitute the above parameters into the formula:ptx/pwidth = stx/cwidthpty/pheight = sty/cheightwidth/pwidth = swidth/cwidthheight/pheight = sheight/cheightThe above four parameters of ptx, pty, width, and height are ZBar decoding to facilitate the calculation of the intercepted area. The flexibility of this scan is enhanced.(3)
Through the different detection standards of one-dimensional code and two-dimensional code, the two-dimensional code standard corresponding to the scanned image can be found. The edge gradient can be obtained by scanning each point to form an edge, and the front and rear edge values can be subtracted (width = scn- > edge-scn) and converted into a width stream, which can be the most scanning feature, and the standard corresponding to one-dimensional code or two-dimensional code can be interpreted [19]. The code is shown as below:const zbar_symbol_t ∗symbol = zbar_image_first_symbol(image)for(; symbol; symbol = zbar_symbol_next(symbol)){zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);const char ∗data = zbar_symbol_get_data(symbol); rt_kprintf(“decoded %s symbol “%s”\n”,zbar_get_symbol_name(typ), data);}(4)
The scanned QR code picture complies with the QR code standard and may be entered into the parser module's entry function. The coordinate parameters are determined using the second point, the three centre points are identified and rectified into a square, and the picture is binarized using the third point. The decoded codeword is read, the QR code version information is retrieved and decoded, and the decoded result is shown in hexadecimal format.USART asynchronous communication, GPIO setup, DCMI, high-frequency noise removal, and other protocol modules are among the software application modules. Due to space constraints, no detailed introduction is provided [20-21].


### 3.3. Implementation of the Decoding Function

The main uses of QR codes for cigarette packaging are divided into anti-counterfeiting, traceability, and marketing strategies. This design is mainly applied to a new field: Internet + data collection. After parsing the QR code of the cigarette stick, the QR code data of the cigarette pack, cigarette stick, and cigarette case are bundled and correlated. The functional terminal analyzes the relevant data according to the obtained industrial production end, commercial sales end, and consumption data. Each cigarette company shares the big data analysis results, applies it to its own business expansion, and formulates solutions. Figure6 introduces the practical application case of this design in the QR code decoding of cigarette sticks:Figure 6
QR code decoding flowchart.In the first step, the overall construction of the system hardware platform is completed, and the program after Keil compilation is downloaded to the STM32F407 platform. The OV series camera then switches to camera mode and changes the lens, focal length, and object distance based on the actual location of the cigarette rods on the cigarette system’s sorting line, in order to obtain the greatest shooting effect of the QR code picture [22].The camera's trigger synchronisation signal, which is given by the FS-V21 sensor, is adjusted in the third stage. The synchronisation signal is inserted into the STM32F407 platform and delivered to the camera signal in order to permit real-time shooting of the cigarette rod during the live movement. In the fourth step, after the image is acquired, Zbar decodes successfully, finally uploads it to the terminal through RS232 serial communication, and displays the decoded information on the interface, as shown in Figure7; the QR code image is shown in Figure 8.Figure 7
QR code decoding result display.Figure 8
QR code.Finally, according to the cigarette-bar QR code information storage protocol provided by the cigarette system, the data structure of cigarette-bar information collection is customized and stored in the local terminal of the terminal [23–25]. The data structure of the storage protocol is as follows:{“data”: [{“orderNo”: “20190816402977″,“clientNo”: “1037020102070XXX”,“clientName”: “ XX retailer in Laoshan District, Qingdao “,/∗ seller ∗/“etlDate”: “ “,“sortDate”: “ “,“sortItems”: [{“prodNo”: “196920886380″,“prodName”: “ Zhen Long Ling Yun “,/∗ cigarette brand ∗/“quantity”: “1″,“batchNo”: “ “,“sortSeq”: “ “,“lineNo”: “ “,“lineName”: “ “,“sortLineNo”: “ “.}],“qrcodes”: [“18206403995247616”/∗ QR code information ∗/]}],“createTime”: “20190903123212”.}


## 3.1. The Establishment of Keil Software Engineering

This design programming software adopts American Keil software. Software is powerful, well-structured, and highly readable and is suitable for programming in multiple languages. This design applies to C language programming. Before programming, an engineering project needs to be established, and MDK engineering settings are performed, and the Zbar decoding algorithm engineering module is used [12].Before the project is established, the Keil software system is set. First, find the microprocessor model in the Device option and select the STM32F407 chip. The detailed configuration of the chip appears on the right for reference. Second, select the Output option, set the storage path of the compilation result, and name the file. The most important step is to check the C99 Mode [13-15] in the C/C++ option because the Zbar code source is developed based on C99. The checked C99Mode is shown in Figure 5.Figure 5
C99Mode check option diagram.In the project directory, the C language programming text is named and added to the project.


## 3.2. Zbar Algorithm

Zbar is an open source library for scanning and reading QR codes and barcodes such as I25, CODE39, CODE128, and QR codes. The cigarette stick’s two-dimensional code is a QR code, and the code’s information size is small (approximately 1732 bytes). This method may be used for decoding at a high rate. It is compiled in C language, with easy programming and debugging [16-18], and is based on the STM32 development environment in design.(1)
The picture of the QR code is captured with the OV series camera and converted to grayscale. The scanning process of the Zbar image scanner is comparable to the zigzag form, and it scans line by line. The code is as follows:IplImage∗img = cvLoadImage(“C:\\ tobacco 1.jpg”)IplImage∗imgGray = cvCreateImage(cvGetSize(img), 8, 1)cvCvtColor(img, imgGray, CV_RGB2GRAY)intn = scanner.scan(image);(2)
Zbar decoding needs to preview the coordinates of the upper left vertex of the QR code image to obtain the following parameters:Height: height of the QR code imageWidth: width of the QR code imagePheight: height of the preview image, which is the resolution height of the cameraPwidth: width of the preview image, that is, the resolution width of the camerast: upper left vertex coordinatesSheight: height of scan frameSwidth: width of scan frameCheight: height of the preview controlCwidth: width of the preview controlSubstitute the above parameters into the formula:ptx/pwidth = stx/cwidthpty/pheight = sty/cheightwidth/pwidth = swidth/cwidthheight/pheight = sheight/cheightThe above four parameters of ptx, pty, width, and height are ZBar decoding to facilitate the calculation of the intercepted area. The flexibility of this scan is enhanced.(3)
Through the different detection standards of one-dimensional code and two-dimensional code, the two-dimensional code standard corresponding to the scanned image can be found. The edge gradient can be obtained by scanning each point to form an edge, and the front and rear edge values can be subtracted (width = scn- > edge-scn) and converted into a width stream, which can be the most scanning feature, and the standard corresponding to one-dimensional code or two-dimensional code can be interpreted [19]. The code is shown as below:const zbar_symbol_t ∗symbol = zbar_image_first_symbol(image)for(; symbol; symbol = zbar_symbol_next(symbol)){zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);const char ∗data = zbar_symbol_get_data(symbol); rt_kprintf(“decoded %s symbol “%s”\n”,zbar_get_symbol_name(typ), data);}(4)
The scanned QR code picture complies with the QR code standard and may be entered into the parser module's entry function. The coordinate parameters are determined using the second point, the three centre points are identified and rectified into a square, and the picture is binarized using the third point. The decoded codeword is read, the QR code version information is retrieved and decoded, and the decoded result is shown in hexadecimal format.USART asynchronous communication, GPIO setup, DCMI, high-frequency noise removal, and other protocol modules are among the software application modules. Due to space constraints, no detailed introduction is provided [20-21].


## 3.3. Implementation of the Decoding Function

The main uses of QR codes for cigarette packaging are divided into anti-counterfeiting, traceability, and marketing strategies. This design is mainly applied to a new field: Internet + data collection. After parsing the QR code of the cigarette stick, the QR code data of the cigarette pack, cigarette stick, and cigarette case are bundled and correlated. The functional terminal analyzes the relevant data according to the obtained industrial production end, commercial sales end, and consumption data. Each cigarette company shares the big data analysis results, applies it to its own business expansion, and formulates solutions. Figure6 introduces the practical application case of this design in the QR code decoding of cigarette sticks:Figure 6
QR code decoding flowchart.In the first step, the overall construction of the system hardware platform is completed, and the program after Keil compilation is downloaded to the STM32F407 platform. The OV series camera then switches to camera mode and changes the lens, focal length, and object distance based on the actual location of the cigarette rods on the cigarette system’s sorting line, in order to obtain the greatest shooting effect of the QR code picture [22].The camera's trigger synchronisation signal, which is given by the FS-V21 sensor, is adjusted in the third stage. The synchronisation signal is inserted into the STM32F407 platform and delivered to the camera signal in order to permit real-time shooting of the cigarette rod during the live movement. In the fourth step, after the image is acquired, Zbar decodes successfully, finally uploads it to the terminal through RS232 serial communication, and displays the decoded information on the interface, as shown in Figure7; the QR code image is shown in Figure 8.Figure 7
QR code decoding result display.Figure 8
QR code.Finally, according to the cigarette-bar QR code information storage protocol provided by the cigarette system, the data structure of cigarette-bar information collection is customized and stored in the local terminal of the terminal [23–25]. The data structure of the storage protocol is as follows:{“data”: [{“orderNo”: “20190816402977″,“clientNo”: “1037020102070XXX”,“clientName”: “ XX retailer in Laoshan District, Qingdao “,/∗ seller ∗/“etlDate”: “ “,“sortDate”: “ “,“sortItems”: [{“prodNo”: “196920886380″,“prodName”: “ Zhen Long Ling Yun “,/∗ cigarette brand ∗/“quantity”: “1″,“batchNo”: “ “,“sortSeq”: “ “,“lineNo”: “ “,“lineName”: “ “,“sortLineNo”: “ “.}],“qrcodes”: [“18206403995247616”/∗ QR code information ∗/]}],“createTime”: “20190903123212”.}


## 4. Conclusion

This study takes the decoding of the two-dimensional code printed on the cigarette stick as the main research object. For this purpose, a hardware platform with the microprocessor STM32F407 as the core is built, and C language is used for software programming. Relying on the production equipment of the cigarette system, the coding characteristics of the QR code and the conditions required for decoding are analyzed, and an OV series camera module to obtain pictures is built. The QR code information is collected in the hardware and software modules of the corresponding QR code decoding, and the feasibility of the entire system is tested. Finally, the identification and storage of QR code information is completed.Later, research planning can focus on improving the decoding success rate, which requires further research on hardware and software for the application environment. The research results can be applied to other production links of cigarette systems such as cigarette packs and cigarette boxes to obtain corresponding QR code information, expand information solutions in the cigarette industry, and realize intelligent management of supply chain, production, sales, and logistics distribution.



---

*Source: 3131302-2022-05-27.xml*