Get the App
SLTechnology News&Howtos  ›  Development  › 

The problem of Chinese garbled code in writing pictures in Graphics2D and its solution

Shulou Source: shulou.com Published: 2022-06-02 15:38:02 11月12日 Update

This article will explain in detail the problem and solutions of Chinese garbled codes for pictures written in Graphics2D. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

An overview of Chinese garbled codes in Graphics2D writing pictures

Due to the needs of the work, I needed to write to the picture, and there was garbled code when writing Chinese. I couldn't find a lot of information on the Internet. Later, I kept trying, and finally found the garbled code caused by the name setting problem of Font.

The cause of the problem & solution

The glyph class Font is used to standardize the glyph size, style, font, etc., used by components. Its constructor function:

Public Font (String name,int style,int size)

Name indicates that font names are available locally

Style represents the font style and contains three types of Font.PLAIN,Font.BOLD,Font.ITALIC, which correspond to flat, bold and italic. It can be seen that Font name cannot be set at will.

Check which fonts are supported by the server

GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment (); String [] fa=ge.getAvailableFontFamilyNames (); for (String s:fa) {System.out.println (s);}

If the desired font does not exist on the server, upload it to the server via ftp and copy it to the / usr/local/jdk/jre/lib/fonts/ directory (jdk installation directory), and then restart the java process by downloading it from the Internet or other system copies (such as the Song font file simsun.ttc simsun.ttf).

Test code

BufferedImage buffImg = ImageIO.read (new File ("/ tmp/1.jpg")); Graphics2D g = buffImg.createGraphics (); g.setColor (Color.BLACK); Font f = new Font ("Arial", Font.PLAIN, 30); g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setFont (f); FontMetrics fm = g.getFontMetrics (f) G.drawString ("China 31", 100,100); ImageIO.write (buffImg, FORMAT, new File ("/ tmp/test.jpg")); garbled when Graphics2D displays text

Graphics2D is garbled when displaying text. One solution is to add fonts to the server.

One is to convert Chinese characters into unicode.

The words like\ u6388\ u6743\ u7b7e\ u540d\ uff1a are stored in the database.

It is still displayed on the page that\ u6388\ u6743\ u7b7e\ u540d\ uff1a is obviously escaped by java.

String s = "\ u5ba2\ u6237\ u7b7e\ u5b57\ uff1a"; String str = s.replaceAll ("\", "")

Now just replace it with replalaceAll.

On the Graphics2D to write pictures in Chinese garbled problems and solutions to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Garbled font picture question Chinese server service text method content glyph Arial copy article more style directory knowledge article no Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information MariaDB Shulou Tech Info macOS