zoukankan      html  css  js  c++  java
  • 读取点阵字库

    本次所用字库为横向取模的16*16点阵字库,ucdos自带。

    源字库文件放到资源文件中并嵌入exe,应用时只复制exe到用户处,双击即可运行并提取字库,不用再复制ASC16及HZK16文件。

    可将此功能做成dll,方便别的项目调用。

    建立好项目后建立一个资源文件,然后添加文件,将ASC16及HZK16文件添加到资源文件中。

    读取资源文件中的两个字库文件到文件流:

     ResourceManager resource = new ResourceManager(typeof(Resource1));//About为资源文件名(没有扩展名)
                byte[] bitASC16 = resource.GetObject("ASC16") as byte[];//图片资源名
                byte[] bitHZK16 = resource.GetObject("HZK16") as byte[];//图片资源名

                //将byte[]写入FileStream
                FileStream fsAsc16 = new FileStream("sASC16", FileMode.OpenOrCreate);
                fsAsc16.Write(bitASC16, 0, bitASC16.Length);
                FileStream fsHzk16 = new FileStream("sHZK16", FileMode.OpenOrCreate);
                fsHzk16.Write(bitHZK16, 0, bitHZK16.Length);

    具体实现代码(textBox1.Text为要取字模的字符,半角或全角,数字、字母、汉字;textBox2.Text为取到的十六进制的16*16点阵字库):

    private void btnGet_Click(object sender, EventArgs e)
            {

    ResourceManager resource = new ResourceManager(typeof(Resource1));//Resource1为资源文件名(没有扩展名)

     byte[] bitASC16 = resource.GetObject("ASC16") as byte[];//添加到资源文件中的文件名            

    byte[] bitHZK16 = resource.GetObject("HZK16") as byte[];//添加到资源文件中的文件名

     textBox2.Text = "";            

    int cnt = 16;            

    string message = textBox1.Text;        

    FileStream fsAsc16 = new FileStream("sASC16", FileMode.OpenOrCreate);            

    fsAsc16.Write(bitASC16, 0, bitASC16.Length);            

    FileStream fsHzk16 = new FileStream("sHZK16", FileMode.OpenOrCreate);            

    fsHzk16.Write(bitHZK16, 0, bitHZK16.Length);

    //字库不放入资源文件,单独放于可执行文件同级目录下

    //FileStream fsAsc16 = new FileStream("ASC16", FileMode.Open);    //将"ASC16"字库文件读入文件流            

    //FileStream fsHzk16 = new FileStream("HZK16", FileMode.Open);    //将"HZK16"字库文件读入文件流            

    for (int index = 0; index < message.Length; index++)            

    {

     string s = message.Substring(index, 1);                

    if (Convert.ToChar(s) < 256)    //判断是否为中文,小于256为字符                

    {                    

    byte[] bMsg = new byte[16];                    

    int offset = cnt * (byte)(s[0]);                    

    fsAsc16.Seek(offset, SeekOrigin.Begin);                    

    fsAsc16.Read(bMsg, 0, 16);                    

    for (int i = 0; i < bMsg.Length; i++)                    

    {                        

    textBox2.Text += bMsg[i].ToString("X2");                    

    }                                 

    }                

    else                

    {                    

    byte[] bMsg = new byte[32];                    

    byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(s.ToCharArray());                    

    int offset = 32 * (94 * (bytes[0] - 0xA1) + bytes[1] - 0xA1);                    

    fsHzk16.Seek(offset, SeekOrigin.Begin);                    

    fsHzk16.Read(bMsg, 0, 32);

                        for (int i = 0; i < bMsg.Length; i++)                    

    {                       

    textBox2.Text += bMsg[i].ToString("X2");                    

    }                

    }                

    }

    fsAsc16.Close();            

    fsHzk16.Close();            

     }

    参考资料:C# byte[]和文件FileStream相互转化 http://www.cnblogs.com/wskfire/archive/2007/11/30/978212.html

    using System.IO;

    //读filename到byte[]

            private byte[] ReadFile(string fileName)

            {

                FileStream pFileStream = null;

                byte[] pReadByte = new byte[0];

                try

                {

                    pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                    BinaryReader r = new BinaryReader(pFileStream);

                    r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开

                    pReadByte = r.ReadBytes((int)r.BaseStream.Length);

                    return pReadByte;

                }

                catch

                {

                    return pReadByte;

                }

                finally

                {

                    if (pFileStream != null)

                        pFileStream.Close();

                }

            }

            //写byte[]到fileName

            private bool writeFile(byte[] pReadByte, string fileName)

            {

                FileStream pFileStream = null;

                try

                {

                    pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);

                    pFileStream.Write(pReadByte, 0, pReadByte.Length);

                }

                catch

                {

                    return false;

                }

                finally

                {

                    if (pFileStream != null)

                        pFileStream.Close();

                }

                return true;

            }

    参考资料:C# 读取点阵字库  http://blog.163.com/ekinhb@126/blog/static/35551315200972313221889/

    List<byte[]> listMsg = new List<byte[]>();

    for (int index = 0; index < message.Length; index++)

    {    

    FileStream fsAsc16 = new FileStream("ASC16", FileMode.Open);    //将"ASC16"字库文件读入文件流    

    FileStream fsHzk16 = new FileStream("HZK16", FileMode.Open);    //将"HZK16"字库文件读入文件流

        string s = message.Substring(index, 1);    

    if (Convert.ToChar(s) < 256)    //判断是否为中文,小于256为字符    

    {        

    byte[] bMsg = new byte[16];        

    int offset = cnt *  (byte)(s[0]);        

    fsAsc16.Seek(offset, SeekOrigin.Begin);        

    fsAsc16.Read(bMsg, 0, 16);        

    listMsg.Add(bMsg);    

    }    

    else    

    {        

    byte[] bMsg = new byte[32];        

    byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(s.ToCharArray());        

    int offset = 32 * (94 * (bytes[0] - 0xA1) + bytes[1] - 0xA1);        

    fsHzk16.Seek(offset, SeekOrigin.Begin);        

    fsHzk16.Read(bMsg, 0, 32);

     byte[] bMsgH = new byte[cnt];        

    byte[] bMsgL = new byte[cnt];        

    int l = 0;        

    int h = 0;        

    for (int j = 0; j < cnt * 2; j++)        

    {            

    if (j % 2 == 0)                

    bMsgH[h++] = bMsg[j];            

    else                

    bMsgL[l++] = bMsg[j];        

    }        

    listMsg.Add(bMsgH);        

    listMsg.Add(bMsgL);    

    }    

    fsAsc16.Close();    

    fsHzk16.Close();

    }

    //为字节数组排序

    byte[] bMessages = new byte[list.Count * 16];

    for (int i = 0; i < list.Count; i++)

    {    

    for (int j = 0; j < list[i].Length; j++)    

    {        

    bMessages[i + j * list.Count] = list[i][j];    

    }

    }

  • 相关阅读:
    Java中的权限修饰符
    return,break,continue三者的区别
    JS代码放在不同位置的区别
    创建画笔工具
    关于SPH的核函数求导过程
    c++多级指针与“多维”数组 摘自别人博客
    RAP开发入门-搭建RAP开发环境(一)
    06 media媒体查询
    resolution 像素密度
    手动配置viewport-04
  • 原文地址:https://www.cnblogs.com/enjoyprogram/p/2473131.html
Copyright © 2011-2022 走看看