On CentOs server, Java uses Graphics2D to compose text into the image and display small boxes.

 / / background
 File bgImageFile = new File(bgImageName);
	BufferedImage big = ImageIO.read(bgImageFile);
	// The composite image
	 Graphics2D graphics = big.createGraphics();
	 // Set the font to black
	 graphics.setPaint(Color.BLACK);
	 // Brush thickness
	 graphics.setStroke(new BasicStroke(1f));
	 // Remove text aliasing
	 graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	 // Remove image aliasing
	 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	 // Create a font
    Font font = new Font("宋体", Font.PLAIN, 38);
    // Set the font
    graphics.setFont(font);
    // Indicates the position of the text on the image (x,y). The first one is the content you set.
    graphics.drawString(userName, widthX / 2.820);
Copy the code

Effect:Display box is no font library, not coding problem, display?? Centos does not have a font library

Step 1 Install the font library

Yum install Fontconfig install fastestMirror... Running Transaction installing: libfontenc- 1.1.3- 3.el7.x86_64                                                                                                       1/2Installing:1:xorg-x11-font-utils7 -.5- 21.el7.x86_64                                                                                             2/2Verifying: libfontenc- 1.1.3- 3.el7.x86_64                                                                                                       1/2In the validation:1:xorg-x11-font-utils7 -.5- 21.el7.x86_64                                                                                             2/2Installed: xorg-x11-font-utils.x86_64 1:7.5- 21.el7 is installed as a dependency: libfontenc.x86_640:1.1.3- 3El7 finished!Copy the code

Step 2 Install the update font command

yum install mkfontscale

[root@service-1 ~]# yum install mkfontscaleHostfile is resolving dependencies --> checking transactions --> Package xorg-x11-font-utils.x86_64.1.7.5- 21.el7 will be installed --> handling dependencies libfontenc.so.1() (64Bit), it is packaged1:xorg-x11-font-utils7 -.5- 21.el7.x86_64 Requires --> checking transactions --> software package libfontenc.x86_64.0.1.1.3- 3.el7 will be installed --> Resolve dependencies to complete dependency resolution = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = version Package structure size of the source = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ================================ Installing: xorg-x11-font-utils                          x86_64                          1:7.5- 21.el7                          base                          104K installed for dependency: libfontenc x86_641.1.3- 3.el7                           base                           31K Transaction Overview = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = installation1The software package (+1Total downloads:135K Installation size:408 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): xorg-x11-font-utils7 -.5- 21.el7.x86_64.rpm                                                                                 | 104 kB  00:00:00     
(2/2): libfontenc- 1.1.3- 3.el7.x86_64.rpm                                                                                         |  31 kB  00:00:00------------------------------------------------------------------------------------------------------------------------ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- a total1.5 MB/s | 135 kB  00:00:00Running Transaction Check Running Transaction test Transaction test Succeeded Running Transaction Installing: libfontenc- 1.1.3- 3.el7.x86_64                                                                                                       1/2Installing:1:xorg-x11-font-utils7 -.5- 21.el7.x86_64                                                                                             2/2Verifying: libfontenc- 1.1.3- 3.el7.x86_64                                                                                                       1/2In the validation:1:xorg-x11-font-utils7 -.5- 21.el7.x86_64                                                                                             2/2Installed: xorg-x11-font-utils.x86_64 1:7.5- 21.el7 is installed as a dependency: libfontenc.x86_640:1.1.3- 3El7 finished!Copy the code

Step 3 Check the Chinese fonts

fc-list :lang=zh
Copy the code

It is found that there is no Chinese font library, so we need to upload the directory to create the text body library

mkdir /usr/share/fonts/chinese
cd /usr/share/fonts/chinese
Copy the code

Set access permissions for this directory

chmod -R 755 /usr/share/fonts/chinese
Copy the code

Click here to view the Chinese font library

Step 4 Associate the uploaded font library

cd /usr/share/fonts/chinese
mkfontscale
mkfontdir
fc-cache

Copy the code

Check whether the installation is successful

[root@service-1 chinese]# fc-list :lang=zh/ usr/share/fonts/Chinese/SIMSUN. TTC: song typeface, SIMSUN: style = routine, Regular/usr/share/fonts/Chinese/DENGL. The vera.ttf: DengXian,DengXian Light:style=Light,Regular /usr/share/fonts/chinese/DENG.TTF: DengXian:style=Regular /usr/share/fonts/chinese/DENGB.TTF: DengXian:style=Bold /usr/share/fonts/chinese/SIMSUN.TTC: New song typeface, NSimSun: style = routine, Regular/usr/share/fonts/Chinese/SIMKAI. The vera.ttf: KaiTi:style=Regular,Normaali /usr/share/fonts/chinese/SIMHEI.TTF: SimHei:style=Normal /usr/share/fonts/chinese/SIMFANG.TTF: FangSong:style=Regular,NormaaliCopy the code

The perfect solution