In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.