Solve graphics2d. DrawString (message, px, py) garbled problem __ garbled problem

Source: Internet
Author: User

Yesterday began to find solutions to print pictures appear in Chinese garbled problem, today finally finished, write a rough pass, for people who have the same problem.

Problem Description:

The user uses the mobile phone to log in our system, when ordering the product, needs to download a picture from the server side, displays to the handset screen, this picture may contain the Chinese and English characters, depending on the situation. Results test down, English is no problem, Chinese print as a box.

Environment Description:

Development platform for WIN7, test environment for Linux.

Data Source:

The information used to print to the picture is obtained from another system via the Web Serice.

=====

Let's talk about the solution,

1, whether the character coding problem

To check for the correct encoding of the obtained string, try a variety of character set encoding conversions in the following way, but there is no effect;

    String message= "...";//ws gets the information  
    new String (Message.getbytes ("UTF8"), "GBK");  


Come back and think about our system, the system JSP page and database coding are used in the Utf-8 code, whether the input, save or display did not appear in Chinese garbled problem; In particular, the system with a number of WS-Mode synchronization data, there is no garbled problem, The information that WS obtains is also UTF-8 encoded. To verify this idea, do a JSP page, use the Utf-8 way to display the information obtained, without any conversion of the premise, the correct display of Chinese. Ultimately, it is not a problem with character set encoding.

2, whether the printing process has been a problem

Looking at the JDK API instructions, I found that when using fonts, I did not specifically set, using the default font. Write a simple output platform default font class, the resulting font is the same information:

    Default Font:java.awt.font[family=dialog,name=dialog,style=plain,size=12]   


So go to the internet to find "Java drawstring Chinese garbled" keyword information, find CSDN a message: http://topic.csdn.net/u/20080603/13/ 3590c1f9-30bc-4e4d-bb2a-ada472b05973.html. Everyone can go and see, I will not post it. My action is:

1 specifies the font to be printed to the picture as Chinese

 
G2d.setfont (New Font ("Song Body", Font.plain, 12));   


2 upload Win7 italicized Word file to server (Linux)

My local font file address is "C:\WINDOWS\FONTS\SIMSUN.TTC", upload to the server address is: "/USR/JDK/INSTANCES/JDK1.5.0/JRE/LIB/FONTS/SIMSUN.TTC"

3 through the verification program, make sure that the small box on the picture correctly displayed as Chinese

Print a picture of Chinese garbled problem here is solved.

Considering, because the print character becomes the picture, needs to use the pixel arrangement, if the font is not correct, does not have the way to print AH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.