Call opencv library programming to display pictures and student ID names under Ubuntu

Article directory

  • 1. Principle of Chinese character dot matrix font library
    • (1), Chinese character encoding
      • 1. Location code
      • 2. Internal code
    • (2) Dot matrix font structure
      • 1. Bitmap font storage
      • 2. 16*16 dot matrix font library
      • 3. 14*14 and 12*12 dot matrix fonts
    • (3). Acquisition of Chinese character lattice
      • 1. Use location code to obtain Chinese characters
      • 2. Use the Chinese character internal code to obtain Chinese characters
  • 2. Call opencv library programming to display pictures and student ID names under Ubuntu
    • 1. Open a new folder in ubuntu to store code, pictures, 24-dot matrix .hz files, and ASCII code .zf files.
    • 2. Paste the picture, 24-dot matrix .hz file, and ASCII code .zf file into the path
    • 3. Create a .cpp file
    • 4.Compile
    • 5. Run
  • Summarize
  • refer to

1. Principle of Chinese character dot matrix font library

(1), Chinese character encoding

1. Location code

It is stipulated in the national standard GD2312-80 that all national standard Chinese characters and symbols are allocated in a square matrix with 94 rows and 94 columns. Each row of the square matrix is called a “zone”, numbered from zone 01 to zone 94, and each column is called a “zone”. It is a “bit”, numbered from 01 to 94. The four Arabic numerals formed by the area code and bit number of each Chinese character and symbol in the square matrix are their “area code”. The first two digits of the area code are its area code, and the last two digits are its location number. A Chinese character or symbol can be uniquely determined using the location code. Conversely, any Chinese character or symbol also corresponds to a unique location code. The location code of the Chinese character “mother” is 3624, which indicates that it is in the 24th position of area 36 of the square matrix. The location code of the question mark “?” is 0331, which means it is in the 3l position of area 03.

2. Internal code

The in-machine code of Chinese characters refers to the encoding that represents a Chinese character in the computer. There is a slight difference between the machine code and the location code. As mentioned above, the area code and bit code of Chinese character area code have values between 1 and 94. If the area code is directly used as the internal code, it will be confused with the basic ASCII code. In order to avoid the conflict between the internal code and the basic ASCII code, the control code (00H~1FH) in the basic ASCII code needs to be avoided, and it needs to be distinguished from the characters in the basic ASCII code. In order to achieve these two points, you can first add 20H to the area code and bit code respectively, and then add 80H (here “H” means that the first two digits are hexadecimal numbers). After these processes, it takes two bytes to represent a Chinese character using the internal code, which are called high-order byte and low-order byte respectively. The two-byte internal code is expressed according to the following rules:

High byte = area code + 20H + 80H (or area code + A0H)

Low byte = bit code + 20H + 80H (or bit code + AOH)

Since the hexadecimal numbers of the area code and bit code of Chinese characters are both 01H~5EH (that is, 01~94 in decimal), the value range of the high-order byte and low-order byte of Chinese characters is A1H~ FEH (i.e. 161~254 in decimal).
For example, the area code of the Chinese character “ah” is 1601. The area code and bit code are expressed in hexadecimal respectively as 1001H. The high-order byte of its internal code is B0H and the low-order byte is A1H. The internal code is B0A1H.

(2), dot matrix font structure

1. Bitmap font storage

In the dot matrix font library of Chinese characters, each bit of each byte represents a point of a Chinese character.
Characters are composed of a rectangular dot matrix. 0 represents nothing and 1 represents a bit. Draw 0 and 1 in different colors to form a Chinese character. Commonly used dot matrix matrices include 1212, 14< /em>14, 16*16 three fonts.
Font libraries are divided into horizontal matrices and vertical matrices according to the different points represented by bytes. Currently, most font libraries
They are all horizontal matrix storage methods (the most commonly used one should be the early UCDOS font). The vertical matrix is generally because some liquid crystals use the vertical scanning display method. In order to improve the display speed, the font matrix is made vertical to save time. Matrix conversion is also required during display. What we describe next refers to the horizontal matrix font.

2. 16*16 dot matrix font library

For a 1616 matrix, the total number of bits required is 1616 = 256 bits, each word
Sections are 8 bits, so each Chinese character requires 256/8=32 bytes to represent.
That is, every two bytes represent 16 points in a line, and a total of 16 lines are needed. When displaying Chinese characters, only one
Read 32 bytes simultaneously and print out every two bytes as one line to form a Chinese character.
The lattice structure is shown in the figure below:

3. 1414 and 1212 dot matrix fonts

For the 1414 and 1212 fonts, theoretical calculations indicate that the lattice required for them is (14
14/8)=25, (1212/8)=18 bytes, but if stored in this way, then
When dot matrix and display, since each of their rows is not an integer of 8, it will involve the calculation and processing of the dot matrix, which will increase the complexity of the program and reduce the efficiency of the program.
In order to solve this problem, some bitmap fonts will convert 1414 and 1212 fonts into 1614
and 16
12 are stored, that is, each line is still stored as two bytes, but for the 1414 font, each
The last two digits of the two bytes are not used. For 12
12 bytes, the last 4 bits of every two bytes are not used. This will be handled differently according to different fonts, so when using the font Please pay attention to this problem when using, especially the 14*14 font.

(3), Chinese character lattice acquisition

1. Use location code to obtain Chinese characters

The Chinese character dot matrix font library is stored according to the order of location codes. Therefore, we can
Get the lattice of a font, its calculation formula is as follows:

Dot matrix starting position = ((area code – 1)*94 + (bit code – 1)) * number of Chinese character dot matrix bytes

After obtaining the starting position of the dot matrix, we can start from this position and read out the dot matrix of a Chinese character.

2. Use the Chinese character internal code to obtain Chinese characters

We have already mentioned before that the relationship between the location code of Chinese characters and the internal code is as follows:

High byte of internal code = area code + 20H + 80H (or area code + A0H)

Internal code low byte = bit code + 20H + 80H (or bit code + AOH)

On the other hand, we can also obtain the location code based on the internal code:

Area code = high byte of internal code – A0H

Bit code = low byte of built-in code – AOH

By combining this formula with the formula for obtaining the Chinese character lattice, you can obtain the lattice position of the Chinese character.
I found this good program that can read and display Chinese character dot matrix fonts. If you want to make something like an e-book, it is of great reference value. Although the program is written in TC20, the C in the DOS era is very similar to that of a microcontroller. Yes, just understand the main formula part.

2. Call the opencv library to program to display pictures and student ID names under Ubuntu

1. Open a new folder in ubuntu to store code, pictures, 24-dot matrix .hz files, and ASCII code .zf files

Terminal input:

mkdir chinesetest
cdchinesetest

2. Paste the picture, 24-dot matrix .hz file, and ASCII code .zf file into the path


The text encoding format is ANSI format, and the default encoding format is utf-8, otherwise garbled characters will appear
Select File As Modified and save

3. Create a .cpp file

Use vim to edit and write code:

#include<iostream>

#include<opencv/cv.h>

#include"opencv2/opencv.hpp"

#include<opencv/cxcore.h>

#include<opencv/highgui.h>

#include<math.h>

using namespace cv;

using namespace std;

void paint_chinese(Mat & amp; image,int x_offset,int y_offset,unsigned long offset);

void paint_ascii(Mat & amp; image,int x_offset,int y_offset,unsigned long offset);

void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path);

int main(){<!-- -->

    String image_path="/home/seele/documentation/embedded/chinesetest/furina.png";

    char* logo_path=(char*)"/home/seele/documentation/embedded/chinesetest/logo.txt";

    put_text_to_image(20,300,image_path,logo_path);

    return 0;

}



void paint_ascii(Mat & amp; image,int x_offset,int y_offset,unsigned long offset){<!-- -->


Point p;

p.x = x_offset;

p.y = y_offset;



char buff[16];


FILE *ASCII;

if ((ASCII = fopen("/home/seele/鏂囨./哓屽叆叮?chinesetest/Asci0816.zf", "rb")) == NULL){<!-- -->

printf("Can't open ascii.zf,Please check the path!");

//getch();

exit(0);

}

fseek(ASCII, offset, SEEK_SET);

fread(buff, 16, 1, ASCII);

int i, j;

Point p1 = p;

for (i = 0; i<16; i + + )
{<!-- -->

p.x = x_offset;

for (j = 0; j < 8; j + + )
{<!-- -->

p1 = p;

if (buff[i] & amp; (0x80 >> j))

{<!-- -->

\t\t\t\t

circle(image, p1, 0, Scalar(0, 0, 255), -1);

p1.x + + ;

circle(image, p1, 0, Scalar(0, 0, 255), -1);

p1.y + + ;

circle(image, p1, 0, Scalar(0, 0, 255), -1);

p1.x--;

circle(image, p1, 0, Scalar(0, 0, 255), -1);

\t\t\t\t

}

p.x + =2;

}

p.y + =2;

}

}



void paint_chinese(Mat & amp; image,int x_offset,int y_offset,unsigned long offset){<!-- -->

    Point p;

    p.x=x_offset;

    p.y=y_offset;

    FILE *HZK;

    char buff[16];

    if((HZK=fopen("/home/seele/documentation/embedded/chinesetest/HZKf2424.hz","rb"))==NULL){<!-- -->

        printf("Can't open HZKf2424.hz,Please check the path!");

        exit(0);

    }

    fseek(HZK, offset, SEEK_SET);

    fread(buff, 16, 1, HZK);

    bool mat[24][24];

    int i,j,k;

    for (i = 0; i<16; i + + )

{<!-- -->

for (j = 0; j<3; j + + )

for (k = 0; k<8; k + + )

if (buff[i * 3 + j] & amp; (0x80 >> k))

{<!-- -->

mat[j * 8 + k][i] = true;

}

else {<!-- -->

mat[j * 8 + k][i] = false;

}

}

    for (i = 0; i < 24; i + + )

{<!-- -->

p.x = x_offset;

for (j = 0; j < 24; j + + )

{<!-- -->

if (mat[i][j])

circle(image, p, 1, Scalar(255, 0, 0), -1);

p.x + + ;

}

p.y + + ;

}

}



void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path){<!-- -->
    Mat image=imread(image_path);

    int length=18;

    unsigned char qh,wh;

    unsigned long offset;

    unsigned char hexcode[30];

    FILE* file_logo;

    if ((file_logo = fopen(logo_path, "rb")) == NULL){<!-- -->

printf("Can't open txtfile,Please check the path!");

//getch();

exit(0);

}

    fseek(file_logo, 0, SEEK_SET);

    fread(hexcode, length, 1, file_logo);

    int x =x_offset,y = y_offset;

    for(int m=0;m<length;){<!-- -->

        if(hexcode[m]==0x23){<!-- -->

            break;

        }

        else if(hexcode[m]>0xaf){<!-- -->

            qh=hexcode[m]-0xaf;

            wh=hexcode[m + 1] - 0xa0;

            offset=(94*(qh-1) + (wh-1))*72L;

            paint_chinese(image,x,y,offset);

            d4-a0=52

            */

            m=m + 2;

            x + =24;

        }

        else{<!-- -->

        wh=hexcode[m];

        offset=wh*16l;

        paint_ascii(image,x,y,offset);

        m + + ;

        x + =16;
 }



    }

    cv::imshow("image", image);

    cv::waitKey();

}

4. Compile

g + + test.cpp -o test `pkg-config --cflags --libs opencv`

5. Run

./test

Summary

The main content of this study includes understanding the internal code and location code encoding rules of Chinese characters, as well as the glyph data storage format. At the same time, I also learned how to use C/C++ to program the opencv library to display a picture and open a text file named “logo.txt” under Ubuntu. Through this study, we learned about Chinese character encoding and processing, as well as the application of the opencv library in image processing. This is of great significance for the study and application of computer science and related fields.

Reference

https://blog.csdn.net/weixin_56102526/article/details/121178128

syntaxbug.com © 2021 All Rights Reserved.