Welcome to NUWA-HP60C Depth Camera repository
2. QR code creation and recognition
2. QR code creation and recognition
2.1. QR QR code
2.1.1. QR QR code introduction
QR code is a type of two-dimensional barcode. QR comes from the abbreviation of "Quick Response" in English, which means quick response. It comes from the inventor's hope that QR code can allow its content to be decoded quickly. QR code not only has large information capacity, high reliability and low cost, but also can represent a variety of text information such as Chinese characters and images. It has strong confidentiality and anti-counterfeiting properties and is very convenient to use. More importantly, the QR code technology is open source.
2.1.2. QR QR code structure

2.1.3. QR QR code characteristics
The data value in the QR code contains repeated information (redundant value). Therefore, even if up to 30% of the QR code structure is destroyed, it does not affect the readability of the QR code. The storage space of the QR code is up to 7089 bits or 4296 characters, including punctuation and special characters, which can be written into the QR code. In addition to numbers and characters, words and phrases (such as URLs) can also be encoded. As more data is added to the QR code, the code size increases and the code structure becomes more complex.
2.1.4, QR code creation and recognition
1), create QR code
- Source code path
~/ascam_ros2_ws/src/yahboomcar_visual/simple_qrcode/QRcode_Create.py- Installation package
xxxxxxxxxxpython3 -m pip install qrcode pyzbarsudo apt-get install libzbar-dev- Start
xxxxxxxxxxcd ~/ascam_ros2_ws/src/yahboomcar_visual/simple_qrcodepython QRcode_Create.pyAfter the program runs, it will prompt you to enter the generated content, and press Enter to confirm the content. Here we take the creation of the "yahboom" string as an example,

- Core source code analysis
xxxxxxxxxx#Create qrcode objectqr = qrcode.QRCode(version=1,error_correction=qrcode.constants.ERROR_CORRECT_H,box_size=5,border=4,)#Meaning of each parameter'''version: An integer with a value of 1 to 40, which controls the size of the QR code (the minimum value is 1, which is a 12×12 matrix).If you want the program to determine it automatically, set the value to None and use the fit parameter.error_correction: Controls the error correction function of the QR code. The following 4 constants can be used.ERROR_CORRECT_L: About 7% or less errors can be corrected.ERROR_CORRECT_M (default): About 15% or less errors can be corrected.ERROR_CORRECT_H: About 30% or less errors can be corrected.box_size: Controls the number of pixels contained in each small grid in the QR code.border: controls the number of grids contained in the border (the distance between the QR code and the image border) (the default is 4, which is the minimum value specified by the relevant standards)'''#qrcode QR code adds logomy_file = Path(logo_path)if my_file.is_file(): img = add_logo(img, logo_path)#Add dataqr.add_data(data)# Fill data# fill dataqr.make(fit=True)# Generate images# generate imagesimg = qr.make_image(fill_color="green", back_color="white")2) Identify QR code
- Source code path
xxxxxxxxxx~/ascam_ros2_ws/src/yahboomcar_visual/yahboomcar_visual/QRcode_Parsing.py- Start
xxxxxxxxxx#Camera startupros2 launch ascamera hp60c.launch.pyros2 run yahboomcar_visual QRcode_Parsing
- View node communication graph
xxxxxxxxxxros2 run rqt_graph rqt_graph
- Core source code analysis
xxxxxxxxxxself.sub_img = self.create_subscription(CamImage,'/ascamera_hp60c/camera_publisher/rgb0/image',self.handleTopic,100)frame = self.decodeDisplay(frame)def decodeDisplay(self,image):gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)# 需要先把输出的中文字符转换成Unicode编码形式# The output Chinese characters need to be converted to Unicode encoding firstbarcodes = pyzbar.decode(gray)for barcode in barcodes:# 提取二维码的边界框的位置# Extract the position of the boundary box of the TWO-DIMENSIONAL code# 画出图像中条形码的边界框# Draw the bounding box for the bar code in the image(x, y, w, h) = barcode.rectcv.rectangle(image, (x, y), (x + w, y + h), (225, 0, 0), 5)encoding = 'UTF-8'# 画出来,就需要先将它转换成字符串# to draw it, you need to convert it to a stringbarcodeData = barcode.data.decode(encoding)barcodeType = barcode.type# 绘出图像上数据和类型# Draw the data and type on the imagepilimg = Image.fromarray(image)# 创建画笔# create brushdraw = ImageDraw.Draw(pilimg) # 图片上打印 Print on picture# 参数1:字体文件路径,参数2:字体大小# parameter 1: font file path, parameter 2: font sizefontStyle = ImageFont.truetype("/home/yahboom/ascam_ros2_ws/src/yahboomcar_visual/yahboomcar_visual/Block_Simplified.TTF", size=12, encoding=encoding)# # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体# Parameter 1: print coordinates, parameter 2: text, parameter 3: font color, parameter 4: fontdraw.text((x, y - 25), str(barcode.data, encoding), fill=(255, 0, 0), font=fontStyle)# # PIL图片转cv2 图片# PIL picture to CV2 pictureimage = np.array(pilimg)# 向终端打印条形码数据和条形码类型# Print barcode data and barcode type to terminalprint("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))return image