Java - does not show after drawString Chinese fonts on Linux environment picture (but English numbers can ...).

1. Problem:

      After the test no problem window environment, update the code to the linux server, found that generate images Chinese fonts do not display?

 

2. Analysis: (related code below)

      The system has a relationship with linux? Linux is finally found find no corresponding font type on the server caused because the picture is generated in the system under linux, the use java.awt.Font this class. Examples of when it will go to the jre in font, if not find the corresponding font, it will not show up.

// 在图片上写文字!
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
// 字体、字型、字号大小. 
g.setFont(new Font("仿宋", Font.PLAIN, fontSize));
// 指定位置画文字
g.drawString("啦啦", 100, 100);
g.dispose();

 

3. resolve as follows:

Premise: first find the font files in the local Windows:
Here Insert Picture Description

Solve a, add the appropriate font in the jre. (No test)

1. Locate the Java Runtime Environment installation directory (/ user / ... / jre / lib / fonts), put jre below.

2. Create a folder. (Mkdir winFonts)

3. Restart tomcat server. (Emphasis - not restart does not take effect. )
 

Solve Second, add the font files in the Linux system. (Effective pro-test)

1. In this directory (/ usr / share / fonts) Create a folder winFonts. (Folder name at random)

To the newly created folder 2. Copy the need to add font files (refer to 3.1).

3. Refresh font: fc-cache

4. View font: fc-list

5. Restart tomcat server. (Emphasis - not restart does not take effect. )
Here Insert Picture Description

common problem?

1. If fc-list did not see the added font, font this might be subject to copyright protection can not be added to it, it is recommended replaced free fonts.
2. Add fonts will not take effect immediately, you need to restart the server.

Published 107 original articles · won praise 173 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_42986107/article/details/103001305