当前位置:   article > 正文

J2me 游戏点阵字库引擎(六)之部分汉字库【续】

j2me游戏bin文件里面的图片

4,生成点阵字库。这里我需要向大家推荐一款点阵字库生成软件:《特大点阵字库制作软件》。 其软件的使用截图如下

图上的dd.hzk文件就是我们生成的GB2312编码的汉字点阵字库。

 

5.将汉字点阵字库加载到程序中,代码如下:

 

  1. public void initChineseFontByte() {
  2. int ic;
  3. InputStream in = null;
  4. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  5. DataOutputStream dos = new DataOutputStream(baos);
  6. byte[] buffer = new byte[1024];
  7. try {
  8. in = "".getClass().getResourceAsStream(ZK_PATH);
  9. if (in != null) {
  10. while ((ic = in.read(buffer)) > 0) {
  11. dos.write(buffer, 0, ic);
  12. }
  13. chineseFontByte = baos.toByteArray();
  14. in.close();
  15. }
  16. dos.close();
  17. baos.close();
  18. } catch (Exception e) {
  19. System.out.println("getTextByUTF Error:" + e.toString());
  20. } finally {
  21. buffer = null;
  22. in = null;
  23. dos = null;
  24. baos = null;
  25. }
  26. System.gc();
  27. }

 6,通过汉字的机内码查找在dd.hzk中找到相应的汉字字形码,并将其和汉字的机内码一起做数据持久化。

  1. public byte[] getFontStream(String str) {
  2. String subStr;
  3. int[] fontCode = null;
  4. byte[] fontShapeCode = null;
  5. LinkedList<Integer> allFontCodingLk = new LinkedList<Integer>();
  6. LinkedList<Byte> allFontShapeLk = new LinkedList<Byte>();
  7. initChineseFontByte();
  8. int len = str.length();
  9. for (int i = 0; i < len; i++) {
  10. char ch = str.charAt(i);
  11. subStr = str.substring(i, i + 1);
  12. fontCode = getByteCode(subStr);
  13. if (fontCode.length < 2) {
  14. System.out.println(ch);
  15. } else {
  16. fontShapeCode = read(fontCode[0], fontCode[1]);
  17. if (fontShapeCode != null) {
  18. allFontCodingLk.add((int) ch);
  19. addByteToLinkedList(allFontShapeLk, fontShapeCode);
  20. } else {
  21. System.out.println(ch + " " + fontCode[0] + " "
  22. + fontCode[1]);
  23. }
  24. }
  25. }
  26. try {
  27. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  28. DataOutputStream dataOutputStream = new DataOutputStream(
  29. outputStream);
  30. dataOutputStream.writeByte(FontHeight);
  31. dataOutputStream.writeByte(ChineseWidth);
  32. int size = allFontCodingLk.size();
  33. dataOutputStream.writeInt(size);
  34. Integer[] allFondCode = new Integer[size];
  35. allFontCodingLk.toArray(allFondCode);
  36. for (int i = 0; i < size; i++) {
  37. dataOutputStream.writeInt(allFondCode[i].intValue());
  38. }
  39. Byte[] allFontShapeCode = new Byte[allFontShapeLk.size()];
  40. allFontShapeLk.toArray(allFontShapeCode);
  41. for (int i = 0; i < allFontShapeCode.length; i++) {
  42. dataOutputStream.writeByte(allFontShapeCode[i].byteValue());
  43. }
  44. return outputStream.toByteArray();
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return null;
  49. }

  7.输出到相应的文件中

  1. public void generatesFontFile(byte[] b) {
  2. try {
  3. File file = new File("D:/J2ME/ToolFont/myFont.bin");
  4. FileOutputStream out = new FileOutputStream(file);
  5. try {
  6. BufferedOutputStream buf = new BufferedOutputStream(out);
  7. if (b != null) {
  8. buf.write(b, 0, b.length);
  9. }
  10. buf.flush();
  11. buf.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. } catch (FileNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. }

 程序执行如图:

  

 

8.编写加载部分字库的函数

  1. static void initFontByte() {
  2. try {
  3. InputStream inputStream = "".getClass()
  4. .getResourceAsStream(ZK_PATH);
  5. DataInputStream dataInputStream = new DataInputStream(inputStream);
  6. fontHeight = dataInputStream.readByte();
  7. chineseCharWidth = dataInputStream.readByte();
  8. NUM_OF_CH_CHAR = dataInputStream.readInt();
  9. chineseFontCode = new int[NUM_OF_CH_CHAR];
  10. for (int i = 0; i < NUM_OF_CH_CHAR; i++) {
  11. chineseFontCode[i] = dataInputStream.readInt();
  12. }
  13. int oneChMSize = fontHeight * (((chineseCharWidth - 1) >> 3) + 1);
  14. int allSize = oneChMSize * NUM_OF_CH_CHAR;
  15. chineseShapeCode = new byte[allSize];
  16. dataInputStream.read(chineseShapeCode, 0, allSize);
  17. dataInputStream.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }

 

 10. 下面我需要最后添加一个函数,就是在对应的机内码集中将自己的汉字位置找出来。还记得上一节中,我们说的排序吗???  这里在从机内码中将自己的汉字找出来,根据其排序采用二分查找,以此来加快程序的执行速度。根据机内码的位置,得到字形码的位置,将汉字绘制出来。

       这个函数,我就不贴出来了,相信同仁是可以写出来的!!

  

       谢谢大家的阅读! 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/715932
推荐阅读
相关标签
  

闽ICP备14008679号