4,生成点阵字库。这里我需要向大家推荐一款点阵字库生成软件:《特大点阵字库制作软件》。 其软件的使用截图如下
:
图上的dd.hzk文件就是我们生成的GB2312编码的汉字点阵字库。
5.将汉字点阵字库加载到程序中,代码如下:
public void initChineseFontByte() {
int ic;
InputStream in = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] buffer = new byte[1024];
try {
in = "".getClass().getResourceAsStream(ZK_PATH);
if (in != null) {
while ((ic = in.read(buffer)) > 0) {
dos.write(buffer, 0, ic);
}
chineseFontByte = baos.toByteArray();
in.close();
}
dos.close();
baos.close();
} catch (Exception e) {
System.out.println("getTextByUTF Error:" + e.toString());
} finally {
buffer = null;
in = null;
dos = null;
baos = null;
}
System.gc();
}
6,通过汉字的机内码查找在dd.hzk中找到相应的汉字字形码,并将其和汉字的机内码一起做数据持久化。
public byte[] getFontStream(String str) {
String subStr;
int[] fontCode = null;
byte[] fontShapeCode = null;
LinkedList<Integer> allFontCodingLk = new LinkedList<Integer>();
LinkedList<Byte> allFontShapeLk = new LinkedList<Byte>();
initChineseFontByte();
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
subStr = str.substring(i, i + 1);
fontCode = getByteCode(subStr);
if (fontCode.length < 2) {
System.out.println(ch);
} else {
fontShapeCode = read(fontCode[0], fontCode[1]);
if (fontShapeCode != null) {
allFontCodingLk.add((int) ch);
addByteToLinkedList(allFontShapeLk, fontShapeCode);
} else {
System.out.println(ch + " " + fontCode[0] + " "
+ fontCode[1]);
}
}
}
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(
outputStream);
dataOutputStream.writeByte(FontHeight);
dataOutputStream.writeByte(ChineseWidth);
int size = allFontCodingLk.size();
dataOutputStream.writeInt(size);
Integer[] allFondCode = new Integer[size];
allFontCodingLk.toArray(allFondCode);
for (int i = 0; i < size; i++) {
dataOutputStream.writeInt(allFondCode[i].intValue());
}
Byte[] allFontShapeCode = new Byte[allFontShapeLk.size()];
allFontShapeLk.toArray(allFontShapeCode);
for (int i = 0; i < allFontShapeCode.length; i++) {
dataOutputStream.writeByte(allFontShapeCode[i].byteValue());
}
return outputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
7.输出到相应的文件中
public void generatesFontFile(byte[] b) {
try {
File file = new File("D:/J2ME/ToolFont/myFont.bin");
FileOutputStream out = new FileOutputStream(file);
try {
BufferedOutputStream buf = new BufferedOutputStream(out);
if (b != null) {
buf.write(b, 0, b.length);
}
buf.flush();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
程序执行如图:
8.编写加载部分字库的函数
static void initFontByte() {
try {
InputStream inputStream = "".getClass()
.getResourceAsStream(ZK_PATH);
DataInputStream dataInputStream = new DataInputStream(inputStream);
fontHeight = dataInputStream.readByte();
chineseCharWidth = dataInputStream.readByte();
NUM_OF_CH_CHAR = dataInputStream.readInt();
chineseFontCode = new int[NUM_OF_CH_CHAR];
for (int i = 0; i < NUM_OF_CH_CHAR; i++) {
chineseFontCode[i] = dataInputStream.readInt();
}
int oneChMSize = fontHeight * (((chineseCharWidth - 1) >> 3) + 1);
int allSize = oneChMSize * NUM_OF_CH_CHAR;
chineseShapeCode = new byte[allSize];
dataInputStream.read(chineseShapeCode, 0, allSize);
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
10. 下面我需要最后添加一个函数,就是在对应的机内码集中将自己的汉字位置找出来。还记得上一节中,我们说的排序吗??? 这里在从机内码中将自己的汉字找出来,根据其排序采用二分查找,以此来加快程序的执行速度。根据机内码的位置,得到字形码的位置,将汉字绘制出来。
这个函数,我就不贴出来了,相信同仁是可以写出来的!!
谢谢大家的阅读!