Java调取16*16点阵HZK汉字库并打印点阵

读懂代码需要了解以下几个知识点:

1、GB2312编码

GB2312编码https://www.qqxiuzi.cn/zh/hanzi-gb2312-bianma.php#:~:text=GB2312%E8%A7%84%E5%AE%9A%E5%AF%B9%E6%94%B6%E5%BD%95%E7%9A%84%E6%AF%8F%E4%B8%AA%E5%AD%97%E7%AC%A6%E9%87%87%E7%94%A8%E4%B8%A4%E4%B8%AA%E5%AD%97%E8%8A%82%E8%A1%A8%E7%A4%BA%EF%BC%8C%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%AD%97%E8%8A%82%E4%B8%BA%E2%80%9C%E9%AB%98%E5%AD%97%E8%8A%82%E2%80%9D%EF%BC%8C%E5%AF%B9%E5%BA%9494%E4%B8%AA%E5%8C%BA%EF%BC%9B%E7%AC%AC%E4%BA%8C%E4%B8%AA%E5%AD%97%E8%8A%82%E4%B8%BA%E2%80%9C%E4%BD%8E%E5%AD%97%E8%8A%82%E2%80%9D%EF%BC%8C%E5%AF%B9%E5%BA%9494%E4%B8%AA%E4%BD%8D%E3%80%82,%E6%89%80%E4%BB%A5%E5%AE%83%E7%9A%84%E5%8C%BA%E4%BD%8D%E7%A0%81%E8%8C%83%E5%9B%B4%E6%98%AF%EF%BC%9A0101%EF%BC%8D9494%E3%80%82%20%E5%8C%BA%E5%8F%B7%E5%92%8C%E4%BD%8D%E5%8F%B7%E5%88%86%E5%88%AB%E5%8A%A0%E4%B8%8A0xA0%E5%B0%B1%E6%98%AFGB2312%E7%BC%96%E7%A0%81%E3%80%82

2、RandomAccessFile文件指针操作

Java文件指针操作https://www.bjsxt.com/a/8455.html#:~:text=%E5%9C%A8,Java%E5%BC%80%E5%8F%91%20%E4%B8%AD%E7%94%A8%E4%B8%80%E4%B8%AA%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E6%8C%87%E5%90%91%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%EF%BC%8C%E8%BF%99%E4%B8%AA%E6%8C%87%E9%92%88%E7%A7%B0%E4%B8%BA%E6%96%87%E4%BB%B6%E6%8C%87%E9%92%88%E3%80%823、区位码计算偏移字节数

offset = (94 * (区码 - 1) + (位码 - 1)) * 32;

主要思路:计算出汉字在汉字库中的区位码,计算出偏移量offset,利用文件指针读取汉字库文件。

//-------------------

需要读取16*16汉字库文件

链接:https://pan.baidu.com/s/1t-nDsVYTcFgiVWgGtTdUAA 
提取码:zlcc

//-------------------

实现代码:
import java.io.File;
import java.io.RandomAccessFile;


public class Hz {
    //获取区位码
    private static String bytes2HexString(byte b) {
        return bytes2HexString(new byte[]{b});
    }

    private static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }
    
    public static int[] getLocation(String str) throws Exception {
        byte[] bs = str.getBytes("GB2312");
        int[] s = new int[str.length() * 2];
        for (int i = 0; i < bs.length; i++) {
            int a = Integer.parseInt(bytes2HexString(bs[i]), 16);
            s[i] = (a - 0x80 - 0x20);
        }
        return s;
    }


    public static void Print(String str) throws Exception {
        File file = new File("hzk16");
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        int i, j, k;
        int offset;
        int flag;
        byte[] buffer = new byte[32];

        char[] key = {
                0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
        };
        if (!file.canRead()) {
            System.out.println("err hzk16\n");
            return;
        }


        for (int a = 0; a < str.length()*2; a+=2) {
            int[] location = getLocation(str);
         
            offset = (94 *  (location[a] - 1) + (location[a+1]- 1)) * 32;

            System.out.println(offset);
            raf.seek(offset);
            raf.read(buffer, 0, 31);
//            for (int l = 0; l < buffer.length; l++) {
                System.out.printf("%B",buffer[l]);
//                System.out.println(buffer[l]);
//            }

            for (k = 0; k < 16; k++) {
                for (j = 0; j < 2; j++) {
                    for (i = 0; i < 8; i++) {
                        flag = buffer[k * 2 + j] & key[i];
                        if (flag > 1) System.out.print("● ");
                        else System.out.print("○ ");

                    }
                }
                System.out.print("\n");
            }
            System.out.print("uchar code key[32] = {");
            for (k = 0; k < 31; k++) {
                System.out.printf("0x%02X,", buffer[k]);
            }
            System.out.printf("0x%02X};\n", buffer[31]);
            System.out.print("\n");
        }
        raf.close();

    }
}

运行:

    @Test
    public void hz() throws Exception {
        Hz.Print("汉字");
    }

结果:

76000
○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ 
○ ● ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ 
○ ○ ● ● ○ ● ● ○ ● ● ● ● ● ● ○ ○ 
○ ○ ○ ● ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ 
● ○ ○ ○ ○ ○ ● ○ ○ ○ ○ ○ ● ○ ○ ○ 
○ ● ● ○ ○ ○ ● ○ ○ ○ ○ ○ ● ○ ○ ○ 
○ ○ ● ○ ○ ○ ● ○ ○ ○ ○ ● ○ ○ ○ ○ 
○ ○ ○ ○ ● ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○ 
○ ○ ○ ● ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○ ○ 
○ ○ ● ○ ○ ○ ○ ○ ● ○ ● ○ ○ ○ ○ ○ 
● ● ● ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○ ○ ○ 
○ ○ ● ○ ○ ○ ○ ○ ● ○ ● ○ ○ ○ ○ ○ 
○ ○ ● ○ ○ ○ ○ ○ ○ ○ ○ ● ○ ○ ○ ○ 
○ ○ ● ○ ○ ○ ● ○ ○ ○ ○ ○ ● ○ ○ ○ 
○ ○ ● ○ ○ ● ○ ○ ○ ○ ○ ○ ● ● ● ○ 
○ ○ ○ ○ ● ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ ○ 
uchar code key[32] = {0x00,0x00,0x40,0x08,0x37,0xFC,0x10,0x08,0x82,0x08,0x62,0x08,0x22,0x10,0x09,0x10,0x11,0x20,0x20,0xA0,0xE0,0x40,0x20,0xA0,0x21,0x10,0x22,0x08,0x24,0x0E,0x08,0x00};


进程已结束,退出代码0

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值