Java SDK supports Chinese under Linux

xiaoxiao2021-03-06  106

Reason

JDK needs to read the font list from the font configuration file, and the default font list does not have a Chinese font.

2. Solution

Add Chinese fonts for JDK font profiles

3. Procedure

3.1. Copy the font file to the font directory of JDK

CD / USR / local / java / jre / lib / fontscp /usr/share/fonts/en_cn/TrueType/gbsn00l.ttf.

3.2. Generate a font list file

TTMKFDIR -O FONTS.DIR

3.3. View the newly generated fonts.dir file to view the part about GBSN00L.TTF

VI FONTS.DIR

3.4. Should be on the last line of the file

-ARPHIC TECHNOLOGY CO.-AR PL SUNGTIL GB-Medium-R-NORMAL - 0-0-0-0-P-0-ASCII-0

3. Return to the previous point of directory, join GBSN00LP.TTF to Font.properties

Cd ..vi font.properties

3.6. Add method:

3.6.1 First copy the line of text seen in 3.4, will

0-0-0-0-P-0 change to * -% D - * - * - P- *

The purpose is to make the font to scale, now it is:

-ARPHIC TECHNOLOGY Co.-AR PL SUNGTIL GB-Medium-R-NORMAL - * -% D - * - * - P - * - ASCII-0

3.6.2 Font.Properties files have been included in 0 and 1 font, such as the first line is Serif.0, the second line is SeriF.1, then you need to add

Serif.2 = -ARPHIC TECHNOLOGY Co. - AR PL SUNGTIL GB-Medium-R-NORMAL - * -% D - * - * - P - * - ASCII-0

3.6.3 Pushing in this class, adding XXXX.2 below each XXXX.2 until the # missing glyph character before the file.

3.6.4 Add completion, save the disk exits.

3.7. Add environment variable Java_FONTS

Vi / etc / profilejava_fonts = $ java_home / jre / lib / fonts

3.8. Replace to make environment variables

4. Test

Testchsgraphic.java (JDK1.3)

Import java.awt. *;

Import java.awt.image. *;

Import java.io. *;

Import java.util. *;

Import com.sun.Image.codec.jpeg. *;

Public Class Testchsgraphic

{

Public static void main (string [] args) Throws Exception

{

System.SetProperty ("java.awt.headless", "true");

BufferedImage Bi = New BufferedImage (200, 100, BufferedImage.Type_INT_RGB);

Graphics g = bi.getgraphics ();

String s = "Chinese test";

// pay attention! Specific transcodes may vary!

g.drawstring (new string (s.getbytes (), "GB2312"), 50, 50);

bi.flush ();

FileOutputStream Fos = New FileoutputStream ("Test.jpg");

JPEGIMAGEENCODER JPEGIE = ​​JPEGCODEC.CREATEJPEGENCODER (FOS); JPEGIE.ENCODE (BI);

Fos.close ();

}

}

Testchsgraphic.java (JDK1.4)

Import java.awt.graphics;

Import java.awt.bufferedImage;

Import javax.imageio.Imageio; import java.io. *;

Public Class Testchsgraphic

{

Public static void main (string [] args) Throws Exception

{

System.SetProperty ("java.awt.headless", "true");

BufferedImage Bi = New BufferedImage (200, 100, BufferedImage.Type_INT_RGB);

Graphics g = bi.getgraphics ();

String s = "Chinese test";

// pay attention! Specific transcodes may vary!

g.drawstring (new string (s.getbytes (), "GB2312"), 50, 50);

Imageio.write (BI, "JPEG", New File ("Test.jpg"));

}

}

转载请注明原文地址:https://www.9cbs.com/read-103118.html

New Post(0)